Added JSON response to Upload endpoint

This commit is contained in:
Max W. 2024-02-15 21:51:57 +01:00
parent dbc72a3dc4
commit 3ebac8cf67
4 changed files with 18 additions and 6 deletions

View File

@ -26,7 +26,7 @@ export class UploadComponent {
singleDownload: boolean = false;
fileDescription: string = '';
passwordProtected: boolean = false;
password: string = 'nigga123';
password: string = ''; // Generated by the server for only this file
uploadStarted: boolean = false;
uploadProgress = 0; // Real progress
@ -95,7 +95,8 @@ export class UploadComponent {
axios.post(this.developmentStore.getBaseUrl() + 'api/v1/upload', formData, config)
.then(response => {
console.log('Upload completed successfully:', response.data);
console.log('Upload completed successfully!');
console.log(response.data);
this.uploadFinished = true;
})
.catch(error => {

View File

@ -13,6 +13,7 @@ export class DevelopmentStore {
constructor() {
this.isDevelopment = location.port === '4200';
console.log("Development mode enabled: " + this.isDevelopment);
}
getIsDevelopment(): boolean {

View File

@ -1,5 +1,7 @@
package de.w665.sharepulse.rest.mappings;
import com.google.gson.JsonObject;
import de.w665.sharepulse.model.FileUpload;
import de.w665.sharepulse.rest.ApiRestController;
import de.w665.sharepulse.service.FileService;
import jakarta.servlet.http.HttpServletRequest;
@ -38,8 +40,16 @@ public class Upload extends ApiRestController {
return new ResponseEntity<>("Please select a file to upload.", HttpStatus.NOT_ACCEPTABLE);
}
String fileId = fileService.processUploadedFile(file, request.getRemoteAddr(), password, singleDownload, fileDescription);
FileUpload fileUpload = fileService.processUploadedFile(file, request.getRemoteAddr(), password, singleDownload, fileDescription);
log.debug("User uploaded file " + file.getOriginalFilename() + " from IP " + request.getRemoteAddr() + " successfully.");
return new ResponseEntity<>("File " + file.getOriginalFilename() + " uploaded successfully! ID(" + fileId + ")", HttpStatus.OK);
JsonObject response = new JsonObject();
response.addProperty("fileId", fileUpload.getFileId());
response.addProperty("message", "File " + file.getOriginalFilename() + " uploaded successfully!");
response.addProperty("passwordProtected", fileUpload.isPasswordProtected());
response.addProperty("singleDownload", fileUpload.isSingleDownload());
response.addProperty("uploadDate", fileUpload.getUploadDate().toString());
return new ResponseEntity<>(response.toString(), HttpStatus.OK);
}
}

View File

@ -53,7 +53,7 @@ public class FileService {
}
}
public String processUploadedFile(MultipartFile file, String uploaderIp, String password, boolean singleDownload, String fileDescription) {
public FileUpload processUploadedFile(MultipartFile file, String uploaderIp, String password, boolean singleDownload, String fileDescription) {
String fileId = fileIdService.generateNewUniqueId();
@ -80,7 +80,7 @@ public class FileService {
try {
Files.write(path, file.getBytes());
log.debug("File " + file.getOriginalFilename() + " written to " + path.getFileName());
return fileId;
return fileUpload;
} catch (IOException e) {
e.printStackTrace();
return null;