Added controller for FileUpload, refactor of main package, try to add forum system.
This commit is contained in:
1 parent
cf24d07a78
commit
4a9efbb06d
38 files changed
+165
-78
No files matched your search
@@ -0,0 +1,16 @@
|
||||
package site.easystartup.web.storage.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import site.easystartup.web.storage.service.FileLocalStorageService;
|
||||
import site.easystartup.web.storage.service.StorageService;
|
||||
|
||||
@Configuration
|
||||
public class FileConfig {
|
||||
@Bean
|
||||
@Scope("singleton")
|
||||
public StorageService storageService() {
|
||||
return new FileLocalStorageService();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package site.easystartup.web.storage.controller;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
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.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
|
||||
import site.easystartup.web.storage.service.StorageFileNotFoundException;
|
||||
import site.easystartup.web.storage.service.StorageService;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
public class FileUploadController {
|
||||
|
||||
private final StorageService storageService;
|
||||
|
||||
@Autowired
|
||||
public FileUploadController(StorageService storageService) {
|
||||
this.storageService = storageService;
|
||||
}
|
||||
|
||||
@GetMapping("/upload")
|
||||
public String listUploadedFiles(Model model) {
|
||||
if(storageService.loadAll() == null) {
|
||||
model.addAttribute("message",
|
||||
"Storage is emptiy");
|
||||
return "upload";
|
||||
}
|
||||
model.addAttribute("files", storageService.loadAll().map(
|
||||
path -> MvcUriComponentsBuilder.fromMethodName(FileUploadController.class,
|
||||
"serveFile", path.getFileName().toString()).build().toUri().toString())
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
return "upload";
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@GetMapping("/files/{filename:.+}")
|
||||
@ResponseBody
|
||||
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
|
||||
Resource file = storageService.load(filename);
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"").body(file);
|
||||
}
|
||||
|
||||
@PostMapping("/upload")
|
||||
public String handleFileUpload(@RequestParam("file") MultipartFile file,
|
||||
Model model) {
|
||||
storageService.save(file);
|
||||
model.addAttribute("message",
|
||||
"You successfully uploaded " + file.getOriginalFilename() + "!");
|
||||
|
||||
return "upload";
|
||||
}
|
||||
|
||||
|
||||
@ExceptionHandler(StorageFileNotFoundException.class)
|
||||
public ResponseEntity<?> handleStorageFileNotFound(StorageFileNotFoundException exc) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package site.easystartup.web.storage.service;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class FileLocalStorageService implements StorageService {
|
||||
private final Path root = Paths.get("uploads");
|
||||
|
||||
public FileLocalStorageService() {
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
try {
|
||||
if(!root.toFile().exists())
|
||||
Files.createDirectory(root);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Could not initialize folder for upload!");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void save(MultipartFile file) {
|
||||
try {
|
||||
Files.copy(file.getInputStream(), this.root.resolve(file.getOriginalFilename()));
|
||||
} catch (Exception e) {
|
||||
System.out.println("Could not store the file. Error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void saveImage(MultipartFile file, String name) {
|
||||
try {
|
||||
Files.copy(file.getInputStream(), this.root.resolve(name + ".jpg"));
|
||||
} catch (Exception e) {
|
||||
Files.delete(this.root.resolve(name + ".jpg"));
|
||||
Files.copy(file.getInputStream(), this.root.resolve(name + ".jpg"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource load(String filename) {
|
||||
try {
|
||||
Path file = root.resolve(filename);
|
||||
Resource resource = new UrlResource(file.toUri());
|
||||
if (resource.exists() || resource.isReadable()) {
|
||||
return resource;
|
||||
} else {
|
||||
throw new RuntimeException("Could not read the file!");
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException("Error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
FileSystemUtils.deleteRecursively(root.toFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<Path> loadAll() {
|
||||
try {
|
||||
return Files.walk(this.root, 1).filter(path -> !path.equals(this.root)).map(this.root::relativize);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Could not load the files!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package site.easystartup.web.storage.service;
|
||||
|
||||
public class StorageFileNotFoundException extends RuntimeException {
|
||||
public StorageFileNotFoundException(String filename) {
|
||||
super("Could not find file with name " + filename);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package site.easystartup.web.storage.service;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface StorageService {
|
||||
void init();
|
||||
void save(MultipartFile file);
|
||||
void saveImage(MultipartFile file, String name);
|
||||
Resource load(String filename);
|
||||
void deleteAll();
|
||||
Stream<Path> loadAll();
|
||||
}
|
||||
Reference in new issue
Block a user