use of io.jmix.core.FileRef in project jmix by jmix-framework.
the class TemporaryStorageImpl method putFileIntoStorage.
@Override
public FileRef putFileIntoStorage(UUID fileId, String fileName, FileStorage fileStorage) {
File file = getFile(fileId);
if (file == null) {
throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fileName);
}
FileRef fileRef;
try (InputStream io = new FileInputStream(file)) {
fileRef = fileStorage.saveStream(fileName, io);
} catch (FileNotFoundException e) {
throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, "Temp file is not found " + file.getAbsolutePath());
} catch (IOException e) {
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fileName);
}
deleteFile(fileId);
return fileRef;
}
use of io.jmix.core.FileRef in project jmix by jmix-framework.
the class AwsFileStorage method saveStream.
@Override
public FileRef saveStream(String fileName, InputStream inputStream) {
String fileKey = createFileKey(fileName);
int s3ChunkSizeBytes = this.chunkSize * 1024;
try (BufferedInputStream bos = new BufferedInputStream(inputStream, s3ChunkSizeBytes)) {
S3Client s3Client = s3ClientReference.get();
int totalSizeBytes = bos.available();
if (totalSizeBytes == 0) {
s3Client.putObject(PutObjectRequest.builder().bucket(bucket).key(fileKey).build(), RequestBody.empty());
return new FileRef(getStorageName(), fileKey, fileName);
}
CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder().bucket(bucket).key(fileKey).build();
CreateMultipartUploadResponse response = s3Client.createMultipartUpload(createMultipartUploadRequest);
List<CompletedPart> completedParts = new ArrayList<>();
for (int partNumber = 1, readBytes = 0; readBytes != totalSizeBytes; partNumber++) {
byte[] chunkBytes = new byte[Math.min(totalSizeBytes - readBytes, s3ChunkSizeBytes)];
readBytes += bos.read(chunkBytes);
UploadPartRequest uploadPartRequest = UploadPartRequest.builder().bucket(bucket).key(fileKey).uploadId(response.uploadId()).partNumber(partNumber).build();
String eTag = s3Client.uploadPart(uploadPartRequest, RequestBody.fromBytes(chunkBytes)).eTag();
CompletedPart part = CompletedPart.builder().partNumber(partNumber).eTag(eTag).build();
completedParts.add(part);
}
CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder().parts(completedParts).build();
CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder().bucket(bucket).key(fileKey).uploadId(response.uploadId()).multipartUpload(completedMultipartUpload).build();
s3Client.completeMultipartUpload(completeMultipartUploadRequest);
return new FileRef(getStorageName(), fileKey, fileName);
} catch (IOException | SdkException e) {
log.error("Error saving file to S3 storage", e);
String message = String.format("Could not save file %s.", fileName);
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, message);
}
}
use of io.jmix.core.FileRef in project jmix by jmix-framework.
the class TestFileStorage method saveStream.
@Override
public FileRef saveStream(String fileName, InputStream inputStream) {
byte[] bytes;
try {
bytes = ByteStreams.toByteArray(inputStream);
} catch (IOException e) {
throw new RuntimeException("Failed to save stream", e);
}
FileRef reference = new FileRef(getStorageName(), fileName, fileName);
files.put(reference, bytes);
return reference;
}
use of io.jmix.core.FileRef in project jmix-docs by Haulmont.
the class ScreenWithMultiUploadField method onInit.
// end::autowired[]
// tag::on-init[]
@Subscribe
protected void onInit(InitEvent event) {
fileMultiUploadFieldVersioned.addQueueUploadCompleteListener(e -> {
for (Map.Entry<UUID, String> entry : fileMultiUploadFieldVersioned.getUploadsMap().entrySet()) {
UUID fileId = entry.getKey();
String fileName = entry.getValue();
// <1>
FileRef fileRef = temporaryStorage.putFileIntoStorage(fileId, fileName);
// <2>
webdavDocumentsManagementService.createVersioningDocumentByFileRef(fileRef);
}
notifications.create().withType(Notifications.NotificationType.HUMANIZED).withCaption("Uploaded files: " + fileMultiUploadFieldVersioned.getUploadsMap().values()).show();
fileMultiUploadFieldVersioned.clearUploads();
});
fileMultiUploadFieldVersioned.addFileUploadErrorListener(e -> notifications.create().withCaption("File upload error").withType(Notifications.NotificationType.HUMANIZED).show());
}
use of io.jmix.core.FileRef in project jmix-docs by Haulmont.
the class FileMultiUploadFieldScreen method onInit.
@Subscribe
public void onInit(InitEvent event) {
// <2>
fileMultiUploadField.addQueueUploadCompleteListener(queueUploadCompleteEvent -> {
// <3>
for (Map.Entry<UUID, String> entry : fileMultiUploadField.getUploadsMap().entrySet()) {
// <4>
UUID fileId = entry.getKey();
String fileName = entry.getValue();
// <5>
FileRef fileRef = temporaryStorage.putFileIntoStorage(fileId, fileName);
}
notifications.create().withCaption("Uploaded files: " + fileMultiUploadField.getUploadsMap().values()).show();
// <6>
fileMultiUploadField.clearUploads();
});
fileMultiUploadField.addFileUploadErrorListener(queueFileUploadErrorEvent -> notifications.create().withCaption("File upload error").show());
}
Aggregations