diff --git a/restAPI/src/main/java/restAPI/simulation_running/SimulationRunningController.java b/restAPI/src/main/java/restAPI/simulation_running/SimulationRunningController.java index 6423c31a..21a02bcf 100644 --- a/restAPI/src/main/java/restAPI/simulation_running/SimulationRunningController.java +++ b/restAPI/src/main/java/restAPI/simulation_running/SimulationRunningController.java @@ -6,7 +6,12 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; +import restAPI.util.TempFileUtil; +import java.nio.file.Path; +import java.io.IOException; @RestController public class SimulationRunningController { @@ -21,9 +26,30 @@ public SimulationRunningController(SimulationRunningService simulationRunningSer } @PostMapping("/simulate") - public ResponseEntity handleSimulationRequest(@RequestBody String request){ + public ResponseEntity handleSimulationRequest(@RequestBody String request) { return new ResponseEntity<>("Im a response", HttpStatus.OK); } -} + @PostMapping("/simulate/upload") + public ResponseEntity fileUploading(@RequestParam("file") MultipartFile file) { + // Code to save the file to a database or disk + return ResponseEntity.ok("Successfully uploaded the file"); + } + + + //for uploading the MULTIPLE file to the File system + @PostMapping("/multiple/file") + public ResponseEntity handleMultipleFilesUpload(@RequestParam("files") MultipartFile[] files) + throws IOException { + try { + Path tmpFolder = TempFileUtil.createDefaultTempDir(); + Path[] savedFiles = TempFileUtil.saveFiles(files, tmpFolder); + return ResponseEntity.ok("Successfully uploaded the file"); + } + catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + +} \ No newline at end of file diff --git a/restAPI/src/main/java/restAPI/simulation_running/SimulationRunningService.java b/restAPI/src/main/java/restAPI/simulation_running/SimulationRunningService.java index 6c1ca5f4..d8393505 100644 --- a/restAPI/src/main/java/restAPI/simulation_running/SimulationRunningService.java +++ b/restAPI/src/main/java/restAPI/simulation_running/SimulationRunningService.java @@ -2,13 +2,20 @@ import cambio.simulator.ExperimentStartupConfig; import desmoj.core.simulator.Experiment; +import jdk.jshell.spi.ExecutionControl; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MaxUploadSizeExceededException; +import org.springframework.web.multipart.MultipartFile; import restAPI.simulation_running.restufl_experiment_creation.RestfulExperimentCreator; +import java.util.Arrays; + @Service public class SimulationRunningService { - +// TODO delete the uploaded temp files after creating the experiments. +// TODO delete the created output files after saving to DB. public Experiment runExperiment(final ExperimentStartupConfig startupConfig) { Experiment experiment = new RestfulExperimentCreator().createSimulationExperiment(startupConfig); System.out.printf("[INFO] Starting simulation at approximately %s%n", java.time.LocalDateTime.now()); @@ -19,4 +26,6 @@ public Experiment runExperiment(final ExperimentStartupConfig startupConfig) { return experiment; } + + } diff --git a/restAPI/src/main/java/restAPI/util/DBConnector.java b/restAPI/src/main/java/restAPI/util/DBConnector.java new file mode 100644 index 00000000..eae73d07 --- /dev/null +++ b/restAPI/src/main/java/restAPI/util/DBConnector.java @@ -0,0 +1,4 @@ +package restAPI.util; + +public class DBConnector { +} diff --git a/restAPI/src/main/java/restAPI/util/TempFileUtil.java b/restAPI/src/main/java/restAPI/util/TempFileUtil.java new file mode 100644 index 00000000..21815b6d --- /dev/null +++ b/restAPI/src/main/java/restAPI/util/TempFileUtil.java @@ -0,0 +1,66 @@ +package restAPI.util; + +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MaxUploadSizeExceededException; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; + + +public class TempFileUtil { + + // Create a new temp file. + private static Path createTempFile(Path tmpDir, byte[] content, String prefix) throws IOException { + String customFileSuffix = ".json"; + + Path tmpFile = Files.createTempFile(tmpDir, prefix, customFileSuffix); + return Files.write(tmpFile, content); + } + + // TODO: check whether the uploaded file is a json file: String contentType = file.getContentType(); + // TODO: check the file name for the prefixes. + private static Path saveFile(MultipartFile file, Path path) throws Exception { + if(file.getOriginalFilename() == null) { + throw new IllegalArgumentException("The uploaded file must have a name that includes the prefix" + + " or ."); + } + String fileName = StringUtils.cleanPath(file.getOriginalFilename()); + + try { + if(fileName.contains("..") + || !fileName.startsWith("architecture_") + || !fileName.startsWith("experiment_") ) { + throw new Exception("Filename contains invalid path sequence " + fileName); + } else if (file.isEmpty()) { + throw new Exception(String.format("The uploaded file <%s> is empty.", fileName)); + } + byte[] content = file.getBytes(); + return createTempFile(path, content, fileName); + } catch (MaxUploadSizeExceededException e) { + throw new MaxUploadSizeExceededException(file.getSize()); + } catch (Exception e) { + throw new Exception("Could not save File: " + fileName); + } + } + public static Path[] saveFiles(MultipartFile[] files, Path temDir) { + Path[] filesPaths = new Path[files.length]; + int i = 0; + Arrays.asList(files).forEach(file -> { + try { + filesPaths[i] = saveFile(file, temDir); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + return filesPaths; + } + + // We create a temp directory in the default OS's /tmp folder. + public static Path createDefaultTempDir() throws IOException { + return Files.createTempDirectory("misim"); + } + +} diff --git a/restAPI/src/main/resources/application.properties b/restAPI/src/main/resources/application.properties index 8b137891..cc44b068 100644 --- a/restAPI/src/main/resources/application.properties +++ b/restAPI/src/main/resources/application.properties @@ -1 +1,3 @@ - +spring.servlet.multipart.file-size-threshold=2KB +spring.servlet.multipart.max-file-size=50MB +spring.servlet.multipart.max-request-size=55MB \ No newline at end of file