Class FileService

java.lang.Object
de.bytestore.hostinger.handler.FileService

@Component @Service public class FileService extends Object
The FileService class provides functionality for managing file types and their associated MIME types, as well as generating appropriate icons for those file types. This service initializes and maintains a mapping of file extensions to MIME types and determines corresponding Vaadin icons for files based on their MIME types.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addExtension(String extension, String mimeType)
    Adds a file extension and its associated MIME type to the mapping.
    com.vaadin.flow.component.icon.VaadinIcon
    getIcon(File file)
    Retrieves the corresponding VaadinIcon for the given file based on its MIME type.
    com.vaadin.flow.component.icon.VaadinIcon
    getIcon(String fileName)
    Returns a VaadinIcon corresponding to the MIME type of the provided file name.
    Determines the MIME type of the given file.
    getMIMEType(String fileName)
    Determines the MIME type of a given file based on its file extension.
    boolean
    isImage(io.jmix.core.FileRef fileIO)
    Checks if the given file has an image MIME type.
    void
    Loads MIME type mappings into an internal map.
    void
    onApplicationStarted(org.springframework.boot.context.event.ApplicationStartedEvent event)
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FileService

      public FileService()
  • Method Details

    • onApplicationStarted

      @EventListener public void onApplicationStarted(org.springframework.boot.context.event.ApplicationStartedEvent event)
    • load

      public void load()
      Loads MIME type mappings into an internal map. This method initializes and populates a concurrent hash map that maps file extensions to their corresponding MIME types. The method uses a predefined set of MIME type mappings, as defined in a string containing MIME type to file extension mappings. It processes the mappings, extracting MIME types and their associated file extensions, and stores them in the internal map. Each MIME type can have multiple associated file extensions. A default MIME type of "application/octet-stream" is used for fallback scenarios when a specific type is not recognized. Debug logging is provided to confirm successful initialization. This method assumes the presence of a global map and the existence of an `addExtension` method to associate file extensions with MIME types in the map.
    • getIcon

      public com.vaadin.flow.component.icon.VaadinIcon getIcon(File file)
      Retrieves the corresponding VaadinIcon for the given file based on its MIME type.
      Parameters:
      file - the file for which the icon needs to be determined
      Returns:
      the appropriate VaadinIcon corresponding to the file's MIME type
    • getIcon

      public com.vaadin.flow.component.icon.VaadinIcon getIcon(String fileName)
      Returns a VaadinIcon corresponding to the MIME type of the provided file name.
      Parameters:
      fileName - the name of the file for which the icon is to be retrieved
      Returns:
      a VaadinIcon representing the file type determined by its MIME type
    • addExtension

      public void addExtension(String extension, String mimeType)
      Adds a file extension and its associated MIME type to the mapping.
      Parameters:
      extension - The file extension to be added. It should not be null, and is case-insensitive.
      mimeType - The MIME type associated with the file extension. It should not be null.
    • getMIMEType

      public String getMIMEType(File file)
      Determines the MIME type of the given file. If the provided file is a directory or a symbolic link, it returns an appropriate MIME type for those cases.
      Parameters:
      file - the file for which the MIME type is to be determined
      Returns:
      the MIME type as a String; returns "inode/drive" for drive directories, "inode/directory" for other directories, "inode/symlink" for symbolic links, or the MIME type based on the file's name for regular files
      Throws:
      NullPointerException - if the provided file is null
    • getMIMEType

      public String getMIMEType(String fileName)
      Determines the MIME type of a given file based on its file extension. If the file extension is recognized, the corresponding MIME type is returned. If the extension is not recognized or unavailable, a default MIME type is returned.
      Parameters:
      fileName - The name of the file for which to determine the MIME type. Must not be null.
      Returns:
      The MIME type corresponding to the file's extension, or a default MIME type if the extension is unrecognized.
      Throws:
      NullPointerException - If the file name provided is null.
    • isImage

      public boolean isImage(io.jmix.core.FileRef fileIO)
      Checks if the given file has an image MIME type.
      Parameters:
      fileIO - the name of the file to check the MIME type of
      Returns:
      true if the file has an image MIME type, false otherwise