Search in sources :

Example 1 with FileStorage

use of io.jmix.core.FileStorage in project jmix by jmix-framework.

the class FileStorageTest method testLocator.

@Test
void testLocator() {
    FileStorage fs1 = fileStorageLocator.getByName("testFs");
    fs1.saveStream("testfile", new ByteArrayInputStream(new byte[0]));
    // usage with concrete storage type
    TestFileStorage fs2 = fileStorageLocator.getByName("testFs2");
    fs2.saveStream("testfile", new ByteArrayInputStream(new byte[0]));
    // usage with default storage
    FileStorage fsTest = fileStorageLocator.getDefault();
    fsTest.saveStream("testfile", new ByteArrayInputStream(new byte[0]));
    assertSame(fs1, fsTest);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TestFileStorage(test_support.app.TestFileStorage) FileStorage(io.jmix.core.FileStorage) TestFileStorage(test_support.app.TestFileStorage) Test(org.junit.jupiter.api.Test)

Example 2 with FileStorage

use of io.jmix.core.FileStorage in project jmix by jmix-framework.

the class FileRefCoercing method parseValue.

@Override
public Object parseValue(Object o) throws CoercingParseValueException {
    String storageName = null;
    try {
        if (o instanceof AbstractMap.SimpleEntry) {
            storageName = ((AbstractMap.SimpleEntry<String, MultipartFile>) o).getKey();
            FileStorage fileStorage = fileService.getFileStorage(storageName);
            MultipartFile multipartFile = ((AbstractMap.SimpleEntry<String, MultipartFile>) o).getValue();
            FileRefDatatype refDatatype = new FileRefDatatype();
            FileRef fileRef = fileService.saveFileIntoStorage(multipartFile, fileStorage);
            return refDatatype.format(fileRef);
        }
        if (o instanceof String) {
            FileRef fileRef = FileRef.fromString((String) o);
            FileStorage fileStorage = fileService.getFileStorage(fileRef.getStorageName());
            if (!fileStorage.fileExists(fileRef)) {
                throw new FileNotFoundException();
            }
            return o;
        }
    } catch (FileNotFoundException e) {
        throw new CoercingParseValueException("File with name " + FileRef.fromString((String) o).getFileName() + " not found");
    } catch (IOException e) {
        throw new CoercingParseValueException("Exception with saving file");
    } catch (Exception e) {
        throw new CoercingParseValueException("File storage with name " + storageName + " not found");
    }
    return null;
}
Also used : AbstractMap(java.util.AbstractMap) MultipartFile(org.springframework.web.multipart.MultipartFile) FileRefDatatype(io.jmix.core.metamodel.datatype.impl.FileRefDatatype) FileRef(io.jmix.core.FileRef) CoercingParseValueException(graphql.schema.CoercingParseValueException) FileNotFoundException(java.io.FileNotFoundException) FileStorage(io.jmix.core.FileStorage) IOException(java.io.IOException) CoercingSerializeException(graphql.schema.CoercingSerializeException) IOException(java.io.IOException) CoercingParseValueException(graphql.schema.CoercingParseValueException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with FileStorage

use of io.jmix.core.FileStorage in project jmix by jmix-framework.

the class AwsFileStorageManagementFacade method refreshS3Client.

@ManagedOperation(description = "Refresh Amazon S3 file storage client by storage name")
@ManagedOperationParameters({ @ManagedOperationParameter(name = "storageName", description = "Storage name"), @ManagedOperationParameter(name = "accessKey", description = "Amazon S3 access key"), @ManagedOperationParameter(name = "secretAccessKey", description = "Amazon S3 secret access key"), @ManagedOperationParameter(name = "region", description = "Amazon S3 region"), @ManagedOperationParameter(name = "bucket", description = "Amazon S3 bucket name"), @ManagedOperationParameter(name = "chunkSize", description = "Amazon S3 chunk size (kB)"), @ManagedOperationParameter(name = "endpointUrl", description = "Optional custom S3 storage endpoint URL") })
public String refreshS3Client(String storageName, String accessKey, String secretAccessKey, String region, String bucket, int chunkSize, @Nullable String endpointUrl) {
    FileStorage fileStorage = fileStorageLocator.getByName(storageName);
    if (fileStorage instanceof AwsFileStorage) {
        AwsFileStorage awsFileStorage = (AwsFileStorage) fileStorage;
        awsFileStorage.setAccessKey(accessKey);
        awsFileStorage.setSecretAccessKey(secretAccessKey);
        awsFileStorage.setRegion(region);
        awsFileStorage.setBucket(bucket);
        awsFileStorage.setChunkSize(chunkSize);
        awsFileStorage.setEndpointUrl(endpointUrl);
        awsFileStorage.refreshS3Client();
        return "Refreshed successfully";
    }
    return "Not an Amazon S3 file storage - refresh attempt ignored";
}
Also used : FileStorage(io.jmix.core.FileStorage) ManagedOperationParameters(org.springframework.jmx.export.annotation.ManagedOperationParameters) ManagedOperation(org.springframework.jmx.export.annotation.ManagedOperation)

Example 4 with FileStorage

use of io.jmix.core.FileStorage in project jmix by jmix-framework.

the class AwsFileStorageManagementFacade method refreshS3Client.

@ManagedOperation(description = "Refresh Amazon S3 file storage client by storage name")
@ManagedOperationParameters({ @ManagedOperationParameter(name = "storageName", description = "Storage name"), @ManagedOperationParameter(name = "accessKey", description = "Amazon S3 access key"), @ManagedOperationParameter(name = "secretAccessKey", description = "Amazon S3 secret access key") })
public String refreshS3Client(String storageName, String accessKey, String secretAccessKey) {
    FileStorage fileStorage = fileStorageLocator.getByName(storageName);
    if (fileStorage instanceof AwsFileStorage) {
        AwsFileStorage awsFileStorage = (AwsFileStorage) fileStorage;
        awsFileStorage.setAccessKey(accessKey);
        awsFileStorage.setSecretAccessKey(secretAccessKey);
        awsFileStorage.refreshS3Client();
        return "Refreshed successfully";
    }
    return "Not an Amazon S3 file storage - refresh attempt ignored";
}
Also used : FileStorage(io.jmix.core.FileStorage) ManagedOperationParameters(org.springframework.jmx.export.annotation.ManagedOperationParameters) ManagedOperation(org.springframework.jmx.export.annotation.ManagedOperation)

Example 5 with FileStorage

use of io.jmix.core.FileStorage in project jmix-docs by Haulmont.

the class AttachmentBrowse method getAndSaveImage.

private void getAndSaveImage() {
    try {
        // <2>
        URLConnection connection = new URL("https://picsum.photos/300").openConnection();
        try (InputStream responseStream = connection.getInputStream()) {
            // <3>
            FileStorage fileStorage = fileStorageLocator.getDefault();
            FileRef fileRef = fileStorage.saveStream("photo.jpg", responseStream);
            // <4>
            Attachment attachment = dataManager.create(Attachment.class);
            attachment.setFile(fileRef);
            dataManager.save(attachment);
        }
    } catch (IOException e) {
        throw new RuntimeException("Error getting image", e);
    }
}
Also used : FileRef(io.jmix.core.FileRef) InputStream(java.io.InputStream) FileStorage(io.jmix.core.FileStorage) Attachment(files.ex1.entity.Attachment) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL)

Aggregations

FileStorage (io.jmix.core.FileStorage)8 FileRef (io.jmix.core.FileRef)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ManagedOperation (org.springframework.jmx.export.annotation.ManagedOperation)2 ManagedOperationParameters (org.springframework.jmx.export.annotation.ManagedOperationParameters)2 Attachment (files.ex1.entity.Attachment)1 CoercingParseValueException (graphql.schema.CoercingParseValueException)1 CoercingSerializeException (graphql.schema.CoercingSerializeException)1 FetchPlanRepository (io.jmix.core.FetchPlanRepository)1 FileStorageLocator (io.jmix.core.FileStorageLocator)1 TimeSource (io.jmix.core.TimeSource)1 FileRefDatatype (io.jmix.core.metamodel.datatype.impl.FileRefDatatype)1 PersistenceHints (io.jmix.data.PersistenceHints)1 EmailCleaner (io.jmix.email.EmailCleaner)1 EmailerProperties (io.jmix.email.EmailerProperties)1 SendingMessage (io.jmix.email.entity.SendingMessage)1 FileParseException (io.jmix.search.exception.FileParseException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1