Updated Upload error

- Added header expose
- Updated download UI logic
This commit is contained in:
Max W. 2024-02-17 20:10:03 +01:00
parent 66d33e7e56
commit c084af461f
5 changed files with 81 additions and 6 deletions

View File

@ -9,7 +9,7 @@
type="password" class="input input-bordered text-center w-full max-w-md mb-6" placeholder="Enter file password..." [(ngModel)]="filePassword"/>
<button class="btn btn-primary w-full max-w-md flex justify-center items-center mb-4" (click)="checkFile()">
<button class="btn btn-primary w-full max-w-md flex justify-center items-center mb-4" (click)="requestDownload()">
Download
</button>
<button

View File

@ -23,12 +23,14 @@ export class DownloadComponent {
filePassword: string = "";
downloadInfo: DownloadInfo | null = null;
waitingForPassword: boolean = false;
constructor(private developmentStore: DevelopmentStore) {
this.speedTest();
}
checkFile() {
requestDownload() {
console.log(this.inputFileId);
this.fileId = this.inputFileId; // TODO: Implement link extraction logic
@ -36,8 +38,19 @@ export class DownloadComponent {
this.getDownloadInfo();
}
checkPassword() {
processDownloadInfo() {
if(!this.downloadInfo?.passwordProtected && this.downloadInfo?.downloadable) {
console.log("Proceeding with download");
this.downloadFile();
return;
}
else if(!this.downloadInfo?.downloadable) {
this.download_not_possible?.nativeElement.showModal();
return;
}
else if(this.downloadInfo?.passwordProtected) {
console.log("Password protected");
}
}
private speedTest() {
@ -77,12 +90,58 @@ export class DownloadComponent {
.then(response => {
this.downloadInfo = response.data;
console.log(response.data);
return response.data;
this.processDownloadInfo();
})
.catch(error => {
console.error('Error during download info request:', error);
});
}
private downloadFile() {
axios({
method: 'get',
url: this.developmentStore.getBaseUrl() + 'api/v1/download?fileId=' + this.fileId,
responseType: 'arraybuffer',
headers: {
'Access-Control-Allow-Origin': '*', // Allow CORS
},
onDownloadProgress: function(progressEvent) {
// Calculate the percentage of download completed
if(progressEvent.total) {
const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
console.log(percentCompleted + '%'); // Log the percentage or update any progress UI component
}
}
})
.then(response => {
const contentDisposition = response.headers['content-disposition'];
let filename = "default_filename"; // Default filename in case parsing fails
if (contentDisposition) {
const filenameRegex = /filename="?([^"]+)"?/;
const matches = contentDisposition.match(filenameRegex);
if (matches && matches[1]) {
filename = matches[1];
}
}
const blob = new Blob([response.data], {type: 'application/octet-stream'});
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = filename; // You can specify a filename here
document.body.appendChild(a);
a.click();
// Clean up by revoking the Blob URL and removing the temporary anchor
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
})
.catch(error => {
console.error('Error during download request:', error);
});
}
}
interface DownloadInfo {
downloadable: boolean;

View File

@ -68,6 +68,7 @@ public class Download extends ApiRestController {
headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
headers.add(HttpHeaders.PRAGMA, "no-cache");
headers.add(HttpHeaders.EXPIRES, "0");
headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition");
return ResponseEntity.ok()
.headers(headers)

View File

@ -45,6 +45,14 @@ public class Upload extends ApiRestController {
}
FileUpload fileUpload = fileService.processUploadedFile(file, request.getRemoteAddr(), passwordProtected, singleDownload, fileDescription);
if(fileUpload == null) {
log.debug("File Upload failed for IP: " + request.getRemoteAddr());
Map<String, Object> response = new HashMap<>();
response.put("message", "FileUpload failed. FileName might have invalid characters.");
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
log.debug("User uploaded file " + file.getOriginalFilename() + " from IP " + request.getRemoteAddr() + " successfully.");
Map<String, Object> response = new HashMap<>();

View File

@ -3,7 +3,6 @@ package de.w665.sharepulse.service;
import de.w665.sharepulse.db.repo.FileUploadRepository;
import de.w665.sharepulse.model.FileUpload;
import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -71,6 +70,14 @@ public class FileService {
password = fileSecurityService.generateFilePassword();
}
/*String encodedFileName = "";
try {
encodedFileName = URLEncoder.encode(file.getOriginalFilename(), StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
log.error("Error encoding file name: " + e.getMessage());
return null;
}*/
FileUpload fileUpload = FileUpload.builder()
.fileId(fileId)
.fileName(file.getOriginalFilename())