Class MetaFiles
- java.lang.Object
-
- com.axelor.meta.MetaFiles
-
public class MetaFiles extends Object
This class provides some helper methods to deal with files.
-
-
Constructor Summary
Constructors Constructor Description MetaFiles(MetaFileRepository filesRepo)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MetaAttachmentattach(MetaFile file, Model entity)Attach the givenMetaFileto the givenModelobject and return an instance of aMetaAttachmentthat represents the attachment.DMSFileattach(MetaFile metaFile, String fileName, Model entity)Attach the given file to the given record.DMSFileattach(InputStream stream, String fileName, Model entity)Upload the given file stream and attach it to the given record.static voidcheckPath(String filePath)Check whether the given filePath is valid.static voidcheckType(File file)Check whether the given file is valid.static voidcheckType(String fileType)Check whether the given file type is valid.voidclean()Clean up obsolete temporary files from upload directory.voidclean(String fileId)This method can be used to delete temporary file of an incomplete upload.static PathcreateTempFile(String prefix, String suffix, FileAttribute<?>... attrs)Create a temporary file under upload directory.voiddelete(DMSFile file)Delete the givenDMSFileand also delete linked file if not referenced by any other record.voiddelete(MetaAttachment attachment)Delete the given attachment and relatedMetaFileinstance along with the file content.voiddelete(MetaFile metaFile)Delete the givenMetaFileinstance along with the file content if it exists.voiddeleteAttachments(Model entity)Deletes all attachments and related records (MetaFile, DMSFile, MetaAttachment).StringfileTypeIcon(MetaFile file)static PathfindTempFile(String name)Find a temporary file by the given name created previously.StringgetDownloadLink(MetaFile metaFile, Model parentModel)Gets download link for given meta file.static PathgetPath(MetaFile file)Get the actual storage path of the file represented by the giveMetaFileinstance.static PathgetPath(String filePath)Get the actual storage path of the given relative file path.MetaFileupload(File file)Upload the given file to the file upload directory and create an instance ofMetaFilefor the given file.MetaFileupload(File file, MetaFile metaFile)Fileupload(InputStream chunk, long startOffset, long fileSize, String fileId)Upload the given chunk of file data to a temporary file identified by the given file id.MetaFileupload(InputStream stream, MetaFile metaFile)Upload the given stream to the upload directory and link it to the to givenMetaFile.MetaFileupload(InputStream stream, String fileName)Upload the given stream to the upload directory.
-
-
-
Constructor Detail
-
MetaFiles
@Inject public MetaFiles(MetaFileRepository filesRepo)
-
-
Method Detail
-
getPath
public static Path getPath(MetaFile file)
Get the actual storage path of the file represented by the giveMetaFileinstance.- Parameters:
file- the givenMetaFileinstance- 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
-
createTempFile
public static Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
Create a temporary file under upload directory.- Parameters:
prefix- the file prefix to usesuffix- the file suffix to useattrs- an optional list of file attributes- Returns:
- the path to the newly created file
- Throws:
IOException- if an I/O error occurs- See Also:
Files.createTempFile(String, String, FileAttribute...)
-
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 IOExceptionClean 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)orupload(File)methods, this method doesn't createMetaFileinstance.The temporary file generated should be manually uploaded again using
upload(File, MetaFile)or should be deleted usingclean(String)method if something went wrong.- Parameters:
chunk- the input streamstartOffset- the start offset byte positionfileSize- the actual file sizefileId- 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 ofMetaFilefor the given file.- Parameters:
file- the given file- Returns:
- an instance of
MetaFile - Throws:
IOException- if unable to read the filejavax.persistence.PersistenceException- if unable to save to aMetaFileinstance
-
upload
public MetaFile upload(File file, MetaFile metaFile) throws IOException
Upload the givenFileto the upload directory and link it to the to givenMetaFile.Any existing file linked to the given
MetaFilewill be removed from the upload directory.- Parameters:
file- the file to uploadmetaFile- the targetMetaFileinstance- Returns:
- persisted
MetaFileinstance - Throws:
IOException- if unable to read the filejavax.persistence.PersistenceException- if unable to save toMetaFileinstance
-
upload
public MetaFile upload(InputStream stream, MetaFile metaFile) throws IOException
Upload the given stream to the upload directory and link it to the to givenMetaFile.The given
MetaFileinstance must have fileName set to save the stream as file. Upload the stream- Parameters:
stream- the stream to uploadmetaFile- theMetaFileto link the uploaded file- Returns:
- the given
MetaFileinstance - 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 uploadfileName- the file name to use- Returns:
- a new
MetaFileinstance - 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
DMSFileand will be visible in DMS user interface. Useupload(InputStream, String)along withattach(MetaFile, Model)if you don't want to show the attachment in DMS interface.- Parameters:
stream- the stream to uploadfileName- the file name to useentity- the record to attach to- Returns:
- a
DMSFilerecord 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 attachfileName- alternative file name to use (optional, can be null)entity- the record to attach to- Returns:
- a
DMSFilerecord created for the attachment
-
delete
public void delete(DMSFile file)
Delete the givenDMSFileand also delete linked file if not referenced by any other record.It will attempt to clean up associated
MetaFileandMetaAttachmentrecords and also try to delete linked file from upload directory.- Parameters:
file- theDMSFileto delete
-
attach
public MetaAttachment attach(MetaFile file, Model entity)
Attach the givenMetaFileto the givenModelobject and return an instance of aMetaAttachmentthat represents the attachment.The
MetaAttachmentinstance is not persisted.- Parameters:
file- the givenMetaFileinstanceentity- the givenModelinstance- Returns:
- a new instance of
MetaAttachment
-
delete
public void delete(MetaAttachment attachment) throws IOException
Delete the given attachment and relatedMetaFileinstance 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 givenMetaFileinstance 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-
-
-