@Component(value="cuba_FileUploading") public class FileUploading extends java.lang.Object implements FileUploadingAPI, FileUploadingMBean
FileUploadingAPI.FileInfo, FileUploadingAPI.UploadProgressListener
NAME
Constructor and Description |
---|
FileUploading() |
Modifier and Type | Method and Description |
---|---|
void |
clearTempDirectory() |
java.util.UUID |
createEmptyFile()
Create a new empty temporary file and cache its ID for subsequent operations.
|
FileUploadingAPI.FileInfo |
createFile()
Create a new empty temporary file and cache its ID for subsequent operations.
|
java.util.UUID |
createNewFileId()
Create and cache a new temporary file ID.
|
void |
deleteFile(java.util.UUID fileId)
Remove a file from the temporary storage.
|
void |
deleteFileLink(java.lang.String fileName)
Remove an entry from the list of currently cached temporary file IDs, if such exists.
|
java.io.File |
getFile(java.util.UUID fileId)
Return a previously registered temporary file by its ID.
|
FileDescriptor |
getFileDescriptor(java.util.UUID fileId,
java.lang.String name)
Construct a new FileDescriptor for a temporary file to store it in the middleware FileStorage.
|
FileDescriptor |
putFileIntoStorage(TaskLifeCycle<java.lang.Long> taskLifeCycle)
Upload a file from the client's temporary storage into the middleware FileStorage.
|
void |
putFileIntoStorage(java.util.UUID fileId,
FileDescriptor fileDescr)
Upload a file from the client's temporary storage to the middleware FileStorage.
|
java.util.UUID |
saveFile(byte[] data)
Store the byte array in a new temporary file.
|
java.util.UUID |
saveFile(java.io.InputStream stream,
FileUploadingAPI.UploadProgressListener listener)
Store the content of stream in a new temporary file.
|
void |
setConfiguration(Configuration configuration) |
java.lang.String |
showTempFiles() |
@Inject public void setConfiguration(Configuration configuration)
public java.util.UUID saveFile(byte[] data) throws FileStorageException
FileUploadingAPI
saveFile
in interface FileUploadingAPI
data
- file contentsFileStorageException
- in case of IO problemspublic java.util.UUID saveFile(java.io.InputStream stream, FileUploadingAPI.UploadProgressListener listener) throws FileStorageException
FileUploadingAPI
saveFile
in interface FileUploadingAPI
stream
- stream which content is to be storedlistener
- optional listener to be notified about storing progressFileStorageException
- in case of IO problemspublic java.util.UUID createEmptyFile() throws FileStorageException
FileUploadingAPI
createEmptyFile
in interface FileUploadingAPI
FileStorageException
- in case of IO problemspublic FileUploadingAPI.FileInfo createFile() throws FileStorageException
FileUploadingAPI
createFile
in interface FileUploadingAPI
FileStorageException
- in case of IO problemspublic java.util.UUID createNewFileId() throws FileStorageException
FileUploadingAPI
createNewFileId
in interface FileUploadingAPI
FileStorageException
- in case of IO problemspublic java.io.File getFile(java.util.UUID fileId)
FileUploadingAPI
getFile
in interface FileUploadingAPI
fileId
- temporary file IDpublic FileDescriptor getFileDescriptor(java.util.UUID fileId, java.lang.String name)
FileUploadingAPI
getFileDescriptor
in interface FileUploadingAPI
fileId
- temporary file IDname
- file name to set in the FileDescriptorpublic void deleteFile(java.util.UUID fileId) throws FileStorageException
FileUploadingAPI
deleteFile
in interface FileUploadingAPI
fileId
- temporary file IDFileStorageException
- in case of IO problemspublic void deleteFileLink(java.lang.String fileName)
FileUploadingAPI
deleteFileLink
in interface FileUploadingAPI
fileName
- absolute path to the temporary filepublic void putFileIntoStorage(java.util.UUID fileId, FileDescriptor fileDescr) throws FileStorageException
FileUploadingAPI
putFileIntoStorage
in interface FileUploadingAPI
fileId
- file ID in the temporary storagefileDescr
- corresponding file descriptor entity. Attention: the FileDescriptor instance must be
stored in the database separately.FileStorageException
- in case of IO problemspublic FileDescriptor putFileIntoStorage(TaskLifeCycle<java.lang.Long> taskLifeCycle) throws FileStorageException, java.lang.InterruptedException
FileUploadingAPI
BackgroundTask.run(com.haulmont.cuba.gui.executors.TaskLifeCycle)
TaskLifeCycle.getParams()
map must contain the following entries with
String keys:
fileId
- file ID in the temporary storagefileName
- file name to set in the returned FileDescriptor
Usage:
BackgroundTask<Long, FileDescriptor> uploadProgress = new BackgroundTask<Long, FileDescriptor>(2400, ownerWindow) { @Override public Map<String, Object> getParams() { // file parameters Map<String, Object> params = new HashMap<>(); params.put("fileId", fileUpload.getFileId()); params.put("fileName", fileUpload.getFileName()); return params; } @Override public FileDescriptor run(final TaskLifeCycle<Long> taskLifeCycle) throws Exception { // upload file to middleware and return FileDescriptor return fileUploading.putFileIntoStorage(taskLifeCycle); } @Override public void done(FileDescriptor result) { // commit FileDescriptor to DB dataService.commit(new CommitContext(result)); } };
And then we can show upload progress window:
long fileSize = fileUploading.getFile(fileUpload.getFileId()).length(); BackgroundWorkProgressWindow.show(uploadProgress, getMessage("uploadingFile"), null, fileSize, true, true);
putFileIntoStorage
in interface FileUploadingAPI
taskLifeCycle
- task life cycle with specified params: fileId and fileNameFileDescriptor
instance must be
stored in the database separately.FileStorageException
- in case of IO problemsjava.lang.InterruptedException
- in case of uploading thread interruptedpublic void clearTempDirectory()
clearTempDirectory
in interface FileUploadingMBean
public java.lang.String showTempFiles()
showTempFiles
in interface FileUploadingMBean