Commit 303fdc72084a5d579f26697d7d7e0bdd2cc1cff5

Authored by Antoine Duquenoy
1 parent 66a8b43c

Sauvegarde des fichiers envoyés

Les fichiers envoyés depuis l'interface sont stockés dans le dossier files/<pseudo>_<timestamp>
PFE06/files/admin_1539804173965/arch-tux.jpg 0 → 100644

19.4 KB

PFE06/src/main/java/com/PFE/ServerManager/MainController.java
... ... @@ -3,19 +3,20 @@ package com.PFE.ServerManager;
3 3 import org.springframework.beans.factory.annotation.Autowired;
4 4 import org.springframework.http.HttpStatus;
5 5 import org.springframework.stereotype.Controller;
  6 +import org.springframework.ui.ModelMap;
6 7 import org.springframework.web.bind.annotation.*;
  8 +import org.springframework.web.multipart.MultipartFile;
7 9 import org.springframework.web.servlet.ModelAndView;
8 10  
  11 +import java.io.*;
  12 +import java.sql.Timestamp;
9 13 import java.util.Arrays;
10 14 import java.util.HashSet;
11 15 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
12 16 import org.springframework.security.core.context.SecurityContextHolder;
13 17 import org.springframework.security.core.Authentication;
14   -import org.springframework.web.multipart.MultipartFile;
15   -import org.springframework.http.ResponseEntity;
  18 +import org.springframework.web.context.request.RequestContextHolder;
16 19  
17   -import javax.jws.WebParam;
18   -import javax.persistence.SequenceGenerator;
19 20 import javax.servlet.annotation.MultipartConfig;
20 21  
21 22 @Controller
... ... @@ -86,11 +87,52 @@ public class MainController {
86 87 return modelAndView;
87 88 }
88 89  
89   - @PostMapping(path="/file")
90   - public ResponseEntity<?> addFile(@RequestParam("file") MultipartFile uploadedFile, @RequestParam String filename, @RequestParam String nodes) {
91   - return new ResponseEntity<>(HttpStatus.OK);
  90 + @RequestMapping(value = "/file", method = RequestMethod.POST)
  91 + @ResponseStatus(value = HttpStatus.OK)
  92 + public void submit(
  93 + @RequestParam MultipartFile file, @RequestParam String nodes,
  94 + @RequestParam String filename) {
  95 +
  96 +
  97 + Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  98 + Customer customer = customerRepository.findByPseudo(auth.getName());
  99 + Timestamp timestamp = new Timestamp(System.currentTimeMillis());
  100 + File dirs = new File("files/" + customer.getPseudo() + "_" + timestamp.getTime());
  101 + dirs.mkdirs();
  102 + OutputStream outputStream = null;
  103 + InputStream inputStream = null;
  104 +
  105 + try {
  106 + inputStream = file.getInputStream();
  107 + File newFile = new File(dirs.getPath() + "/" + file.getOriginalFilename());
  108 + if (!newFile.exists()) {
  109 + newFile.createNewFile();
  110 + }
  111 + outputStream = new FileOutputStream(newFile);
  112 + int read = 0;
  113 + byte[] bytes = new byte[1024];
  114 +
  115 + while((read = inputStream.read(bytes)) != -1) {
  116 + outputStream.write(bytes, 0, read);
  117 + }
  118 + }
  119 +
  120 + catch (IOException e) {
  121 + e.printStackTrace();
  122 + }
  123 +
  124 + finally {
  125 + try {
  126 + outputStream.close();
  127 + }
  128 + catch(IOException e) {
  129 +
  130 + }
  131 + }
  132 +
92 133 }
93 134  
  135 +
94 136 @GetMapping(path="/login")
95 137 public ModelAndView login() {
96 138 ModelAndView modelAndView = new ModelAndView();
... ...
PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java
... ... @@ -52,7 +52,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
52 52 .logoutSuccessUrl("/login")
53 53 .and()
54 54 .exceptionHandling()
55   - .accessDeniedPage("/denied");
  55 + .accessDeniedPage("/denied")
  56 + .and()
  57 + .csrf().disable();
56 58 }
57 59  
58 60 }
59 61 \ No newline at end of file
... ...
PFE06/src/main/resources/static/js/home.js
... ... @@ -107,10 +107,14 @@ $(document).ready(function() {
107 107 $("#warningFilesNumber").modal()
108 108 }
109 109  
  110 + request.onreadystatechange = function() {
  111 + if(this.readyState === XMLHttpRequest.DONE && this.status === 200) {
  112 + formData = new FormData();
  113 + }
  114 + }
  115 +
110 116 request.send(formData);
111 117  
112   - delete formData;
113   - formData = new FormData();
114 118 }
115 119 else {
116 120 if(readyToSend) {
... ...