@Component(value="cuba_FileUploading") public class FileUploading extends java.lang.Object implements FileUploadingAPI
FileUploadingAPI.FileInfo, FileUploadingAPI.UploadProgressListener| Modifier and Type | Field and Description |
|---|---|
protected io.jmix.ui.upload.TemporaryStorage |
delegate |
protected CubaFileStorage |
fileStorage |
protected io.jmix.core.TimeSource |
timeSource |
NAME| Constructor and Description |
|---|
FileUploading() |
| Modifier and Type | Method and Description |
|---|---|
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(io.jmix.ui.executor.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.
|
@Autowired protected io.jmix.ui.upload.TemporaryStorage delegate
@Autowired protected CubaFileStorage fileStorage
@Autowired protected io.jmix.core.TimeSource timeSource
public java.util.UUID saveFile(byte[] data)
throws FileStorageException
FileUploadingAPIsaveFile in interface FileUploadingAPIdata - file contentsFileStorageException - in case of IO problemspublic java.util.UUID saveFile(java.io.InputStream stream,
@Nullable
FileUploadingAPI.UploadProgressListener listener)
throws FileStorageException
FileUploadingAPIsaveFile in interface FileUploadingAPIstream - 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
FileUploadingAPIcreateEmptyFile in interface FileUploadingAPIFileStorageException - in case of IO problemspublic FileUploadingAPI.FileInfo createFile() throws FileStorageException
FileUploadingAPIcreateFile in interface FileUploadingAPIFileStorageException - in case of IO problemspublic java.util.UUID createNewFileId()
throws FileStorageException
FileUploadingAPIcreateNewFileId in interface FileUploadingAPIFileStorageException - in case of IO problems@Nullable public java.io.File getFile(java.util.UUID fileId)
FileUploadingAPIgetFile in interface FileUploadingAPIfileId - temporary file ID@Nullable public FileDescriptor getFileDescriptor(java.util.UUID fileId, java.lang.String name)
FileUploadingAPIgetFileDescriptor in interface FileUploadingAPIfileId - temporary file IDname - file name to set in the FileDescriptorpublic void deleteFile(java.util.UUID fileId)
throws FileStorageException
FileUploadingAPIdeleteFile in interface FileUploadingAPIfileId - temporary file IDFileStorageException - in case of IO problemspublic void deleteFileLink(java.lang.String fileName)
FileUploadingAPIdeleteFileLink in interface FileUploadingAPIfileName - absolute path to the temporary filepublic void putFileIntoStorage(java.util.UUID fileId,
FileDescriptor fileDescr)
throws FileStorageException
FileUploadingAPIputFileIntoStorage in interface FileUploadingAPIfileId - 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(io.jmix.ui.executor.TaskLifeCycle<java.lang.Long> taskLifeCycle) throws FileStorageException, java.lang.InterruptedException
FileUploadingAPIBackgroundTask.run(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 FileDescriptorUsage:
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 FileUploadingAPItaskLifeCycle - 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 interrupted