Class MetaFiles


  • public class MetaFiles
    extends Object
    This class provides some helper methods to deal with files.
    • Method Detail

      • getPath

        public static Path getPath​(MetaFile file)
        Get the actual storage path of the file represented by the give MetaFile instance.
        Parameters:
        file - the given MetaFile instance
        Returns:
        actual file path
      • getPath

        public static Path getPath​(String filePath)
        Get the actual storage path of the given relative file path.
        Parameters:
        filePath - relative file path
        Returns:
        actual file path
      • checkPath

        public static void checkPath​(String filePath)
        Check whether the given filePath is valid.

        The filePath is value if it is inside upload directory or matches upload file whitelist pattern and doesn't match upload blacklist pattern.

        Parameters:
        filePath - the file path to check
        Throws:
        IllegalArgumentException
      • checkType

        public static void checkType​(String fileType)
        Check whether the given file type is valid.

        The file is valid if it matches file upload whitelist types and doesn't match upload blacklist types.

        Parameters:
        fileType - the file type to check
        Throws:
        IllegalArgumentException
      • checkType

        public static void checkType​(File file)
        Check whether the given file is valid.

        The file is valid if it matches file upload whitelist types and doesn't match upload blacklist types.

        Parameters:
        file - the file to check
        Throws:
        IllegalArgumentException
      • findTempFile

        public static Path findTempFile​(String name)
        Find a temporary file by the given name created previously.
        Parameters:
        name - name of the temp file
        Returns:
        file path
      • clean

        public void clean()
                   throws IOException
        Clean up obsolete temporary files from upload directory.
        Throws:
        IOException - if an I/O error occurs
      • clean

        public void clean​(String fileId)
                   throws IOException
        This method can be used to delete temporary file of an incomplete upload.
        Parameters:
        fileId - the upload file id
        Throws:
        IOException - if an I/O error occurs
      • upload

        public File upload​(InputStream chunk,
                           long startOffset,
                           long fileSize,
                           String fileId)
                    throws IOException
        Upload the given chunk of file data to a temporary file identified by the given file id.

        Upload would restart if startOffset is 0 (zero), otherwise upload file size is checked against given startOffset. The startOffset must be less than expected fileSize.

        Unlike the upload(File, MetaFile) or upload(File) methods, this method doesn't create MetaFile instance.

        The temporary file generated should be manually uploaded again using upload(File, MetaFile) or should be deleted using clean(String) method if something went wrong.

        Parameters:
        chunk - the input stream
        startOffset - the start offset byte position
        fileSize - the actual file size
        fileId - an unique upload file identifier
        Returns:
        a temporary file where upload is being saved
        Throws:
        IOException - if there is any error during io operations
      • upload

        public MetaFile upload​(File file)
                        throws IOException
        Upload the given file to the file upload directory and create an instance of MetaFile for the given file.
        Parameters:
        file - the given file
        Returns:
        an instance of MetaFile
        Throws:
        IOException - if unable to read the file
        javax.persistence.PersistenceException - if unable to save to a MetaFile instance
      • upload

        public MetaFile upload​(File file,
                               MetaFile metaFile)
                        throws IOException
        Upload the given File to the upload directory and link it to the to given MetaFile.

        Any existing file linked to the given MetaFile will be removed from the upload directory.

        Parameters:
        file - the file to upload
        metaFile - the target MetaFile instance
        Returns:
        persisted MetaFile instance
        Throws:
        IOException - if unable to read the file
        javax.persistence.PersistenceException - if unable to save to MetaFile instance
      • upload

        public MetaFile upload​(InputStream stream,
                               MetaFile metaFile)
                        throws IOException
        Upload the given stream to the upload directory and link it to the to given MetaFile.

        The given MetaFile instance must have fileName set to save the stream as file. Upload the stream

        Parameters:
        stream - the stream to upload
        metaFile - the MetaFile to link the uploaded file
        Returns:
        the given MetaFile instance
        Throws:
        IOException - if an I/O error occurs
      • upload

        public MetaFile upload​(InputStream stream,
                               String fileName)
                        throws IOException
        Upload the given stream to the upload directory.
        Parameters:
        stream - the stream to upload
        fileName - the file name to use
        Returns:
        a new MetaFile instance
        Throws:
        IOException - if an I/O error occurs
      • attach

        public DMSFile attach​(InputStream stream,
                              String fileName,
                              Model entity)
                       throws IOException
        Upload the given file stream and attach it to the given record.

        The attachment will be saved as DMSFile and will be visible in DMS user interface. Use upload(InputStream, String) along with attach(MetaFile, Model) if you don't want to show the attachment in DMS interface.

        Parameters:
        stream - the stream to upload
        fileName - the file name to use
        entity - the record to attach to
        Returns:
        a DMSFile record created for the attachment
        Throws:
        IOException - if an I/O error occurs
      • attach

        public DMSFile attach​(MetaFile metaFile,
                              String fileName,
                              Model entity)
        Attach the given file to the given record.
        Parameters:
        metaFile - the file to attach
        fileName - alternative file name to use (optional, can be null)
        entity - the record to attach to
        Returns:
        a DMSFile record created for the attachment
      • delete

        public void delete​(DMSFile file)
        Delete the given DMSFile and also delete linked file if not referenced by any other record.

        It will attempt to clean up associated MetaFile and MetaAttachment records and also try to delete linked file from upload directory.

        Parameters:
        file - the DMSFile to delete
      • delete

        public void delete​(MetaAttachment attachment)
                    throws IOException
        Delete the given attachment and related MetaFile instance along with the file content.
        Parameters:
        attachment - the attachment to delete
        Throws:
        IOException - if unable to delete file
      • delete

        public void delete​(MetaFile metaFile)
                    throws IOException
        Delete the given MetaFile instance along with the file content if it exists.
        Parameters:
        metaFile - the file to delete
        Throws:
        IOException - if unable to delete file
      • deleteAttachments

        public void deleteAttachments​(Model entity)
        Deletes all attachments and related records (MetaFile, DMSFile, MetaAttachment). Finally, deletes all real files.
        Parameters:
        entity -
      • getDownloadLink

        public String getDownloadLink​(MetaFile metaFile,
                                      Model parentModel)
        Gets download link for given meta file. Permissions can be checked from given parent record.
        Parameters:
        metaFile -
        parentModel -
        Returns:
        download link