Search in sources :

Example 1 with FileRef

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

the class FileStorageUploadFieldScreen method onManuallyControlledFieldFileUploadSucceed.

@Subscribe("manuallyControlledField")
public void onManuallyControlledFieldFileUploadSucceed(SingleFileUploadField.FileUploadSucceedEvent event) {
    // <1>
    File file = temporaryStorage.getFile(manuallyControlledField.getFileId());
    if (file != null) {
        notifications.create().withCaption("File is uploaded to temporary storage at " + file.getAbsolutePath()).show();
    }
    // <2>
    // <3>
    FileRef fileRef = temporaryStorage.putFileIntoStorage(manuallyControlledField.getFileId(), event.getFileName());
    manuallyControlledField.setValue(fileRef);
    notifications.create().withCaption("Uploaded file: " + manuallyControlledField.getFileName()).show();
}
Also used : FileRef(io.jmix.core.FileRef) File(java.io.File) Subscribe(io.jmix.ui.screen.Subscribe)

Example 2 with FileRef

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

the class ImageScreen method onInit.

// end::set-source[]
// end::listener[]
// tag::value-source[]
// tag::set-source[]
// tag::listener[]
@Subscribe
public void onInit(InitEvent event) {
    // end::listener[]
    // end::set-source[]
    personsTable.addGeneratedColumn("image", entity -> {
        Image<FileRef> image = uiComponents.create(Image.NAME);
        image.setValueSource(new ContainerValueSource<>(personsTable.getInstanceContainer(entity), "image"));
        image.setHeight("100px");
        image.setScaleMode(Image.ScaleMode.CONTAIN);
        return image;
    });
    // end::value-source[]
    // tag::set-source[]
    String address = "https://www.cuba-platform.com/sites/all/themes/cuba_adaptive/img/upper-header-logo.png";
    URL url = null;
    try {
        url = new URL(address);
        programmaticImage.setSource(UrlResource.class).setUrl(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
// end::set-source[]
// tag::listener[]
// tag::value-source[]
// tag::set-source[]
}
Also used : MalformedURLException(java.net.MalformedURLException) FileRef(io.jmix.core.FileRef) UrlResource(io.jmix.ui.component.UrlResource) URL(java.net.URL) Subscribe(io.jmix.ui.screen.Subscribe)

Example 3 with FileRef

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

the class LocalFileStorage method saveStream.

@Override
public FileRef saveStream(String fileName, InputStream inputStream) {
    Path relativePath = createRelativeFilePath(fileName);
    FileRef fileRef = new FileRef(storageName, pathToString(relativePath), fileName);
    saveStream(fileRef, inputStream);
    return fileRef;
}
Also used : Path(java.nio.file.Path) FileRef(io.jmix.core.FileRef)

Example 4 with FileRef

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

the class FileDownloadController method downloadFile.

@GetMapping
public void downloadFile(@RequestParam String fileRef, @RequestParam(required = false) Boolean attachment, HttpServletResponse response) {
    checkFileDownloadPermission();
    try {
        FileRef fileReference;
        fileReference = FileRef.fromString(fileRef);
        fileTransferService.downloadAndWriteResponse(fileReference, fileReference.getStorageName(), attachment, response);
    } catch (IllegalArgumentException e) {
        throw new RestAPIException("Invalid file reference", String.format("Cannot convert '%s' into valid file reference", fileRef), HttpStatus.BAD_REQUEST, e);
    }
}
Also used : FileRef(io.jmix.core.FileRef) RestAPIException(io.jmix.rest.exception.RestAPIException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 5 with FileRef

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

the class FileStorageUploadFieldImpl method saveFile.

protected void saveFile(String fileName) {
    checkFileStorageInitialized();
    switch(mode) {
        case MANUAL:
            internalValueChangedOnUpload = true;
            // FileRef should be set manually after uploading file into storage
            setValue(null);
            setValueToPresentation(fileName);
            internalValueChangedOnUpload = false;
            break;
        case IMMEDIATE:
            FileRef fileRef = temporaryStorage.putFileIntoStorage(fileId, fileName, fileStorage);
            setValue(fileRef);
            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