protected void checkPrimaryStorageAccessible(File[] roots, FileDescriptor fileDescr) throws FileStorageException { if (!roots[0].exists()) { log.error("Inaccessible primary storage at {}", roots[0]); throw new FileStorageException(FileStorageException.Type.STORAGE_INACCESSIBLE, fileDescr.getId().toString()); } }
protected void checkStorageDefined(File[] roots, FileDescriptor fileDescr) throws FileStorageException { if (roots.length == 0) { log.error("No storage directories defined"); throw new FileStorageException(FileStorageException.Type.STORAGE_INACCESSIBLE, fileDescr.getId().toString()); } }
public static String getFileName(FileDescriptor fileDescriptor) { return fileDescriptor.getId().toString() + "." + fileDescriptor.getExtension(); }
@Override public byte[] loadFile(FileDescriptor fileDescr) throws FileStorageException { InputStream inputStream = openStream(fileDescr); try { return IOUtils.toByteArray(inputStream); } catch (IOException e) { throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fileDescr.getId().toString(), e); } finally { IOUtils.closeQuietly(inputStream); } }
protected void downloadFromMiddlewareAndWriteResponse(FileDescriptor fd, HttpServletResponse response) throws IOException { ServletOutputStream os = response.getOutputStream(); try (InputStream is = fileLoader.openStream(fd)) { IOUtils.copy(is, os); os.flush(); } catch (FileStorageException e) { throw new RestAPIException("Unable to download file from FileStorage", "Unable to download file from FileStorage: " + fd.getId(), HttpStatus.INTERNAL_SERVER_ERROR, e); } }
@Override public byte[] loadFile(FileDescriptor fileDescr) throws FileStorageException { InputStream inputStream = openStream(fileDescr); try { return IOUtils.toByteArray(inputStream); } catch (IOException e) { throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fileDescr.getId().toString(), e); } finally { IOUtils.closeQuietly(inputStream); } }
/** * Creates URL for downloading a file. * * @param fd file descriptor * @param attachment sets Content-Disposition: attachment * @return URL string */ public static String makeUrl(FileDescriptor fd, boolean attachment) { StringBuilder sb = new StringBuilder(); UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME); sb.append("dispatch/download?") .append("s=").append(sessionSource.getUserSession().getId()).append("&") .append("f=").append(fd.getId()); if (attachment) sb.append("&a=true"); return sb.toString(); }
protected ResponseEntity<FileInfo> createFileInfoResponseEntity(HttpServletRequest request, FileDescriptor fd) { FileInfo fileInfo = new FileInfo(fd.getId(), fd.getName(), fd.getSize()); UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString()) .path("/{id}") .buildAndExpand(fd.getId().toString()); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setLocation(uriComponents.toUri()); return new ResponseEntity<>(fileInfo, httpHeaders, HttpStatus.CREATED); }
@Override public void putFileIntoStorage(UUID fileId, FileDescriptor fileDescr) throws FileStorageException { try { uploadFileIntoStorage(fileId, fileDescr, null); } catch (InterruptedException e) { // should never happen throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fileDescr.getId().toString()); } deleteFile(fileId); }
protected FileDescriptor createFileDescriptor(@Nullable String fileName, long size) { FileDescriptor fd = metadata.create(FileDescriptor.class); if (Strings.isNullOrEmpty(fileName)) { fileName = fd.getId().toString(); } fd.setName(fileName); fd.setExtension(FilenameUtils.getExtension(fileName)); fd.setSize(size); fd.setCreateDate(timeSource.currentTimestamp()); return fd; }
@Override public InputStream openStream(FileDescriptor fileDescr) throws FileStorageException { checkFileDescriptor(fileDescr); File[] roots = getStorageRoots(); if (roots.length == 0) { log.error("No storage directories available"); throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fileDescr.getId().toString()); } InputStream inputStream = null; for (File root : roots) { File dir = getStorageDir(root, fileDescr); File file = new File(dir, getFileName(fileDescr)); if (!file.exists()) { log.error("File " + file + " not found"); continue; } try { inputStream = FileUtils.openInputStream(file); break; } catch (IOException e) { log.error("Error opening input stream for " + file, e); } } if (inputStream != null) return inputStream; else throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fileDescr.getId().toString()); }
protected void uploadToMiddleware(InputStream is, FileDescriptor fd) throws FileStorageException { try { fileLoader.saveStream(fd, new FileLoader.SingleInputStreamSupplier(is)); } catch (FileStorageException e) { throw new RestAPIException("Unable to upload file to FileStorage", "Unable to upload file to FileStorage: " + fd.getId(), HttpStatus.INTERNAL_SERVER_ERROR, e); } } }
String url = selectedUrl + fileDownloadContext + "?s=" + userSession.getId() + "&f=" + fd.getId().toString();
writer.write(fd.getId().toString()); writer.close();
File file = new File(dir, com.haulmont.cuba.core.app.filestorage.FileStorage.getFileName(fileDescriptor)); if (!file.exists()) { sb.append(fileDescriptor.getId()) .append(", ") .append(fileDescriptor.getName())
String url = selectedUrl + fileDownloadContext + "?s=" + userSessionSource.getUserSession().getId() + "&f=" + fd.getId().toString();
protected void checkIfFileDescriptorExists(FileDescriptor fd) throws FileStorageException { try (Transaction tx = persistence.getTransaction()) { FileDescriptor existingFile = persistence.getEntityManager().find(FileDescriptor.class, fd.getId()); if (existingFile == null || entityStates.isDeleted(existingFile)) { throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fd.getName()); } tx.commit(); } } }
@Override protected byte[] getContent(Object paramValue) { try { DataManager dataManager = AppBeans.get(DataManager.class); FileStorageAPI fileStorageAPI = AppBeans.get(FileStorageAPI.class); FileDescriptor file; if (paramValue instanceof FileDescriptor) { file = dataManager.load(new LoadContext<>(FileDescriptor.class).setId(((FileDescriptor) paramValue).getId())); } else { file = dataManager.load(new LoadContext<>(FileDescriptor.class).setId(UuidProvider.fromString(paramValue.toString()))); } byte[] bytes = fileStorageAPI.loadFile(file); return bytes; } catch (FileStorageException e) { throw new ReportFormattingException(String.format("Unable to get image from file storage. File id [%s]", paramValue), e); } } }
stencilDs.getItem().setIconFileId(fd.getId()); stencilDs.getItem().setIconFile(fd); downloadIconBtn.setCaption(fd.getName());