Skip to content

Commit

Permalink
Move ROOT to the file upload controller class
Browse files Browse the repository at this point in the history
  • Loading branch information
gregturn committed Jun 27, 2016
1 parent bc402ee commit 5ce0618
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 2 additions & 4 deletions complete/src/main/java/hello/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@
@SpringBootApplication
public class Application {

public static String ROOT = "upload-dir";

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
CommandLineRunner init() {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(ROOT));
FileSystemUtils.deleteRecursively(new File(FileUploadController.ROOT));

Files.createDirectory(Paths.get(ROOT));
Files.createDirectory(Paths.get(FileUploadController.ROOT));
};
}
}
12 changes: 7 additions & 5 deletions complete/src/main/java/hello/FileUploadController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class FileUploadController {

private static final Logger log = LoggerFactory.getLogger(FileUploadController.class);

public static String ROOT = "upload-dir";

private final ResourceLoader resourceLoader;

@Autowired
Expand All @@ -39,9 +41,9 @@ public FileUploadController(ResourceLoader resourceLoader) {
@RequestMapping(method = RequestMethod.GET, value = "/")
public String provideUploadInfo(Model model) throws IOException {

model.addAttribute("files", Files.walk(Paths.get(Application.ROOT))
.filter(path -> !path.equals(Paths.get(Application.ROOT)))
.map(path -> Paths.get(Application.ROOT).relativize(path))
model.addAttribute("files", Files.walk(Paths.get(ROOT))
.filter(path -> !path.equals(Paths.get(ROOT)))
.map(path -> Paths.get(ROOT).relativize(path))
.map(path -> linkTo(methodOn(FileUploadController.class).getFile(path.toString())).withRel(path.toString()))
.collect(Collectors.toList()));

Expand All @@ -53,7 +55,7 @@ public String provideUploadInfo(Model model) throws IOException {
public ResponseEntity<?> getFile(@PathVariable String filename) {

try {
return ResponseEntity.ok(resourceLoader.getResource("file:" + Paths.get(Application.ROOT, filename).toString()));
return ResponseEntity.ok(resourceLoader.getResource("file:" + Paths.get(ROOT, filename).toString()));
} catch (Exception e) {
return ResponseEntity.notFound().build();
}
Expand All @@ -65,7 +67,7 @@ public String handleFileUpload(@RequestParam("file") MultipartFile file,

if (!file.isEmpty()) {
try {
Files.copy(file.getInputStream(), Paths.get(Application.ROOT, file.getOriginalFilename()));
Files.copy(file.getInputStream(), Paths.get(ROOT, file.getOriginalFilename()));
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
} catch (IOException|RuntimeException e) {
Expand Down

0 comments on commit 5ce0618

Please sign in to comment.