Skip to content
Snippets Groups Projects
Commit 4dae3bc1 authored by Joshua Gill's avatar Joshua Gill
Browse files

Merge remote-tracking branch 'origin/develop' into develop

parents b7874e08 722b9cfc
No related branches found
No related tags found
2 merge requests!114LoggingService service class, new method to add a log to the "Logs" table when...,!111fixed superadmin issue
Showing
with 68 additions and 0 deletions
package com.example.clientproject.service.Utils;
import com.example.clientproject.data.shops.ShopsRepo;
import com.example.clientproject.data.users.UsersRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Component
public class RemoveRedundantFiles {
@Autowired
private ShopsRepo shopRepo;
@Autowired
private UsersRepo userRepo;
private final String dir = "./src/main/resources/static/imgs/uploaded/";
private Set<String> getFilesInDir(){
return Stream.of(new File(dir).listFiles())
.filter(file -> !file.isDirectory())
.map(File::getName)
.collect(Collectors.toSet());
}
private Set<String> getFilesInDb(){
Set<String> curFiles = new HashSet<>();
shopRepo.findAll().forEach(x -> {curFiles.add(x.getShopImage());
curFiles.add(x.getShopBanner());});
userRepo.findAll().forEach(x -> curFiles.add(x.getUserProfilePicture()));
return curFiles;
}
private Set<String> getDeletableFiles(){
Set<String> deletableFiles = getFilesInDir();
deletableFiles.removeAll(getFilesInDb());
return deletableFiles;
}
public void deleteFiles(){
for(String fileName:getDeletableFiles()) {
File file = new File(dir + fileName);
if(file.delete()){
System.out.println("Deleted file " + fileName);
}else{
System.out.println("Failed to delete " + fileName);
}
}
}
}
package com.example.clientproject.web.restControllers; package com.example.clientproject.web.restControllers;
import com.example.clientproject.exceptions.ForbiddenErrorException; import com.example.clientproject.exceptions.ForbiddenErrorException;
import com.example.clientproject.service.Utils.RemoveRedundantFiles;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
...@@ -16,10 +17,19 @@ import java.util.UUID; ...@@ -16,10 +17,19 @@ import java.util.UUID;
@RestController @RestController
public class FileUpload { public class FileUpload {
private RemoveRedundantFiles removeFiles;
public FileUpload(RemoveRedundantFiles removeFile){
removeFiles = removeFile;
}
String UPLOAD_FOLDER = "./src/main/resources/static/imgs/uploaded/"; String UPLOAD_FOLDER = "./src/main/resources/static/imgs/uploaded/";
@PostMapping("/upload") // //new annotation since 4.3 @PostMapping("/upload") // //new annotation since 4.3
public String singleFileUpload(@RequestParam("file") MultipartFile file) { public String singleFileUpload(@RequestParam("file") MultipartFile file) {
removeFiles.deleteFiles();
if (file.isEmpty()) { if (file.isEmpty()) {
throw new ForbiddenErrorException("No file"); throw new ForbiddenErrorException("No file");
} }
......
src/main/resources/static/imgs/uploaded/012d7d87_1906_4f1d_949c_e8a5814588a2.png

47.1 KiB

src/main/resources/static/imgs/uploaded/02b3324f_113c_4c98_8ad6_7f1cf28f74c9.jpg

6.35 KiB

src/main/resources/static/imgs/uploaded/0998cd76_0b99_40de_8ed0_3c8a6f0fa097.png

35.3 KiB

src/main/resources/static/imgs/uploaded/0a18d08f_3bb2_4903_b1c7_d81246373cf1.png

70.3 KiB

src/main/resources/static/imgs/uploaded/12d8340a_95a8_4454_ba7c_464d89f5bed1.png

47.1 KiB

src/main/resources/static/imgs/uploaded/1318622a_7d76_410a_ad6e_035cff865b2c.png

48.1 KiB

src/main/resources/static/imgs/uploaded/1eab1fb2_7744_4eb6_9505_8a1e9b59981b.jpg

6.35 KiB

src/main/resources/static/imgs/uploaded/3671547d_dbcf_4f1c_b3de_1c299e02acbf.jpg

6.35 KiB

src/main/resources/static/imgs/uploaded/43f67c70_c2cc_4dce_81c3_c018e6357751.jpg

6.35 KiB

src/main/resources/static/imgs/uploaded/45f41b47_75c1_4c81_b743_cd649a58f69c.jpg

6.35 KiB

src/main/resources/static/imgs/uploaded/4603767e_1453_4174_9519_6c009ddd0b0b.jpg

6.35 KiB

src/main/resources/static/imgs/uploaded/4d017978_ae1e_4967_a254_740d30fc26df.png

47.1 KiB

src/main/resources/static/imgs/uploaded/695dc059_5ff8_465c_84c3_eb67457d5639.png

47.1 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment