Search in sources :

Example 11 with FileRef

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;
}
Also used : FileRef(io.jmix.core.FileRef) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileStorageException(io.jmix.core.FileStorageException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 12 with 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);
    }
}
Also used : CompletedPart(software.amazon.awssdk.services.s3.model.CompletedPart) ArrayList(java.util.ArrayList) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) IOException(java.io.IOException) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) FileStorageException(io.jmix.core.FileStorageException) BufferedInputStream(java.io.BufferedInputStream) FileRef(io.jmix.core.FileRef) SdkException(software.amazon.awssdk.core.exception.SdkException) S3Client(software.amazon.awssdk.services.s3.S3Client) CompletedMultipartUpload(software.amazon.awssdk.services.s3.model.CompletedMultipartUpload) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest)

Example 13 with FileRef

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;
}
Also used : FileRef(io.jmix.core.FileRef) IOException(java.io.IOException)

Example 14 with FileRef

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());
}
Also used : FileRef(io.jmix.core.FileRef) UUID(java.util.UUID) Map(java.util.Map) Subscribe(io.jmix.ui.screen.Subscribe)

Example 15 with FileRef

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());
}
Also used : FileRef(io.jmix.core.FileRef) UUID(java.util.UUID) Map(java.util.Map) Subscribe(io.jmix.ui.screen.Subscribe)

Aggregations

FileRef (io.jmix.core.FileRef)19 IOException (java.io.IOException)8 InputStream (java.io.InputStream)5 Subscribe (io.jmix.ui.screen.Subscribe)4 FileStorage (io.jmix.core.FileStorage)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 FileStorageException (io.jmix.core.FileStorageException)2 FileInputStream (java.io.FileInputStream)2 URL (java.net.URL)2 Map (java.util.Map)2 UUID (java.util.UUID)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)1 Attachment (files.ex1.entity.Attachment)1 CoercingParseValueException (graphql.schema.CoercingParseValueException)1 CoercingSerializeException (graphql.schema.CoercingSerializeException)1 FileRefDatatype (io.jmix.core.metamodel.datatype.impl.FileRefDatatype)1 RestAPIException (io.jmix.rest.exception.RestAPIException)1 UrlResource (io.jmix.ui.component.UrlResource)1