content: package net.alanbinu.springboot.fileuploaddownload.service;
  
  import java.io.IOException;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.util.StringUtils;
  import org.springframework.web.multipart.MultipartFile;
  
  import net.alanbinu.springboot.fileuploaddownload.exception.FileNotFoundException;
  import net.alanbinu.springboot.fileuploaddownload.exception.FileStorageException;
  import net.alanbinu.springboot.fileuploaddownload.model.DatabaseFile;
  import net.alanbinu.springboot.fileuploaddownload.repository.DatabaseFileRepository;
  
  @Service
  public class DatabaseFileService {
  
      @Autowired
      private DatabaseFileRepository dbFileRepository;
  
      public DatabaseFile storeFile(MultipartFile file) {
          // Normalize file name
          String fileName = StringUtils.cleanPath(file.getOriginalFilename());
  
          try {
              // Check if the file's name contains invalid characters
              if(fileName.contains("..")) {
                  throw new FileStorageException("Sorry! Filename contains invalid path sequence " + fileName);
              }
  
              DatabaseFile dbFile = new DatabaseFile(fileName, file.getContentType(), file.getBytes());
  
              return dbFileRepository.save(dbFile);
          } catch (IOException ex) {
              throw new FileStorageException("Could not store file " + fileName + ". Please try again!", ex);
          }
      }
  
      public DatabaseFile getFile(String fileId) {
          return dbFileRepository.findById(fileId)
                  .orElseThrow(() -> new FileNotFoundException("File not found with id " + fileId));
      }
  }
  