Ignore:
Timestamp:
06/20/24 11:57:13 (11 days ago)
Author:
223021 <daniel.ilievski.2@…>
Branches:
main
Children:
0f0add0
Parents:
befb988
Message:

Did more refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/impl/ApplicationServiceImpl.java

    rbefb988 r08f82ec  
    4141    public ApplicationServiceImpl(@Value("${file.upload-dir}") String uploadDir, UserRepository userRepository, ApplicationRepository applicationRepository, JobAdvertisementRepository jobAdvertisementRepository,
    4242                                  JobSeekerRepository jobSeekerRepository) {
    43         this.fileStorageLocation = Paths.get(uploadDir).toAbsolutePath().normalize();
     43        this.fileStorageLocation = Paths.get(uploadDir + "/applications").toAbsolutePath().normalize();
    4444
    4545        try {
     
    5858    public ApplicationDetailsDTO submitApplication(ApplicationDTO applicationDTO) {
    5959
     60        JobSeeker jobSeeker = jobSeekerRepository.findById(applicationDTO.getJobSeekerId())
     61                .orElseThrow(() -> new IllegalArgumentException("User not found."));
     62        JobAdvertisement jobAdvertisement = jobAdvertisementRepository.findById(applicationDTO.getJobAdId())
     63                .orElseThrow(() -> new IllegalArgumentException("Job advertisement not found."));
     64
    6065        if (applicationDTO.getResumeFile().isEmpty()) {
    6166            throw new RuntimeException("Failed to store empty file.");
    6267        }
    63 
    64         Path targetLocation = this.fileStorageLocation.resolve(applicationDTO.getResumeFile().getOriginalFilename());
    65         try {
    66             Files.copy(applicationDTO.getResumeFile().getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
    67         } catch (IOException e) {
    68             throw new RuntimeException(e);
    69         }
    70 
    71         JobSeeker jobSeeker = jobSeekerRepository.findById(applicationDTO.getJobSeekerId()).orElseThrow(() -> new IllegalArgumentException("User not found"));
    72         JobAdvertisement jobAdvertisement = jobAdvertisementRepository.findById(applicationDTO.getJobAdId()).orElseThrow(() -> new IllegalArgumentException("Job Ad not found"));
    7368
    7469        List<String> answers = new ArrayList<>();
     
    7772        answers.add(applicationDTO.getAnswerThree());
    7873
    79         Application application = new Application(jobSeeker, jobAdvertisement, applicationDTO.getResumeFile().getOriginalFilename(), answers, applicationDTO.getMessageToRecruiter());
    80         applicationRepository.save(application);
     74        Application application = new Application(jobSeeker, jobAdvertisement,
     75                answers,applicationDTO.getMessageToRecruiter());
     76        application = applicationRepository.save(application);
     77
     78        Path filePath = this.fileStorageLocation.resolve(String.valueOf(application.getId())).resolve("resume");
     79        Path targetLocation = filePath.resolve(applicationDTO.getResumeFile().getOriginalFilename());
     80
     81        try {
     82            Files.createDirectories(filePath);
     83            Files.copy(applicationDTO.getResumeFile().getInputStream(), targetLocation);
     84        } catch (IOException e) {
     85            throw new RuntimeException(e);
     86        }
     87
     88        String relativePath = Paths.get("uploads","applications",String.valueOf(application.getId()),
     89                "resume", applicationDTO.getResumeFile().getOriginalFilename()).toString();
     90        application.setResumeFilePath(relativePath);
     91        application = applicationRepository.save(application);
     92
    8193        return Application.mapToApplicationDetailsDTO(application);
    8294    }
     
    95107
    96108    @Override
    97     public Resource loadResumeAsResource(String fileName) {
     109    public Resource loadResumeAsResource(Long applicationId) {
     110        Application application = applicationRepository.findById(applicationId).
     111                orElseThrow(() -> new IllegalArgumentException("Application not found"));
     112
     113        String relativeFilePath = application.getResumeFilePath();
     114        Path filePath = fileStorageLocation.getParent().getParent().resolve(relativeFilePath).normalize();
     115
    98116        try {
    99             Path filePath = fileStorageLocation.resolve(fileName).normalize();
    100117            Resource resource = new UrlResource(filePath.toUri());
    101118            if (resource.exists()) {
    102119                return resource;
    103120            } else {
    104                 throw new RuntimeException("File not found " + fileName);
     121                throw new RuntimeException("File not found: " + relativeFilePath);
    105122            }
    106123        } catch (IOException ex) {
    107             throw new RuntimeException("File not found " + fileName, ex);
     124            throw new RuntimeException("File path is invalid: " + relativeFilePath, ex);
    108125        }
    109126    }
Note: See TracChangeset for help on using the changeset viewer.