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);
}
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;
}
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";
}
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";
}
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);
}
}
Aggregations