Search in sources :

Example 6 with FileRef

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

the class CubaFileStorage method toFileRef.

public FileRef toFileRef(FileDescriptor fileDescriptor) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(fileDescriptor.getCreateDate());
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);
    String datePath = year + "/" + StringUtils.leftPad(String.valueOf(month), 2, '0') + "/" + StringUtils.leftPad(String.valueOf(day), 2, '0');
    String fileExtension = StringUtils.isNoneBlank(fileDescriptor.getExtension()) ? "." + fileDescriptor.getExtension() : StringUtils.EMPTY;
    String path = datePath + "/" + fileDescriptor.getId() + fileExtension;
    return new FileRef(delegate.getStorageName(), path, fileDescriptor.getName());
}
Also used : FileRef(io.jmix.core.FileRef) Calendar(java.util.Calendar)

Example 7 with FileRef

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

the class FileStorageTest method testSaveLoad.

@Test
void testSaveLoad() throws IOException {
    FileRef ref = fileStorage.saveStream("testfile", new ByteArrayInputStream("some content".getBytes()));
    InputStream inputStream = fileStorage.openStream(ref);
    byte[] storedFile = ByteStreams.toByteArray(inputStream);
    assertEquals("some content", new String(storedFile));
}
Also used : FileRef(io.jmix.core.FileRef) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.jupiter.api.Test)

Example 8 with FileRef

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

the class TestFileStorage method saveStream.

@Override
public FileRef saveStream(String fileName, InputStream inputStream) {
    byte[] bytes;
    try {
        bytes = ByteStreams.toByteArray(inputStream);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    FileRef reference = new FileRef("testFs", fileName, fileName);
    files.put(reference, bytes);
    return reference;
}
Also used : FileRef(io.jmix.core.FileRef) IOException(java.io.IOException)

Example 9 with FileRef

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

the class GraphQLFilesDownloadController method downloadFile.

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

Example 10 with FileRef

use of io.jmix.core.FileRef 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;
}
Also used : AbstractMap(java.util.AbstractMap) MultipartFile(org.springframework.web.multipart.MultipartFile) FileRefDatatype(io.jmix.core.metamodel.datatype.impl.FileRefDatatype) FileRef(io.jmix.core.FileRef) CoercingParseValueException(graphql.schema.CoercingParseValueException) FileNotFoundException(java.io.FileNotFoundException) FileStorage(io.jmix.core.FileStorage) IOException(java.io.IOException) CoercingSerializeException(graphql.schema.CoercingSerializeException) IOException(java.io.IOException) CoercingParseValueException(graphql.schema.CoercingParseValueException) FileNotFoundException(java.io.FileNotFoundException)

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