-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from Konrad-Abramowski/development
Deploy version 1.0.0
- Loading branch information
Showing
67 changed files
with
1,657 additions
and
12,630 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM maven:3.6.3-jdk-11 AS build | ||
WORKDIR /usr/local/service | ||
COPY src /usr/local/service/src | ||
COPY pom.xml /usr/local/service | ||
RUN mvn -f /usr/local/service/pom.xml clean package | ||
|
||
FROM adoptopenjdk/openjdk11:latest | ||
EXPOSE 8080 | ||
WORKDIR /usr/local/service | ||
COPY --from=build /usr/local/service/target/smart-home-0.0.1-SNAPSHOT.jar /usr/local/service/target/smart-home-0.0.1-SNAPSHOT.jar | ||
RUN apt-get update | ||
RUN apt-get -y upgrade | ||
RUN apt-get -y install cups-client | ||
ENTRYPOINT ["java", "-jar", "target/smart-home-0.0.1-SNAPSHOT.jar"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
backend/src/main/java/com/server/smarthome/SmartHomeApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
package com.server.smarthome; | ||
|
||
import com.server.smarthome.properties.StorageProperties; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
|
||
@SpringBootApplication | ||
@EnableConfigurationProperties(StorageProperties.class) | ||
public class SmartHomeApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SmartHomeApplication.class, args); | ||
} | ||
|
||
} |
137 changes: 137 additions & 0 deletions
137
backend/src/main/java/com/server/smarthome/controller/FileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package com.server.smarthome.controller; | ||
|
||
|
||
import com.server.smarthome.model.File; | ||
import com.server.smarthome.model.PrinterConfiguration; | ||
import com.server.smarthome.model.ResponseFile; | ||
import com.server.smarthome.service.FileServiceImpl; | ||
import org.apache.pdfbox.pdmodel.PDDocument; | ||
import org.apache.pdfbox.printing.PDFPageable; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; | ||
|
||
import javax.print.PrintService; | ||
import javax.print.PrintServiceLookup; | ||
import java.awt.print.PrinterException; | ||
import java.awt.print.PrinterJob; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@CrossOrigin(origins = "*") | ||
@RestController | ||
@RequestMapping("/") | ||
public class FileController { | ||
|
||
private FileServiceImpl fileService; | ||
private String selectedPrinter; | ||
|
||
public FileController(final FileServiceImpl fileService) { | ||
this.fileService = fileService; | ||
} | ||
|
||
@PostMapping("/upload") | ||
public ResponseEntity<?> uploadFiles(@RequestParam("file") MultipartFile[] files) { | ||
try { | ||
for (MultipartFile file : files) { | ||
fileService.store(file); | ||
} | ||
|
||
return ResponseEntity.status(HttpStatus.OK). | ||
body(fileService.getAllFilesAsResponseFileList()); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED). | ||
body("Uploading file failed"); | ||
} | ||
} | ||
|
||
@GetMapping("/files") | ||
public ResponseEntity<List<ResponseFile>> getListFiles() { | ||
return ResponseEntity.status(HttpStatus.OK). | ||
body(fileService.getAllFilesAsResponseFileList()); | ||
} | ||
|
||
@GetMapping("/files/{id}") | ||
public ResponseEntity<byte[]> getFile(@PathVariable String id) { | ||
File file = fileService.getFile(id); | ||
|
||
return ResponseEntity.ok() | ||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"") | ||
.body(file.getData()); | ||
} | ||
|
||
@DeleteMapping("files/{id}") | ||
public ResponseEntity deleteFile(@PathVariable String id) { | ||
boolean result = fileService.deleteFileById(id); | ||
if (result) { | ||
return ResponseEntity.status(HttpStatus.OK). | ||
body(fileService.getAllFilesAsResponseFileList()); | ||
} else { | ||
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | ||
} | ||
} | ||
|
||
@GetMapping("/printer/available-printers") | ||
public ResponseEntity<List<String>> getPrinters() { | ||
return new ResponseEntity<>(Arrays.stream(PrintServiceLookup.lookupPrintServices(null, null)) | ||
.map(PrintService::getName).collect(Collectors.toList()), HttpStatus.OK); | ||
|
||
} | ||
|
||
@PostMapping("/printer/config/{selectedPrinter}") | ||
public ResponseEntity<String> configurePrinter(@PathVariable String selectedPrinter) { | ||
this.selectedPrinter = selectedPrinter; | ||
return new ResponseEntity<>(HttpStatus.OK); | ||
} | ||
|
||
@GetMapping("/printer/config") | ||
public ResponseEntity<PrinterConfiguration> getPrinter() { | ||
return new ResponseEntity<>(new PrinterConfiguration(this.selectedPrinter), HttpStatus.OK); | ||
} | ||
|
||
@PostMapping("/printAll") | ||
public ResponseEntity printAllFiles() { | ||
List<ResponseFile> files = fileService.getAllFiles().map(dbFile -> { | ||
String fileDownloadUri = ServletUriComponentsBuilder | ||
.fromCurrentContextPath() | ||
.path("/files/") | ||
.path(dbFile.getId()) | ||
.toUriString(); | ||
|
||
return new ResponseFile( | ||
dbFile.getId(), | ||
dbFile.getName(), | ||
fileDownloadUri, | ||
dbFile.getType(), | ||
dbFile.getData().length); | ||
}).collect(Collectors.toList()); | ||
|
||
try { | ||
PrintService myPrintService = fileService.findPrintService(selectedPrinter); | ||
|
||
for (int i = 0; i < files.size(); i++) { | ||
URL url = new URL(files.get(i).getUrl()); | ||
java.io.File file = new java.io.File("file" + i + ".pdf"); | ||
org.apache.commons.io.FileUtils.copyURLToFile(url, file); | ||
PDDocument document = PDDocument.load(file); | ||
PrinterJob job = PrinterJob.getPrinterJob(); | ||
job.setPageable(new PDFPageable(document)); | ||
job.setPrintService(myPrintService); | ||
job.print(); | ||
file.deleteOnExit(); | ||
document.close(); | ||
} | ||
|
||
} catch (IOException | PrinterException e) { | ||
e.printStackTrace(); | ||
} | ||
return new ResponseEntity<>(HttpStatus.OK); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/com/server/smarthome/exception/StorageException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.server.smarthome.exception; | ||
|
||
public class StorageException extends RuntimeException { | ||
|
||
public StorageException(String message) { | ||
super(message); | ||
} | ||
|
||
public StorageException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/com/server/smarthome/exception/StorageFileNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.server.smarthome.exception; | ||
|
||
public class StorageFileNotFoundException extends StorageException { | ||
|
||
public StorageFileNotFoundException(String message) { | ||
super(message); | ||
} | ||
|
||
public StorageFileNotFoundException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
backend/src/main/java/com/server/smarthome/model/File.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.server.smarthome.model; | ||
|
||
import org.hibernate.annotations.GenericGenerator; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Table(name = "files") | ||
public class File { | ||
|
||
@Id | ||
@GeneratedValue(generator = "uuid") | ||
@GenericGenerator(name = "uuid", strategy = "uuid2") | ||
private String id; | ||
|
||
private String name; | ||
|
||
private String type; | ||
|
||
@Lob | ||
private byte[] data; | ||
|
||
public File() {} | ||
|
||
public File(final String name, final String type, final byte[] data) { | ||
this.name = name; | ||
this.type = type; | ||
this.data = data; | ||
} | ||
|
||
public void setName(final String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setType(final String type) { | ||
this.type = type; | ||
} | ||
|
||
public void setData(final byte[] data) { | ||
this.data = data; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public byte[] getData() { | ||
return data; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/com/server/smarthome/model/PrinterConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.server.smarthome.model; | ||
|
||
public class PrinterConfiguration { | ||
|
||
private String name; | ||
|
||
public PrinterConfiguration() { | ||
} | ||
|
||
public PrinterConfiguration(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
Oops, something went wrong.