public class MetaFiles extends Object
Constructor and Description |
---|
MetaFiles(MetaFileRepository filesRepo) |
Modifier and Type | Method and Description |
---|---|
DMSFile |
attach(InputStream stream,
String fileName,
Model entity)
Upload the given file stream and attach it to the given record.
|
MetaAttachment |
attach(MetaFile file,
Model entity)
Attach the given
MetaFile to the given Model object and return an instance of a
MetaAttachment that represents the attachment. |
DMSFile |
attach(MetaFile metaFile,
String fileName,
Model entity)
Attach the given file 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 given
DMSFile and also delete linked file if not referenced by any other
record. |
void |
delete(MetaAttachment attachment)
Delete the given attachment and related
MetaFile instance along with the file content. |
void |
delete(MetaFile metaFile)
Delete the given
MetaFile instance along with the file content. |
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 give
MetaFile 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 of
MetaFile
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 given
MetaFile . |
MetaFile |
upload(InputStream stream,
String fileName)
Upload the given stream to the upload directory.
|
@Inject public MetaFiles(MetaFileRepository filesRepo)
public static Path getPath(MetaFile file)
MetaFile
instance.file
- the given MetaFile
instancepublic static Path getPath(String filePath)
filePath
- relative file pathpublic static void checkPath(String filePath)
The filePath is value if it is inside upload directory or matches upload file whitelist pattern and doesn't match upload blacklist pattern.
filePath
- the file path to checkIllegalArgumentException
public static void checkType(String fileType)
The file is valid if it matches file upload whitelist types and doesn't match upload blacklist types.
fileType
- the file type to checkIllegalArgumentException
public static void checkType(File file)
The file is valid if it matches file upload whitelist types and doesn't match upload blacklist types.
file
- the file to checkIllegalArgumentException
public static Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
prefix
- the file prefix to usesuffix
- the file suffix to useattrs
- an optional list of file attributesIOException
- if an I/O error occursFiles.createTempFile(String, String, FileAttribute...)
public static Path findTempFile(String name)
name
- name of the temp filepublic void clean() throws IOException
IOException
- if an I/O error occurspublic void clean(String fileId) throws IOException
fileId
- the upload file idIOException
- if an I/O error occurspublic File upload(InputStream chunk, long startOffset, long fileSize, String fileId) throws IOException
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.
chunk
- the input streamstartOffset
- the start offset byte positionfileSize
- the actual file sizefileId
- an unique upload file identifierIOException
- if there is any error during io operationspublic MetaFile upload(File file) throws IOException
MetaFile
for the given file.file
- the given fileMetaFile
IOException
- if unable to read the filePersistenceException
- if unable to save to a MetaFile
instancepublic MetaFile upload(File file, MetaFile metaFile) throws IOException
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.
file
- the file to uploadmetaFile
- the target MetaFile
instanceMetaFile
instanceIOException
- if unable to read the filePersistenceException
- if unable to save to MetaFile
instancepublic MetaFile upload(InputStream stream, MetaFile metaFile) throws IOException
MetaFile
.
The given MetaFile
instance must have fileName set to save the stream as file.
Upload the stream
stream
- the stream to uploadmetaFile
- the MetaFile
to link the uploaded fileMetaFile
instanceIOException
- if an I/O error occurspublic MetaFile upload(InputStream stream, String fileName) throws IOException
stream
- the stream to uploadfileName
- the file name to useMetaFile
instanceIOException
- if an I/O error occurspublic DMSFile attach(InputStream stream, String fileName, Model entity) throws IOException
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.
stream
- the stream to uploadfileName
- the file name to useentity
- the record to attach toDMSFile
record created for the attachmentIOException
- if an I/O error occurspublic DMSFile attach(MetaFile metaFile, String fileName, Model entity)
metaFile
- the file to attachfileName
- alternative file name to use (optional, can be null)entity
- the record to attach toDMSFile
record created for the attachmentpublic void delete(DMSFile file)
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.
file
- the DMSFile
to deletepublic MetaAttachment attach(MetaFile file, Model entity)
MetaFile
to the given Model
object and return an instance of a
MetaAttachment
that represents the attachment.
The MetaAttachment
instance is not persisted.
file
- the given MetaFile
instanceentity
- the given Model
instanceMetaAttachment
public void delete(MetaAttachment attachment) throws IOException
MetaFile
instance along with the file content.attachment
- the attachment to deleteIOException
- if unable to delete filepublic void delete(MetaFile metaFile) throws IOException
MetaFile
instance along with the file content.metaFile
- the file to deleteIOException
- if unable to delete filepublic void deleteAttachments(Model entity)
entity
-