Search in sources :

Example 16 with FileRef

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

Example 17 with FileRef

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

Example 18 with FileRef

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

Example 19 with FileRef

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

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