Skip to content

Commit

Permalink
issue pughlab#5: allow owner to upload study for individual folder lo…
Browse files Browse the repository at this point in the history
…cation based on userId
  • Loading branch information
mickey-ng committed Mar 3, 2023
1 parent f359e50 commit 3cfa49a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,14 +20,26 @@ public class FileController {
FilesStorageService storageService;

@PostMapping("/upload")
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
storageService.save(file);
public ResponseEntity<String> 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<List<String>> getFiles() {
List<String> fileList = storageService.getFiles();
public ResponseEntity<List<String>> getFiles(HttpSession session) {
String userId = getUserIdFromSession(session);

List<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface FilesStorageService {

void init();

void save(MultipartFile file);
void save(MultipartFile file, String userId);

List<String> getFiles();
List<String> getFiles(String userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -67,10 +80,11 @@ private void extractGZip(File inputFile) {
}

@Override
public List<String> getFiles() {
public List<String> getFiles(String userId) {
Path userDir = getUserPath(userId);
List<String> ret = new LinkedList<>();
try {
ret = Files.walk(root)
ret = Files.walk(userDir)
.filter(Files::isRegularFile)
.map(file -> file.getFileName().toString())
.collect(Collectors.toList());
Expand Down

0 comments on commit 3cfa49a

Please sign in to comment.