Skip to content

Commit

Permalink
Add basic files upload and on disk persistence api.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArefMT committed Jan 14, 2024
1 parent 7c0d90e commit 2d530dc
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -21,9 +26,30 @@ public SimulationRunningController(SimulationRunningService simulationRunningSer
}

@PostMapping("/simulate")
public ResponseEntity<String> handleSimulationRequest(@RequestBody String request){
public ResponseEntity<String> handleSimulationRequest(@RequestBody String request) {

return new ResponseEntity<>("Im a response", HttpStatus.OK);
}

}
@PostMapping("/simulate/upload")
public ResponseEntity<String> 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<String> 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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -19,4 +26,6 @@ public Experiment runExperiment(final ExperimentStartupConfig startupConfig) {

return experiment;
}


}
4 changes: 4 additions & 0 deletions restAPI/src/main/java/restAPI/util/DBConnector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package restAPI.util;

public class DBConnector {
}
66 changes: 66 additions & 0 deletions restAPI/src/main/java/restAPI/util/TempFileUtil.java
Original file line number Diff line number Diff line change
@@ -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" +
" <architecture_> or <experiment_>.");
}
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");
}

}
4 changes: 3 additions & 1 deletion restAPI/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@

spring.servlet.multipart.file-size-threshold=2KB
spring.servlet.multipart.max-file-size=50MB
spring.servlet.multipart.max-request-size=55MB

0 comments on commit 2d530dc

Please sign in to comment.