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