Package net.sf.basedb.core
Class File
- All Implemented Interfaces:
AccessControlled
,Annotatable
,Controlled
,DiskConsumable
,Identifiable
,Nameable
,Ownable
,Removable
,Shareable
,Subtypable
,Transactional
This class is used to represent individual file items
and information about them.
This item also handels the physical file on disk. It should
work as expected, but under very unfortunate circumstances,
ie. an error happens during rollback or commit after the
changers has been written to the database, it may not delete
all files that should be deleted.
- Version:
- 2.0
- Author:
- enell
- Last modified
- $Date: 2023-12-20 14:32:15 +0100 (Wed, 20 Dec 2023) $
-
Nested Class Summary
Modifier and TypeClassDescription(package private) class
private class
This class is used for uploading a file.Nested classes/interfaces inherited from interface net.sf.basedb.core.Transactional
Transactional.Action
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The id for theFileType
item representing a file containing aligned sequence data.private StreamCacher
Files that are EXTERNAL are cached locally per transactionstatic final String
The id for theFileType
item representing a file containing analysed extra value data.static final String
The id for theFileType
item representing an image file.private File
The path to the internal file.static final int
The maximum length of the character set that can be stored in the database.static final int
The maximum length of the MIME type that can be stored in the database.static final int
The maximum length of the URL type that can be stored in the database.private File
The path to a newly uploaded file.static final String
The id for theFileType
item representing a plate file.static final String
The id for theFileType
item representing a plate mapping file.static final String
The id for theFileType
item representing a print map file.static final String
The id for theFileType
item representing a protocol.static final String
The id for theFileType
item representing a raw data file.static final String
The id for theFileType
item representing a reporter file.static final String
The id for theFileType
item representing a reporter map file.private static final String
static final String
The id for theFileType
item representing a file containing analysed spot data.private static final org.apache.commons.lang3.time.FastDateFormat
static final Item
The type of item represented by this class.private boolean
If the next upload should use compression or not.private File.UploadStream
A stream for uploading a physical file.Fields inherited from interface net.sf.basedb.core.Nameable
MAX_DESCRIPTION_LENGTH, MAX_NAME_LENGTH
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
compress
(ProgressReporter progress) Compress the file stored on the disk.private void
compressOrDecompress
(ProgressReporter progress, boolean compress) void
decompress
(ProgressReporter progress) Decompress the file stored on the disk.void
download
(OutputStream out, long offset) Download the physical file for this file item.static boolean
Checks if a file with the specified name exists in the directory.(package private) static File
getAbsolutePath
(String relativePath) Convert a relative path back to an absolute file path.Get all parents objects which are annotatable and the logged in user has read permission to.static File
Get aFile
item when you know the id.static File
Get aFile
item when you know the path.long
getBytes()
Get the absolute number of bytes this item uses.Get a (possible) cached stream for reading from the file.Get the character set used in this file (for text-files only).long
Get the compressed size of this file.(package private) FileData
getData()
Get theBasicData
object that holds all data for this item.long
Always 0.Get the associatedDirectory
item.getDownloadStream
(long offset) Get anInputStream
that can be used to download the physical file for this file item.static File
Get aFile
item when you know the directory and filename.Get the file server on which the external file is located.private String
Get the internal name of this file.Get the subtype of the item.Get the date and time the actual file data was last updated.Get theLocation
the items is stored at.getMd5()
Get the MD5 hash of the file contents, or null if not known.The mimetype of thisFile
.static File
Create a newFile
item.private File
getNewFile
(boolean compress) Get a new random file path.getPath()
Get the complete path of this file.getQuery()
Get a query configured to retrieveFile
items.AlwaysQuotaType.FILE
.(package private) static String
getRelativePath
(File file) Get the relative path of the specified file from the base upload directory and convert directory separator to forward slash.private ConnectionParameters
long
getSize()
Get the (uncompressed) size of this file.getType()
Get the type of item represented by the object.getUploadStream
(boolean checkMd5) Get anOutputStream
that can be used to upload a physical file for this file item.getUploadStream
(boolean checkMd5, Boolean compress) Get anOutputStream
that can be used to upload a physical file for this file item.getURI()
Get the URL of an external file as an URI.getUrl()
Get the URL to the (external) file.Get all:FileAttachable
items with this file attached(package private) void
initPermissions
(int granted, int denied) Check the write protected flag and deny DELETE if it is set.boolean
If this file is stored in a compressed format or not.boolean
isUsed()
Checks if: AFileAttachable
item is using this key.boolean
Check if the file data is write protected.(package private) void
onAfterCommit
(Transactional.Action action) This method is called on eachTransactional
object after a successful commit to the database.(package private) void
onBeforeCommit
(Transactional.Action action) If a project is active, automatically share the new item according to the settings of that project, unless a project or item key has been explicitely set (including null).(package private) void
onRollback
(Transactional.Action action) This method is called on eachTransactional
object after an unsuccessful commit to the database.void
setCharacterSet
(String charset) Set the character set used in this file (for text-files only).void
setDirectory
(Directory directory) Set the directory of thisFile
item.void
setFileServer
(FileServer fileServer) Set the file server on which this external file is located.private void
setInternalName
(String internalName) Set the internal name of this file.void
setItemSubtype
(ItemSubtype subtype) Set the subtype on the item.void
setLocation
(Location location) Set the location of thisFile
item.private void
setMetadataFromURI
(UriMetadata metadata) void
setMimeType
(String mimeType) Set the mime type for thisFile
item.void
setMimeTypeAuto
(String defaultMimeType, ItemSubtype defaultFileType) Set the mime type automatically by checking the extension of the name.void
Set the name of the item.void
Set the URL to an external file that should represent this file object.void
setUrl
(String url, UriMetadata metadata) Set the URL to an external file that should represent this file object and update the metadata without going to the remote server.void
setWriteProtected
(boolean writeProtected) Set the write protected flag for this file.void
If this is an external file, try to update the metadata.void
upload
(InputStream in, boolean checkMd5) Upload a physical file for this file item.void
upload
(InputStream in, boolean checkMd5, Boolean compress) Upload a physical file for this file item.boolean
Return the result of auto-compress check from the last call tosetMimeTypeAuto(String, ItemSubtype)
.boolean
Always FALSE.Methods inherited from class net.sf.basedb.core.AnnotatedItem
getAnnotationSet, getProtocol, isAnnotated, removeAnnotations
Methods inherited from class net.sf.basedb.core.CommonItem
getDescription, getName, getRemovedBy, isRemoved, setDescription, setRemoved
Methods inherited from class net.sf.basedb.core.SharedItem
getItemKey, getProjectKey, isShared, setItemKey, setProjectKey
Methods inherited from class net.sf.basedb.core.OwnedItem
getOwner, isOwner, setOwner, takeOwnership
Methods inherited from class net.sf.basedb.core.BasicItem
addAnnotatableParents, addUsingItems, addUsingItems, breakCircularReferences, checkBatchAnnotatableUsage, checkPermission, equals, getBasicData, getDbControl, getId, getPermissions, getPluginPermissions, getSessionControl, getVersion, hashCode, hasPermission, isDetached, isInDatabase, onAfterInsert, setDbControl, setProjectDefaults, toString, validate
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.sf.basedb.core.AccessControlled
checkPermission, getPermissions, hasPermission
Methods inherited from interface net.sf.basedb.core.Identifiable
getId, getVersion
Methods inherited from interface net.sf.basedb.core.Ownable
getOwner, isOwner, setOwner, takeOwnership
-
Field Details
-
TYPE
The type of item represented by this class. -
PROTOCOL
The id for theFileType
item representing a protocol.- Since:
- 3.0
- See Also:
-
RAW_DATA
The id for theFileType
item representing a raw data file.- Since:
- 3.0
- See Also:
-
PLATE_MAPPING
The id for theFileType
item representing a plate mapping file.- Since:
- 3.0
- See Also:
-
REPORTER_MAP
The id for theFileType
item representing a reporter map file.- Since:
- 3.0
- See Also:
-
PRINT_MAP
The id for theFileType
item representing a print map file.- Since:
- 3.0
- See Also:
-
IMAGE
The id for theFileType
item representing an image file.- Since:
- 3.0
- See Also:
-
REPORTER
The id for theFileType
item representing a reporter file.- Since:
- 3.0
- See Also:
-
PLATE
The id for theFileType
item representing a plate file.- Since:
- 3.0
- See Also:
-
SPOT_DATA
The id for theFileType
item representing a file containing analysed spot data.- Since:
- 3.0
- See Also:
-
EXTRAVALUE_DATA
The id for theFileType
item representing a file containing analysed extra value data.- Since:
- 3.0
- See Also:
-
ALIGNED_SEQUENCE
The id for theFileType
item representing a file containing aligned sequence data.- Since:
- 3.0
- See Also:
-
newInternalFile
The path to a newly uploaded file. -
downloadCache
Files that are EXTERNAL are cached locally per transaction -
internalFile
The path to the internal file. -
uploadStream
A stream for uploading a physical file. -
uploadCompressed
private boolean uploadCompressedIf the next upload should use compression or not. -
MAX_MIMETYPE_LENGTH
public static final int MAX_MIMETYPE_LENGTHThe maximum length of the MIME type that can be stored in the database.- See Also:
-
MAX_CHARSET_LENGTH
public static final int MAX_CHARSET_LENGTHThe maximum length of the character set that can be stored in the database.- Since:
- 2.9
- See Also:
-
MAX_URL_LENGTH
public static final int MAX_URL_LENGTHThe maximum length of the URL type that can be stored in the database.- Since:
- 2.16
- See Also:
-
separator
-
SUBDIR_FORMAT
private static final org.apache.commons.lang3.time.FastDateFormat SUBDIR_FORMAT
-
-
Constructor Details
-
File
File(FileData data) Creates a new file item from the given data.- Parameters:
data
- the data.
-
-
Method Details
-
getNew
Create a newFile
item.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.directory
- TheDirectory
that holds the file. It may not be null.- Returns:
- The new
File
item - Throws:
BaseException
- This exception is thrown if there is another error
-
getById
public static File getById(DbControl dc, int id) throws ItemNotFoundException, PermissionDeniedException, BaseException Get aFile
item when you know the id.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.id
- The id of the item to load- Returns:
- The
File
item - Throws:
ItemNotFoundException
- If an item with the specified id is not foundPermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission to the itemBaseException
- If there is another error
-
getByPath
public static File getByPath(DbControl dc, Path path, boolean create) throws ItemNotFoundException, PermissionDeniedException, BaseException Get aFile
item when you know the path. Use the create parameter to specify if a new file should be create if it doesn't exists. The directory part of the path is also created if needed.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.path
- The path to the file to loadcreate
- TRUE if a new file should be created if one doesn't eixts, FALSE to throw an exception (DbControl.saveItem must always be called)- Returns:
- The
File
item - Throws:
ItemNotFoundException
- If an item with the specified path is not found and create is FALSEPermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission to the itemBaseException
- If there is another error
-
getFile
public static File getFile(DbControl dc, Directory directory, String fileName, boolean create) throws ItemNotFoundException, PermissionDeniedException, BaseException Get aFile
item when you know the directory and filename. Use the create parameter to specify if a new file should be create if it doesn't exists.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.directory
- The directory where the file is located.fileName
- The name of the file.create
- TRUE if a new file should be created if one doesn't eixts, FALSE to throw an exception (DbControl.saveItem must always be called)- Returns:
- The
File
item - Throws:
ItemNotFoundException
- If an item with the specified path is not found and create is FALSEPermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission to the itemBaseException
- If there is another error
-
exists
public static boolean exists(DbControl dc, Directory directory, String fileName) throws BaseException Checks if a file with the specified name exists in the directory.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.directory
- The directory to check in.fileName
- The name of the file to look for.- Returns:
- TRUE if the file exists, FALSE otherwise.
- Throws:
BaseException
-
getQuery
Get a query configured to retrieveFile
items.- Returns:
- An
ItemQuery
object
-
getData
FileData getData()Description copied from class:BasicItem
Get theBasicData
object that holds all data for this item.- Overrides:
getData
in classAnnotatedItem
-
getType
Description copied from interface:Identifiable
Get the type of item represented by the object. The returned value is one of the values defined in theItem
enumeration.- Specified by:
getType
in interfaceIdentifiable
- Returns:
- A value indicating the type of item
-
setName
Description copied from interface:Nameable
Set the name of the item. The name cannot be null and mustn't be longer than the value specified by theNameable.MAX_NAME_LENGTH
constant.- Specified by:
setName
in interfaceNameable
- Overrides:
setName
in classCommonItem
- Parameters:
name
- The new name for the item- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permissionInvalidDataException
- If the name is null or longer than specified by theNameable.MAX_NAME_LENGTH
constant
-
useDifference
public boolean useDifference()Always FALSE.- Specified by:
useDifference
in interfaceDiskConsumable
- Returns:
- TRUE if the item reports the difference, FALSE if it reports the absolute
- See Also:
-
getBytes
public long getBytes()Description copied from interface:DiskConsumable
Get the absolute number of bytes this item uses. All items must report this number.- Specified by:
getBytes
in interfaceDiskConsumable
- Returns:
- The absolute number of bytes the item uses
- See Also:
-
getDifference
public long getDifference()Always 0.- Specified by:
getDifference
in interfaceDiskConsumable
- Returns:
- The difference in bytes between now and when the item was loaded, or 0 if this item doesn't report differences
-
getQuotaTypeSystemId
AlwaysQuotaType.FILE
.- Specified by:
getQuotaTypeSystemId
in interfaceDiskConsumable
- Returns:
- The systemid of the QuotaType
-
getLocation
Description copied from interface:DiskConsumable
Get theLocation
the items is stored at.- Specified by:
getLocation
in interfaceDiskConsumable
- Returns:
- The location
-
getItemSubtype
Description copied from interface:Subtypable
Get the subtype of the item.- Specified by:
getItemSubtype
in interfaceSubtypable
- Returns:
- A subtype of null if not set
- See Also:
-
setItemSubtype
Description copied from interface:Subtypable
Set the subtype on the item.- Specified by:
setItemSubtype
in interfaceSubtypable
- Parameters:
subtype
- A subtype or null- See Also:
-
getAnnotatableParents
Description copied from interface:Annotatable
Get all parents objects which are annotatable and the logged in user has read permission to. If the item doesn't have any annotatable parents, it may return null or an empty set. The method should only return the immediate parent(s), not parents to parents, etc. As of BASE 3.1 this method may also return child items if the child item is aSubtypable
item that has a subtype with theItemSubtype.getPushAnnotations()
flag set.- Specified by:
getAnnotatableParents
in interfaceAnnotatable
- Returns:
- A set containing annotatable items, or null
-
isUsed
Checks if:- A
FileAttachable
item is using this key.
- Overrides:
isUsed
in classBasicItem
- Returns:
- TRUE if this item is used, FALSE otherwise
- Throws:
BaseException
- If there is another error- See Also:
- A
-
getUsingItems
Get all:FileAttachable
items with this file attached
- Overrides:
getUsingItems
in classBasicItem
- Returns:
- A set containing proxies for the items, or an empty set if no items are using this item
- Since:
- 2.2
- See Also:
-
onBeforeCommit
Description copied from class:SharedItem
If a project is active, automatically share the new item according to the settings of that project, unless a project or item key has been explicitely set (including null).- Overrides:
onBeforeCommit
in classSharedItem
- Throws:
BaseException
- If there is another error- See Also:
-
onAfterCommit
Description copied from class:BasicItem
This method is called on eachTransactional
object after a successful commit to the database. If the subclass overrides this method it should also propagate the call to the superclass, ie.super.onAfterCommit(action)
. It is forbidden to access theDbControl
object from this method and it must not throw any exceptions. All exceptions should be logged using theApplication.getLogger()
object. -
onRollback
Description copied from class:BasicItem
This method is called on eachTransactional
object after an unsuccessful commit to the database. If the subclass overrides this method it should also propagate the call to the superclass, ie.super.onRollback(action)
. It is forbidden to access theDbControl
object from this method and it must not throw any exceptions. All exceptions should be logged using theApplication.getLogger()
object. -
initPermissions
void initPermissions(int granted, int denied) Check the write protected flag and deny DELETE if it is set.- Overrides:
initPermissions
in classSharedItem
- Parameters:
granted
- Permissions that have been granted by the subclassdenied
- Permissions that have been denied by the subclass
-
getInternalName
Get the internal name of this file.- Returns:
- The internal name.
-
setInternalName
Set the internal name of this file.- Parameters:
internalName
- The internal filename.
-
setLocation
public void setLocation(Location location) throws PermissionDeniedException, InvalidUseOfNullException Set the location of thisFile
item.- Parameters:
location
- The newLocation
- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permission, or if the file is write protected and the new location isn'tLocation.PRIMARY
InvalidUseOfNullException
- If location is null
-
getSize
public long getSize()Get the (uncompressed) size of this file.- Returns:
- The size in bytes or -1 if not known
- See Also:
-
getCompressedSize
public long getCompressedSize()Get the compressed size of this file. If the file is not stored in a compressed format this method return the same value asgetSize()
- Returns:
- The compressed size in bytes or -1 if not known
- Since:
- 2.5
- See Also:
-
isCompressed
public boolean isCompressed()If this file is stored in a compressed format or not.- Returns:
- TRUE if the file is compressed
- Since:
- 2.5
-
getMd5
Get the MD5 hash of the file contents, or null if not known.- Returns:
- A 32-character hexadecimal string
-
getMimeType
The mimetype of thisFile
. It can be a definedMimeType
, any mimetype or null.- Returns:
- the mimetype of this file
-
setMimeType
Set the mime type for thisFile
item. It can be any mimetype or one avalible in the mimetype table.- Parameters:
mimeType
- The new mime type for this item- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permissionInvalidDataException
- If the mime type is longer thanMAX_MIMETYPE_LENGTH
-
getCharacterSet
Get the character set used in this file (for text-files only).- Returns:
- The name of the character set as returned by
Charset.name()
, or null if not known or applicable to the file type - Since:
- 2.9
-
setCharacterSet
Set the character set used in this file (for text-files only).- Parameters:
charset
- The name of the character set as returned byCharset.name()
- Since:
- 2.9
-
getDirectory
Get the associatedDirectory
item.- Returns:
- The
Directory
item - Throws:
PermissionDeniedException
- This exception is thrown if the logged in user doesn't haveREAD
permission to the itemsBaseException
- If there is another error
-
setDirectory
public void setDirectory(Directory directory) throws PermissionDeniedException, InvalidUseOfNullException Set the directory of thisFile
item.- Parameters:
directory
- The newDirectory
- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.WRITE
permission to the file orPermission.USE
permission to the directoryInvalidUseOfNullException
- If directory is null
-
isWriteProtected
public boolean isWriteProtected()Check if the file data is write protected. A write protected file can't be deleted, moved offline or to the secondary storage, or replaced with another file. This setting doesn't affect metadata about the file, such as the name, description, file type, mime type, etc.- Returns:
- TRUE if the file is write protected, FALSE otherwise
- Since:
- 2.4
-
setWriteProtected
Set the write protected flag for this file.- Parameters:
writeProtected
- TRUE if the file should be write protected- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permission on the file item.- Since:
- 2.4
- See Also:
-
getLastUpdate
Get the date and time the actual file data was last updated. This value is only affected by file uploads, not by changing the name, description or any other metadata.- Returns:
- The date or null if no file has been uploaded
- Since:
- 2.4
-
getUrl
Get the URL to the (external) file. This property is only set/valid for files that are stored externally (getLocation() == Location.EXTERNAL)- Returns:
- The URL or null if the file is stored internally
- Since:
- 2.16
-
getURI
Get the URL of an external file as an URI. This property is only set/valid for files that are stored externally (getLocation() == Location.EXTERNAL). If the URL is is not a valid URI an exception is thrown.- Returns:
- An URI or null
- Since:
- 3.0
-
setUrl
Set the URL to an external file that should represent this file object. NOTE! The location, size, md5, internal file, etc. will be reset as a result of this call. NOTE 2! If the file server is also updated at the same time it is important that the file server has been changed before setting the URL, otherwise the old file server is used.- Parameters:
url
- The url to the file (null is not allowed)loadMetadata
- If TRUE, a HEAD request will be issued to the given URL and metadata such as size, MIME type, etc. will be extracted if given in the response- Throws:
InvalidDataException
- If the URL is null, too long, or not a valid URLPermissionDeniedException
- If the logged in user doesn't have write permission- Since:
- 2.16
-
setUrl
Set the URL to an external file that should represent this file object and update the metadata without going to the remote server.- Parameters:
url
- The url to the file (null is not allowed)metadata
- Metadata for the URL (or null to not update the metadata)- Throws:
InvalidDataException
- If the URL is null, too long, or not a valid URLPermissionDeniedException
- If the logged in user doesn't have write permission- Since:
- 3.11
-
updateURLMetadata
public void updateURLMetadata()If this is an external file, try to update the metadata.- Since:
- 3.3
-
getFileServer
Get the file server on which the external file is located. The file server can be used to store username/password that is required to access the file. External files that doesn't require authentication usually don't need to be associated with a server.- Returns:
- A FileServer or null if no file server has been set
- Since:
- 2.16
-
setFileServer
public void setFileServer(FileServer fileServer) throws PermissionDeniedException, InvalidUseOfNullException Set the file server on which this external file is located.- Parameters:
fileServer
- The newFileServer
- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.WRITE
permission to the file orPermission.USE
permission to the file serverInvalidUseOfNullException
- Since:
- 2.16
-
getServerInfo
-
setMetadataFromURI
-
getPath
Get the complete path of this file.- Returns:
- A
Path
object
-
setMimeTypeAuto
public void setMimeTypeAuto(String defaultMimeType, ItemSubtype defaultFileType) throws PermissionDeniedException Set the mime type automatically by checking the extension of the name. If no match is found, the mime type is set to the default value if specified, or left unchanged if the default is null. This method will also check theMimeType.getAutoCompress()
flag and select auto-compression for the next upload if it is set. Auto-compression is also selected if theDirectory.getAutoCompress()
flag is set.- Parameters:
defaultMimeType
- The default value to use if no match is found for the file extensiondefaultFileType
- The default file type to set if no match is found or null to leave it unchanged- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.WRITE
permission- Since:
- 3.0
- See Also:
-
uploadCompressed
public boolean uploadCompressed()Return the result of auto-compress check from the last call tosetMimeTypeAuto(String, ItemSubtype)
. If this method returns true, the next call toupload(InputStream, boolean)
orgetUploadStream(boolean)
will store the uploaded file in a compressed format.- Returns:
- TRUE if the next upload should be compressed
- Since:
- 2.5
-
compress
Compress the file stored on the disk. If the file is already compressed or not located in theLocation.PRIMARY
location this method does nothing.- Parameters:
progress
- An optional progress reporter- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permission on the file- Since:
- 2.5
-
decompress
Decompress the file stored on the disk. If the file isn't compressed or not located in theLocation.PRIMARY
location this method does nothing.- Parameters:
progress
- An optional progress reporter- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permission on the file- Since:
- 2.5
-
compressOrDecompress
-
upload
public void upload(InputStream in, boolean checkMd5) throws PermissionDeniedException, BaseException Upload a physical file for this file item. This method callsupload(InputStream, boolean, Boolean)
. A null value is used for the compress parameter to enable auto-compression.- Parameters:
in
- Input stream from the file to be uploaded.checkMd5
- TRUE if the Md5 sum should be checked, FALSE otherwise.- Throws:
PermissionDeniedException
- If the logged in user doesn't have permission to upload the fileBaseException
- If there is some other error.
-
upload
public void upload(InputStream in, boolean checkMd5, Boolean compress) throws PermissionDeniedException, BaseException Upload a physical file for this file item.- Parameters:
in
- AnInputStream
which will be read until end of file is foundcheckMd5
- If the MD5 sum of the uploaded data should be checked against the MD5 sum already stored in the database. If no MD5 exists in the database this parameter is ignoredcompress
- TRUE to store the file in a compressed format, or null to use whatever theDirectory.getAutoCompress()
is specifying or what auto-detection from thesetMimeTypeAuto(String, ItemSubtype)
found- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.WRITE
permission for this item, or if the file is write protectedBaseException
- If there is another error- Since:
- 2.5
-
getUploadStream
Get anOutputStream
that can be used to upload a physical file for this file item. This method callsgetUploadStream(boolean, Boolean)
. A null value is used for the compress parameter to enable auto-compression.- Parameters:
checkMd5
- TRUE if the Md5 sum should be checked, FALSE otherwise- Returns:
- An output stream to upload file to.
-
getUploadStream
public OutputStream getUploadStream(boolean checkMd5, Boolean compress) throws PermissionDeniedException, BaseException Get anOutputStream
that can be used to upload a physical file for this file item. The client application should callclose()
method to finish the upload. Note! Calling this method multiple times without closing the returned stream will return the same stream and the checkMd5 and compress parameters are ignored.- Parameters:
checkMd5
- If the MD5 sum of the uploaded data should be checked against the MD5 sum already stored in the database. If no MD5 exists in the database this parameter is ignoredcompress
- TRUE to store the file in a compressed format, or null to use whatever theDirectory.getAutoCompress()
is specifying or what auto-detection from thesetMimeTypeAuto(String, ItemSubtype)
found- Returns:
- An
OutputStream
object - Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.WRITE
permission for this item or if the file is write protectedBaseException
- If there is another error- Since:
- 2.5
-
download
Download the physical file for this file item.- Parameters:
out
- AnOutputStream
object which will be used to write the fileoffset
- Start reading the physical at the specified offset, use 0 to start reading from the beginning- Throws:
BaseException
- If there is an error
-
getDownloadStream
Get anInputStream
that can be used to download the physical file for this file item. The client application should callclose()
method to finish the download. If the file is an external file, the file will be loaded from the URL given bygetUrl()
, otherwise if the physical file isn't at the primary location an empty stream is returned.- Parameters:
offset
- Start reading the physical at the specified offset, use 0 to start reading from the beginning- Returns:
- An
InputStream
object - Throws:
BaseException
- If there is an error
-
getCachedDownloadStream
Get a (possible) cached stream for reading from the file. This method will enable caching (to the local disk) for external or compressed files. This method is useful if you need to read the file multiple times in the same transaction (for example, when trying to auto-detect a file format). The cached data is cleared at the end of the transaction.- Returns:
- An
InputStream
object - Since:
- 2.16
- See Also:
-
getNewFile
Get a new random file path.- Returns:
- A new random unique file.
- Throws:
BaseException
- If there is an error.
-
getRelativePath
Get the relative path of the specified file from the base upload directory and convert directory separator to forward slash.- Since:
- 2.2
- See Also:
-
getAbsolutePath
Convert a relative path back to an absolute file path.- Since:
- 2.2
- See Also:
-