Added file details display

- Added password generator logic to backend
- Added UI for file details
This commit is contained in:
2024-02-16 17:27:06 +01:00
parent 58e3dd0af5
commit 43e0eb1766
6 changed files with 95 additions and 37 deletions

View File

@ -29,7 +29,7 @@ public class Upload extends ApiRestController {
// Currently testing
@PostMapping("/upload")
public ResponseEntity<String> getUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request,
@RequestParam(value = "password", required = false) String password,
@RequestParam(value = "passwordProtected", required = false) Boolean passwordProtected,
@RequestParam(value = "singleDownload", defaultValue = "false") boolean singleDownload,
@RequestParam(value = "fileDescription", required = false) String fileDescription) {
@ -40,15 +40,17 @@ public class Upload extends ApiRestController {
return new ResponseEntity<>("Please select a file to upload.", HttpStatus.NOT_ACCEPTABLE);
}
FileUpload fileUpload = fileService.processUploadedFile(file, request.getRemoteAddr(), password, singleDownload, fileDescription);
FileUpload fileUpload = fileService.processUploadedFile(file, request.getRemoteAddr(), passwordProtected, singleDownload, fileDescription);
log.debug("User uploaded file " + file.getOriginalFilename() + " from IP " + request.getRemoteAddr() + " successfully.");
JsonObject response = new JsonObject();
response.addProperty("fileId", fileUpload.getFileId());
response.addProperty("fileName", fileUpload.getFileName());
response.addProperty("message", "File " + file.getOriginalFilename() + " uploaded successfully!");
response.addProperty("passwordProtected", fileUpload.isPasswordProtected());
response.addProperty("singleDownload", fileUpload.isSingleDownload());
response.addProperty("uploadDate", fileUpload.getUploadDate().toString());
response.addProperty("password", fileUpload.getDownloadPassword());
return new ResponseEntity<>(response.toString(), HttpStatus.OK);
}

View File

@ -3,14 +3,20 @@ package de.w665.sharepulse.service;
import de.w665.sharepulse.exception.NoDownloadPermissionException;
import de.w665.sharepulse.model.FileUpload;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.Random;
@Slf4j
@Service
public class FileSecurityService {
@Value("${sharepulse.filepassword-length}")
private int passwordLength;
@Value("${sharepulse.filepassword-charset}")
private String passwordCharset;
private final Random random = new Random();
public boolean verifyDownloadPermission(FileUpload file, String password) throws NoDownloadPermissionException {
@ -34,4 +40,16 @@ public class FileSecurityService {
throw new NoDownloadPermissionException("Password protected file can only be downloaded with correct password.");
}
public String generateFilePassword() {
StringBuilder password = new StringBuilder(passwordLength);
for (int i = 0; i < passwordLength; i++) {
int randomIndex = random.nextInt(passwordCharset.length());
char randomChar = passwordCharset.charAt(randomIndex);
password.append(randomChar);
}
return password.toString();
}
}

View File

@ -23,15 +23,16 @@ import java.util.Date;
public class FileService {
private final FileIdService fileIdService;
private final FileSecurityService fileSecurityService;
private final FileUploadRepository fileUploadRepository;
@Value("${sharepulse.temp-filestore-path}")
private String tempDirPath;
@Autowired
public FileService(FileIdService fileIdService, FileUploadRepository fileUploadRepository) {
public FileService(FileIdService fileIdService, FileSecurityService fileSecurityService, FileUploadRepository fileUploadRepository) {
this.fileIdService = fileIdService;
this.fileSecurityService = fileSecurityService;
this.fileUploadRepository = fileUploadRepository;
}
@ -53,10 +54,12 @@ public class FileService {
}
}
public FileUpload processUploadedFile(MultipartFile file, String uploaderIp, String password, boolean singleDownload, String fileDescription) {
public FileUpload processUploadedFile(MultipartFile file, String uploaderIp, boolean passwordProtected, boolean singleDownload, String fileDescription) {
String fileId = fileIdService.generateNewUniqueId();
String password = fileSecurityService.generateFilePassword(); // TODO: generate password
FileUpload fileUpload = FileUpload.builder()
.fileId(fileId)
.fileName(file.getOriginalFilename())
@ -66,7 +69,7 @@ public class FileService {
.uploadedByIpAddress(uploaderIp)
.downloadCount(0)
.fileDescription(fileDescription)
.passwordProtected(password != null && !password.isEmpty())
.passwordProtected(passwordProtected)
.downloadPassword(password)
.build();

View File

@ -1,5 +1,7 @@
# Application config
sharepulse.temp-filestore-path=/temp-filestore
sharepulse.filepassword-length=6
sharepulse.filepassword-charset=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
# Static path
spring.web.resources.static-locations=classpath:/static/browser/
@ -11,9 +13,6 @@ spring.data.rest.base-path=/api/v1
spring.servlet.multipart.max-file-size=1GB
spring.servlet.multipart.max-request-size=1GB
# Logging
logging.level.de.w665.sharepulse=DEBUG
# Database
rethinkdb.host=localhost
rethinkdb.port=28015
@ -24,4 +23,7 @@ server.port=80
spring.application.name=sharepulse
# Spring profiles (Options: development, production) (Controls cors)
spring.profiles.active=development
spring.profiles.active=development
# Logging
logging.level.de.w665.sharepulse=DEBUG