Class FileSystemStorageService

java.lang.Object
org.ntnu.idi.idatt2106.sparesti.sparestibackend.service.FileSystemStorageService

@Service public class FileSystemStorageService extends Object
Service responsible for uploading and getting images for users, challenges and goals.
Since:
1.5.24
Version:
1.0
Author:
L.M.L. Nilsen
  • Constructor Details

    • FileSystemStorageService

      public FileSystemStorageService(UserService userService, ChallengeRepository challengeRepository, GoalRepository goalRepository, StorageProperties properties)
      Constructor for FileSystemStorageService
      Parameters:
      userService - User service for communicating with users
      challengeRepository - Challenge repo for finding challenges
      goalRepository - Goal repo for finding goals
      properties - Storage properties
  • Method Details

    • save

      public void save(org.springframework.web.multipart.MultipartFile file, String identifier, org.springframework.security.core.userdetails.UserDetails userDetails) throws IOException
      Save a valid image for a user, challenge or goal depending on the filename. If the filename has the format {username}-P.png the image belongs to a user, {goalId}-G.png for goals and {challengeId}-C.png for a challenge. The valid filetypes are png, jpg, jpeg and gif.
      Parameters:
      file - The image file that is getting saved.
      identifier - The filename without the extension.
      userDetails - UserDetails for user who wants to save image.
      Throws:
      IOException - If the file is empty.
      StorageException - If the file type is unsupported, or the filename format is wrong.
    • getImage

      public org.springframework.core.io.Resource getImage(String baseFilename, org.springframework.security.core.userdetails.UserDetails userDetails)
      Gets an image for a user, challenge or goal.
      Parameters:
      baseFilename - The name of the file, without the file extension.
      userDetails - The UserDetails of the user wanting to get an image.
      Returns:
      Returns the file as a Resource.
      Throws:
      StorageException - If the challenge or goal does not belong to the user.