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 MetaAttachment
attach(MetaFile file, Model entity)
Attach the givenMetaFile
to the givenModel
object and return an instance of aMetaAttachment
that represents the attachment.DMSFile
attach(MetaFile metaFile, String fileName, Model entity)
Attach the given file to the given record.DMSFile
attach(InputStream stream, String fileName, Model entity)
Upload the given file stream and attach it to the given record.static void
checkPath(String filePath)
Check whether the given filePath is valid.static void
checkType(File file)
Check whether the given file is valid.static void
checkType(String fileType)
Check whether the given file type is valid.void
clean()
Clean up obsolete temporary files from upload directory.void
clean(String fileId)
This method can be used to delete temporary file of an incomplete upload.static Path
createTempFile(String prefix, String suffix, FileAttribute<?>... attrs)
Create a temporary file under upload directory.void
delete(DMSFile file)
Delete the givenDMSFile
and also delete linked file if not referenced by any other record.void
delete(MetaAttachment attachment)
Delete the given attachment and relatedMetaFile
instance along with the file content.void
delete(MetaFile metaFile)
Delete the givenMetaFile
instance along with the file content if it exists.void
deleteAttachments(Model entity)
Deletes all attachments and related records (MetaFile, DMSFile, MetaAttachment).String
fileTypeIcon(MetaFile file)
static Path
findTempFile(String name)
Find a temporary file by the given name created previously.String
getDownloadLink(MetaFile metaFile, Model parentModel)
Gets download link for given meta file.static Path
getPath(MetaFile file)
Get the actual storage path of the file represented by the giveMetaFile
instance.static Path
getPath(String filePath)
Get the actual storage path of the given relative file path.MetaFile
upload(File file)
Upload the given file to the file upload directory and create an instance ofMetaFile
for the given file.MetaFile
upload(File file, MetaFile metaFile)
File
upload(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.MetaFile
upload(InputStream stream, MetaFile metaFile)
Upload the given stream to the upload directory and link it to the to givenMetaFile
.MetaFile
upload(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 giveMetaFile
instance.- Parameters:
file
- the givenMetaFile
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
-
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 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)
orupload(File)
methods, this method doesn't createMetaFile
instance.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 ofMetaFile
for 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 aMetaFile
instance
-
upload
public MetaFile upload(File file, MetaFile metaFile) throws IOException
Upload the givenFile
to the upload directory and link it to the to givenMetaFile
.Any existing file linked to the given
MetaFile
will be removed from the upload directory.- Parameters:
file
- the file to uploadmetaFile
- the targetMetaFile
instance- Returns:
- persisted
MetaFile
instance - Throws:
IOException
- if unable to read the filejavax.persistence.PersistenceException
- if unable to save toMetaFile
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 givenMetaFile
.The given
MetaFile
instance must have fileName set to save the stream as file. Upload the stream- Parameters:
stream
- the stream to uploadmetaFile
- theMetaFile
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 uploadfileName
- 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. 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
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 attachfileName
- 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 givenDMSFile
and also delete linked file if not referenced by any other record.It will attempt to clean up associated
MetaFile
andMetaAttachment
records and also try to delete linked file from upload directory.- Parameters:
file
- theDMSFile
to delete
-
attach
public MetaAttachment attach(MetaFile file, Model entity)
Attach the givenMetaFile
to the givenModel
object and return an instance of aMetaAttachment
that represents the attachment.The
MetaAttachment
instance is not persisted.- Parameters:
file
- the givenMetaFile
instanceentity
- the givenModel
instance- Returns:
- a new instance of
MetaAttachment
-
delete
public void delete(MetaAttachment attachment) throws IOException
Delete the given attachment and relatedMetaFile
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 givenMetaFile
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
-
-
-