diff --git a/cbporganizer-backend/src/main/java/cbporganizer/FileController.java b/cbporganizer-backend/src/main/java/cbporganizer/FileController.java index e864f5f..65e6c86 100644 --- a/cbporganizer-backend/src/main/java/cbporganizer/FileController.java +++ b/cbporganizer-backend/src/main/java/cbporganizer/FileController.java @@ -2,11 +2,14 @@ import cbporganizer.service.FilesStorageService; import org.springframework.beans.factory.annotation.Autowired; +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.client.HttpClientErrorException; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpSession; import java.util.List; @RestController @@ -17,14 +20,26 @@ public class FileController { FilesStorageService storageService; @PostMapping("/upload") - public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) { - storageService.save(file); + public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file, HttpSession session) { + String userId = getUserIdFromSession(session); + + storageService.save(file, userId); return ResponseEntity.status(HttpStatus.OK).body("Uploaded the file successfully: " + file.getOriginalFilename()); } @GetMapping("/") - public ResponseEntity> getFiles() { - List fileList = storageService.getFiles(); + public ResponseEntity> getFiles(HttpSession session) { + String userId = getUserIdFromSession(session); + + List fileList = storageService.getFiles(userId); return new ResponseEntity<>(fileList, HttpStatus.OK); } + + private String getUserIdFromSession(HttpSession session) { + String userId = (String) session.getAttribute("userId"); + if (userId == null) { + throw HttpClientErrorException.Unauthorized.create(HttpStatus.UNAUTHORIZED, "Unauthorized", HttpHeaders.EMPTY, null, null); + } + return userId; + } } diff --git a/cbporganizer-backend/src/main/java/cbporganizer/service/FilesStorageService.java b/cbporganizer-backend/src/main/java/cbporganizer/service/FilesStorageService.java index e14eff0..7c2e6a8 100644 --- a/cbporganizer-backend/src/main/java/cbporganizer/service/FilesStorageService.java +++ b/cbporganizer-backend/src/main/java/cbporganizer/service/FilesStorageService.java @@ -8,7 +8,7 @@ public interface FilesStorageService { void init(); - void save(MultipartFile file); + void save(MultipartFile file, String userId); - List getFiles(); + List getFiles(String userId); } diff --git a/cbporganizer-backend/src/main/java/cbporganizer/service/FilesStorageServiceImpl.java b/cbporganizer-backend/src/main/java/cbporganizer/service/FilesStorageServiceImpl.java index 965410b..f37c247 100644 --- a/cbporganizer-backend/src/main/java/cbporganizer/service/FilesStorageServiceImpl.java +++ b/cbporganizer-backend/src/main/java/cbporganizer/service/FilesStorageServiceImpl.java @@ -30,28 +30,41 @@ public void init() { } @Override - public void save(MultipartFile file) { - Path filePath = this.root.resolve(file.getOriginalFilename()); + public void save(MultipartFile file, String userId) { + Path userDir = getUserPath(userId); try { + if (!Files.exists(userDir)) { + Files.createDirectory(userDir); + } + Path filePath = userDir.resolve(file.getOriginalFilename()); + Files.copy(file.getInputStream(), filePath, java.nio.file.StandardCopyOption.REPLACE_EXISTING); - extractGZip(filePath.toFile()); + extractGZip(filePath.toFile(), userId); } catch (IOException e) { e.printStackTrace(); } } - private void extractGZip(File inputFile) { + /* + * Resolves the path to the user's directory by the userId. + */ + private Path getUserPath(String userId) { + return root.resolve(userId); + } + + private void extractGZip(File inputFile, String userId) { + Path userDir = getUserPath(userId); try { TarArchiveInputStream tarInput = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(inputFile))); TarArchiveEntry entry; while((entry = tarInput.getNextTarEntry()) != null) { if (entry.isDirectory()) { - new File(root.resolve(entry.getName()).toString()).mkdirs(); + new File(userDir.resolve(entry.getName()).toString()).mkdirs(); } else { byte[] buffer = new byte[1024]; - File outFile = root.resolve(entry.getName()).toFile(); + File outFile = userDir.resolve(entry.getName()).toFile(); outFile.getParentFile().mkdirs(); try (FileOutputStream fos = new FileOutputStream(outFile)) { int len; @@ -67,10 +80,11 @@ private void extractGZip(File inputFile) { } @Override - public List getFiles() { + public List getFiles(String userId) { + Path userDir = getUserPath(userId); List ret = new LinkedList<>(); try { - ret = Files.walk(root) + ret = Files.walk(userDir) .filter(Files::isRegularFile) .map(file -> file.getFileName().toString()) .collect(Collectors.toList());