Storage System #92
@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					package ovh.herisson.Clyde.EndPoints;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.http.HttpHeaders;
 | 
				
			||||||
 | 
					import org.springframework.http.HttpStatus;
 | 
				
			||||||
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.*;
 | 
				
			||||||
 | 
					import org.springframework.web.multipart.MultipartFile;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Services.StorageService;
 | 
				
			||||||
 | 
					import org.springframework.core.io.Resource;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Tables.FileType;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@RestController
 | 
				
			||||||
 | 
					@CrossOrigin(origins = "http://localhost:5173")
 | 
				
			||||||
 | 
					public class StorageController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private final StorageService storageServ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public StorageController(StorageService storageServ){
 | 
				
			||||||
 | 
					        this.storageServ= storageServ;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @PostMapping("/upload/{fileType}")
 | 
				
			||||||
 | 
					    public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file, @PathVariable FileType fileType) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String path = storageServ.store(file,fileType);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (path == null) return new ResponseEntity<>("issue with the file storage", HttpStatus.BAD_REQUEST);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return  new ResponseEntity<>(path, HttpStatus.OK);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -31,7 +31,7 @@ public class UserController {
 | 
				
			|||||||
        return new ResponseEntity<>(user, HttpStatus.OK);
 | 
					        return new ResponseEntity<>(user, HttpStatus.OK);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @PostMapping("/user")
 | 
					    @PostMapping("/user") //todo check role
 | 
				
			||||||
    public ResponseEntity<String> postUser(@RequestBody User user){
 | 
					    public ResponseEntity<String> postUser(@RequestBody User user){
 | 
				
			||||||
        userService.save(user);
 | 
					        userService.save(user);
 | 
				
			||||||
        return new ResponseEntity<String>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED);
 | 
					        return new ResponseEntity<String>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED);
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					package ovh.herisson.Clyde.Repositories;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.data.repository.CrudRepository;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Tables.StorageFile;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public interface FileRepository extends CrudRepository<StorageFile,Long> {
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,56 @@
 | 
				
			|||||||
 | 
					package ovh.herisson.Clyde.Services;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.core.io.UrlResource;
 | 
				
			||||||
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
 | 
					import org.springframework.web.multipart.MultipartFile;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Repositories.FileRepository;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Tables.FileType;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Tables.StorageFile;
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import org.springframework.core.io.Resource;
 | 
				
			||||||
 | 
					import java.nio.file.Files;
 | 
				
			||||||
 | 
					import java.nio.file.Path;
 | 
				
			||||||
 | 
					import java.nio.file.Paths;
 | 
				
			||||||
 | 
					import java.nio.file.StandardCopyOption;
 | 
				
			||||||
 | 
					import java.util.Objects;
 | 
				
			||||||
 | 
					import java.util.UUID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Service
 | 
				
			||||||
 | 
					public class StorageService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private final Path rootLocation = Paths.get("cdn/");
 | 
				
			||||||
 | 
					    private final FileRepository fileRepo;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public StorageService(FileRepository filerepo){
 | 
				
			||||||
 | 
					        this.fileRepo = filerepo;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public String store(MultipartFile file, FileType fileType) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (file.getOriginalFilename().isEmpty()){return null;}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        UUID uuid = UUID.randomUUID();
 | 
				
			||||||
 | 
					        String stringUuid = uuid + "." + file.getOriginalFilename().split("\\.",2)[1];
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            if (file.isEmpty()) {
 | 
				
			||||||
 | 
					                return null;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            Path destinationFile = this.rootLocation.resolve(Paths.get(stringUuid));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            Files.copy(file.getInputStream(), destinationFile,StandardCopyOption.REPLACE_EXISTING);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        catch (IOException e) {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String url = this.rootLocation.resolve(Paths.get(Objects.requireNonNull(stringUuid)))
 | 
				
			||||||
 | 
					                .normalize().toString();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        fileRepo.save(new StorageFile(file.getName(),url, fileType));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return url;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,8 @@
 | 
				
			|||||||
 | 
					package ovh.herisson.Clyde.Tables;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public enum FileType {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ProfilePicture,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    EducationCertificate
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,63 @@
 | 
				
			|||||||
 | 
					package ovh.herisson.Clyde.Tables;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import jakarta.persistence.Entity;
 | 
				
			||||||
 | 
					import jakarta.persistence.GeneratedValue;
 | 
				
			||||||
 | 
					import jakarta.persistence.GenerationType;
 | 
				
			||||||
 | 
					import jakarta.persistence.Id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Entity
 | 
				
			||||||
 | 
					public class StorageFile {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Id
 | 
				
			||||||
 | 
					    @GeneratedValue(strategy = GenerationType.AUTO)
 | 
				
			||||||
 | 
					    private Long id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private String name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private String url;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private FileType fileType;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public StorageFile(String name, String url, FileType fileType){
 | 
				
			||||||
 | 
					        this.name = name;
 | 
				
			||||||
 | 
					        this.url = url;
 | 
				
			||||||
 | 
					        this.fileType = fileType;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public StorageFile(){}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void setId(Long id) {
 | 
				
			||||||
 | 
					        this.id = id;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Long getId() {
 | 
				
			||||||
 | 
					        return id;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public String getName() {
 | 
				
			||||||
 | 
					        return name;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void setName(String name) {
 | 
				
			||||||
 | 
					        this.name = name;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public String getUrl() {
 | 
				
			||||||
 | 
					        return url;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void setUrl(String url) {
 | 
				
			||||||
 | 
					        this.url = url;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public FileType getFileType() {
 | 
				
			||||||
 | 
					        return fileType;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void setFileType(FileType fileType) {
 | 
				
			||||||
 | 
					        this.fileType = fileType;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -23,7 +23,9 @@ public class User {
 | 
				
			|||||||
    private String profilePictureUrl;
 | 
					    private String profilePictureUrl;
 | 
				
			||||||
    private ovh.herisson.Clyde.Tables.Role role;
 | 
					    private ovh.herisson.Clyde.Tables.Role role;
 | 
				
			||||||
    private String password;
 | 
					    private String password;
 | 
				
			||||||
    public User(String lastName, String firstName, String email, String address, String country, Date birthDate, String profilePictureUrl, Role role, String password){
 | 
					    public User(String lastName, String firstName, String email, String address,
 | 
				
			||||||
 | 
					                String country, Date birthDate, String profilePictureUrl, Role role, String password)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
        this.lastName = lastName;
 | 
					        this.lastName = lastName;
 | 
				
			||||||
        this.firstName = firstName;
 | 
					        this.firstName = firstName;
 | 
				
			||||||
        this.email = email;
 | 
					        this.email = email;
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user