fixed not getting the file description and a few minor changes

This commit is contained in:
Rostislav Raykov
2024-10-07 00:45:09 +03:00
parent 07e47d23f6
commit 4144f65f53
4 changed files with 20 additions and 29 deletions

View File

@@ -21,7 +21,9 @@ public class FileRestController {
@PostMapping("/upload")
public ResponseEntity<FileEntity> saveFile(@RequestParam("file") MultipartFile file,
FileUploadRequest fileUploadRequest) {
@RequestParam(value = "description") String description,
@RequestParam(value = "keepIndefinitely", defaultValue = "false") boolean keepIndefinitely) {
FileUploadRequest fileUploadRequest = new FileUploadRequest(description, keepIndefinitely);
FileEntity fileEntity = fileService.saveFile(file, fileUploadRequest);
if (fileEntity != null) {
return ResponseEntity.ok(fileEntity);

View File

@@ -2,15 +2,16 @@ package org.rostislav.quickdrop.controller;
import jakarta.servlet.http.HttpServletRequest;
import org.rostislav.quickdrop.model.FileEntity;
import org.rostislav.quickdrop.model.FileUploadRequest;
import org.rostislav.quickdrop.service.FileService;
import org.rostislav.quickdrop.util.FileUtils;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@@ -30,22 +31,6 @@ public class FileViewController {
return "upload";
}
@PostMapping("/upload")
public String saveFile(@RequestParam("file") MultipartFile file,
@RequestParam("description") String description,
@RequestParam(value = "keepIndefinitely", defaultValue = "false") boolean keepIndefinitely,
Model model, HttpServletRequest request) {
FileUploadRequest fileUploadRequest = new FileUploadRequest(description, keepIndefinitely);
FileEntity fileEntity = fileService.saveFile(file, fileUploadRequest);
if (fileEntity != null) {
model.addAttribute("downloadLink", getDownloadLink(request, fileEntity));
model.addAttribute("file", fileEntity);
return "fileUploaded";
}
return "upload";
}
@GetMapping("/list")
public String listFiles(Model model) {
List<FileEntity> files = fileService.getFiles();
@@ -63,6 +48,16 @@ public class FileViewController {
return "fileView";
}
@GetMapping("/uploaded/{uuid}")
public String uploadedFile(@PathVariable String uuid, Model model, HttpServletRequest request) {
FileEntity fileEntity = fileService.getFile(uuid);
model.addAttribute("file", fileEntity);
model.addAttribute("fileSize", FileUtils.formatFileSize(fileEntity.size));
model.addAttribute("downloadLink", getDownloadLink(request, fileEntity));
return "fileUploaded";
}
@GetMapping("/download/{id}")
public ResponseEntity<Resource> downloadFile(@PathVariable Long id) {
return fileService.downloadFile(id);

View File

@@ -9,7 +9,7 @@ document.getElementById("uploadForm").addEventListener("submit", function (event
// Create an AJAX request
const xhr = new XMLHttpRequest();
xhr.open("POST", "/api/file/upload", true); // Updated to use the new API endpoint
xhr.open("POST", "/api/file/upload", true);
// Add CSRF token if required
const csrfTokenElement = document.querySelector('input[name="_csrf"]');
@@ -23,7 +23,7 @@ document.getElementById("uploadForm").addEventListener("submit", function (event
const response = JSON.parse(xhr.responseText);
if (response.uuid) {
// Redirect to the view page using the UUID from the JSON response
window.location.href = "/file/" + response.uuid;
window.location.href = "/file/uploaded/" + response.uuid;
} else {
alert("Unexpected response. Please try again.");
document.getElementById("uploadIndicator").style.display = "none";

View File

@@ -38,14 +38,8 @@
<h3>File Size</h3>
<p th:text="${fileSize}"></p>
<h3>Renew file lifetime</h3>
<form method="post" th:action="@{/file/extend/{id}(id=${file.id})}">
<input th:name="${_csrf.parameterName}" th:value="${_csrf.token}" type="hidden"/>
<button class="btn btn-primary" type="submit">Extend</button>
</form>
<h3>Link</h3>
<p th:text="${downloadLink}"></p>
<a th:href="@{${downloadLink}}"><p th:text="${downloadLink}"></p></a>
<h3>Download</h3>
<p>