Search in sources :

Example 1 with Attachment

use of files.ex1.entity.Attachment in project jmix-docs by Haulmont.

the class AttachmentBrowse method onSaveImageBtnClick.

@Subscribe("saveImageBtn")
public void onSaveImageBtnClick(Button.ClickEvent event) {
    Attachment attachment = attachmentsTable.getSingleSelected();
    if (attachment == null)
        return;
    saveToLocalFile(attachment, Paths.get("."));
}
Also used : Attachment(files.ex1.entity.Attachment)

Example 2 with Attachment

use of files.ex1.entity.Attachment in project jmix-docs by Haulmont.

the class AttachmentBrowse method attachmentsTableFileColumnGenerator.

@Install(to = "attachmentsTable.fileName", subject = "columnGenerator")
private Component attachmentsTableFileColumnGenerator(Attachment attachment) {
    if (attachment.getFile() != null) {
        LinkButton linkButton = uiComponents.create(LinkButton.class);
        linkButton.setAction(new BaseAction("download").withCaption(attachment.getFile().getFileName()).withHandler(actionPerformedEvent -> downloader.download(attachment.getFile())));
        return linkButton;
    } else {
        return new Table.PlainTextCell("<empty>");
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) Autowired(org.springframework.beans.factory.annotation.Autowired) FileStorage(io.jmix.core.FileStorage) FileRef(io.jmix.core.FileRef) StandardCopyOption(java.nio.file.StandardCopyOption) UiComponents(io.jmix.ui.UiComponents) LookupComponent(io.jmix.ui.screen.LookupComponent) URLConnection(java.net.URLConnection) BaseAction(io.jmix.ui.action.BaseAction) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Path(java.nio.file.Path) RestTemplate(org.springframework.web.client.RestTemplate) Resource(org.springframework.core.io.Resource) DataManager(io.jmix.core.DataManager) Files(java.nio.file.Files) FileStorageLocator(io.jmix.core.FileStorageLocator) IOException(java.io.IOException) File(java.io.File) HttpStatus(org.springframework.http.HttpStatus) io.jmix.ui.screen(io.jmix.ui.screen) Attachment(files.ex1.entity.Attachment) Paths(java.nio.file.Paths) Downloader(io.jmix.ui.download.Downloader) ResponseEntity(org.springframework.http.ResponseEntity) io.jmix.ui.component(io.jmix.ui.component) InputStream(java.io.InputStream) BaseAction(io.jmix.ui.action.BaseAction)

Example 3 with Attachment

use of files.ex1.entity.Attachment 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

Attachment (files.ex1.entity.Attachment)3 FileRef (io.jmix.core.FileRef)2 FileStorage (io.jmix.core.FileStorage)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 URLConnection (java.net.URLConnection)2 DataManager (io.jmix.core.DataManager)1 FileStorageLocator (io.jmix.core.FileStorageLocator)1 UiComponents (io.jmix.ui.UiComponents)1 BaseAction (io.jmix.ui.action.BaseAction)1 io.jmix.ui.component (io.jmix.ui.component)1 Downloader (io.jmix.ui.download.Downloader)1 io.jmix.ui.screen (io.jmix.ui.screen)1 LookupComponent (io.jmix.ui.screen.LookupComponent)1 File (java.io.File)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1