Skip to content

Commit

Permalink
Finish files upload api
Browse files Browse the repository at this point in the history
  • Loading branch information
ArefMT committed Jan 14, 2024
1 parent 2d530dc commit 3bf4eb1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
12 changes: 12 additions & 0 deletions restAPI/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
<version>3.3.4-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.0.0-jre</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package restAPI.simulation_running;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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 @@ -25,30 +28,22 @@ public SimulationRunningController(SimulationRunningService simulationRunningSer
this.simulationRunningService = simulationRunningService;
}

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

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

//For uploading the Multipart files and saving them to the file system. And then we run the simulation on them.
@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");
// TODO: handle running the simulation here.
// Do the clean-up
FileUtils.deleteDirectory(tmpFolder.toFile());
return new ResponseEntity<>("Files have been successfully uploaded.", HttpStatus.OK);
}
catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
String errorMessage = e.getMessage();
logger.error(errorMessage);
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

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);
Expand Down
18 changes: 6 additions & 12 deletions restAPI/src/main/java/restAPI/util/TempFileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
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);
Path tmpFile = Files.createTempFile(tmpDir, prefix, ".json");
return Files.write(tmpFile, content);
}

Expand All @@ -28,21 +25,18 @@ private static Path saveFile(MultipartFile file, Path path) throws Exception {
" <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);
|| !(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));
}
String fileNameWithoutExtension = com.google.common.io.Files.getNameWithoutExtension(fileName) + "_";
byte[] content = file.getBytes();
return createTempFile(path, content, fileName);
return createTempFile(path, content, fileNameWithoutExtension);
} 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) {
Expand All @@ -60,7 +54,7 @@ public static Path[] saveFiles(MultipartFile[] files, Path temDir) {

// We create a temp directory in the default OS's /tmp folder.
public static Path createDefaultTempDir() throws IOException {
return Files.createTempDirectory("misim");
return Files.createTempDirectory("misim-");
}

}

0 comments on commit 3bf4eb1

Please sign in to comment.