Skip to content

Commit

Permalink
Merge pull request #70 from insea-connect/drives
Browse files Browse the repository at this point in the history
Drives
  • Loading branch information
AmimiHamza authored Jun 6, 2024
2 parents 65c6fc6 + 3c1a4d1 commit 340641a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import ma.insea.connect.user.DegreePath;
import ma.insea.connect.user.User;
import ma.insea.connect.utils.Functions;

import org.apache.http.protocol.HTTP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -22,8 +24,6 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


@RestController
Expand Down Expand Up @@ -71,18 +71,22 @@ public ResponseEntity<List<DriveItemDto>> getDriveItems(@PathVariable Long degre
driveItemDto.setParent(null);

if(driveItem instanceof Folder) {driveItemDto.setFolder(true);}
if(driveItem instanceof File) {
driveItemDto.setFolder(false);
driveItemDto.setSize(((File) driveItem).getSize());
driveItemDto.setMimeType(((File) driveItem).getMimeType());
driveItemDto.setItemUrl(((File) driveItem).getFileUrl());}
driveItemDtos.add(driveItemDto);
}
return ResponseEntity.ok(driveItemDtos);
}

@PreAuthorize("hasRole('CLASS_REP')")
@PostMapping("drive/{degreePathId}/folders/{parentId}/upload")
public ResponseEntity<DriveItemDto> handleFileUpload(@RequestParam("file") MultipartFile file,@PathVariable Long degreePathId,@PathVariable Long parentId) throws Exception{
public ResponseEntity<HttpStatus> handleFileUpload(@RequestParam("file") MultipartFile file,@PathVariable Long degreePathId,@PathVariable Long parentId) throws Exception{
User user=functions.getConnectedUser();
DegreePath degreePath = degreePathRepository.findById(degreePathId).get();

DriveItemDto driveItemDto = new DriveItemDto();
DriveUserDto driveUserDto = new DriveUserDto();
File fileObj = new File();

Expand All @@ -107,24 +111,16 @@ public ResponseEntity<DriveItemDto> handleFileUpload(@RequestParam("file") Multi
driveUserDto.setEmail(user.getEmail());
driveUserDto.setId(user.getId());

driveItemDto.setItemUrl(fileUrl);
driveItemDto.setName(file.getOriginalFilename());
driveItemDto.setSize(file.getSize());
driveItemDto.setMimeType(file.getContentType());
driveItemDto.setCreatedAt(LocalDateTime.now());
driveItemDto.setCreator(driveUserDto);
driveItemDto.setDegreePath(degreePath);
if(parentId != 0) {
Folder folder = folderService.getFolderById(parentId);
if(folder == null) {return ResponseEntity.notFound().build();}
fileObj.setParent(folder);
}else{
fileObj.setParent(null);
driveItemDto.setParent(null);
}
fileRepository.save(fileObj);

return ResponseEntity.ok(driveItemDto);
return new ResponseEntity(HttpStatus.CREATED);

}
@GetMapping("/degreePaths")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import ma.insea.connect.drive.model.File;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface FileRepository extends JpaRepository<File, Long>{
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@Component
@RequiredArgsConstructor
public class Functions {
private static final String UPLOAD_DIR = "uploads";
private static final String UPLOAD_DIR = "packages\\insea-connect-backend\\src\\main\\resources\\static\\uploads";

private final UserRepository userRepository;
public User getConnectedUser() {
Expand Down Expand Up @@ -47,7 +47,7 @@ public String uploadFile(MultipartFile file)throws IOException{
Files.copy(file.getInputStream(), filePath);

// Return the path of the saved file
return filePath.toString();
return "uploads/" + fileName;
}

public boolean checkPermission(User user, DegreePath degreePath) {
Expand Down

0 comments on commit 340641a

Please sign in to comment.