use of io.jmix.core.FileRef 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);
}
}
use of io.jmix.core.FileRef in project jmix-docs by Haulmont.
the class AttachmentBrowse method saveToLocalFile.
private void saveToLocalFile(Attachment attachment, Path path) {
FileStorage fileStorage = fileStorageLocator.getDefault();
FileRef fileRef = attachment.getFile();
// <5>
InputStream inputStream = fileStorage.openStream(fileRef);
try {
// <6>
Files.copy(inputStream, path.resolve(fileRef.getFileName()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException("Error saving image", e);
}
}
use of io.jmix.core.FileRef in project jmix by jmix-framework.
the class FileUploading method putFileIntoStorage.
@Override
public void putFileIntoStorage(UUID fileId, FileDescriptor fileDescr) throws FileStorageException {
File file = getFile(fileId);
if (file == null) {
throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fileDescr.getName());
}
FileRef fileRef = fileStorage.toFileRef(fileDescr);
try (InputStream io = new FileInputStream(file)) {
fileStorage.getDelegate().saveStream(fileRef, 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, fileDescr.getName());
}
deleteFile(fileId);
}
use of io.jmix.core.FileRef in project jmix by jmix-framework.
the class FileStorageUploadFieldImpl method onFileNameClick.
@Override
protected void onFileNameClick(Button.ClickEvent e) {
FileRef value = getValue();
if (value == null && fileId == null) {
return;
}
switch(mode) {
case MANUAL:
String name = getFileName();
String fileName = StringUtils.isEmpty(name) ? (value != null ? value.getFileName() : "attachment") : name;
downloader.download(this::getFileContent, fileName);
break;
case IMMEDIATE:
if (value == null) {
return;
}
downloader.download(this::getFileContent, value.getFileName());
break;
}
}
Aggregations