List of usage examples for org.apache.commons.vfs2 FileObject isFolder
boolean isFolder() throws FileSystemException;
From source file:com.sonicle.webtop.vfs.PublicService.java
public void processPreviewFiles(HttpServletRequest request, HttpServletResponse response, PrintWriter out) { ArrayList<JsPubGridFile> items = new ArrayList<>(); try {//from w w w . ja v a 2s. co m String linkId = ServletUtils.getStringParameter(request, "linkId", true); String fileId = ServletUtils.getStringParameter(request, "fileId", true); SharingLink link = manager.getSharingLink(linkId); if (link == null) throw new WTException("Link not found [{0}]", linkId); if (!isLinkAuthorized(link)) throw new WTException("Link not authorized [{0}]", linkId); String path = PathUtils.concatPaths(link.getFilePath(), fileId); if (!PathUtils.isFolder(path)) throw new WTException("Invalid file [{0}]", path); StoreFileSystem sfs = manager.getStoreFileSystem(link.getStoreId()); for (FileObject fo : manager.listStoreFiles(StoreFileType.FILE_OR_FOLDER, link.getStoreId(), path)) { if (VfsUtils.isFileObjectHidden(fo)) continue; // Relativize path and force trailing separator if file is a folder String filePath = fo.isFolder() ? PathUtils.ensureTrailingSeparator(sfs.getRelativePath(fo), false) : sfs.getRelativePath(fo); // Relativize path to link (suitable only for folders...see before) filePath = link.relativizePath(filePath); items.add(new JsPubGridFile(filePath, fo)); } new JsonResult("files", items).printTo(out); } catch (Exception ex) { logger.error("Error in PreviewFiles", ex); new JsonResult(false, "Error").printTo(out); } }
From source file:com.sonicle.webtop.vfs.PublicService.java
private void writeStoreFile(HttpServletResponse response, int storeId, String filePath, String outFileName) { try {//w ww . j av a 2 s . com FileObject fo = null; try { fo = manager.getStoreFile(storeId, filePath); if (fo.isFile()) { String mediaType = ServletHelper.guessMediaType(fo.getName().getBaseName(), true); ServletUtils.setFileStreamHeaders(response, mediaType, DispositionType.ATTACHMENT, outFileName); ServletUtils.setContentLengthHeader(response, fo.getContent().getSize()); IOUtils.copy(fo.getContent().getInputStream(), response.getOutputStream()); } else if (fo.isFolder()) { ServletUtils.setFileStreamHeaders(response, "application/zip", DispositionType.ATTACHMENT, outFileName); ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); try { VfsUtils.zipFileObject(fo, zos, true); zos.flush(); } finally { IOUtils.closeQuietly(zos); } } } finally { IOUtils.closeQuietly(fo); } } catch (Exception ex) { logger.error("Error in DownloadFile", ex); ServletUtils.sendError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }
From source file:com.sonicle.webtop.vfs.Service.java
public void processManageGridFiles(HttpServletRequest request, HttpServletResponse response, PrintWriter out) { ArrayList<JsGridFile> items = new ArrayList<>(); try {//from ww w . j av a 2 s . c o m String crud = ServletUtils.getStringParameter(request, "crud", true); if (crud.equals(Crud.READ)) { String parentFileId = ServletUtils.getStringParameter(request, "fileId", null); StoreNodeId parentNodeId = (StoreNodeId) new StoreNodeId().parse(parentFileId); int storeId = Integer.valueOf(parentNodeId.getStoreId()); StoreShareFolder folder = getFolderFromCache(storeId); String path = (parentNodeId.getSize() == 2) ? "/" : parentNodeId.getPath(); boolean showHidden = us.getShowHiddenFiles(); LinkedHashMap<String, SharingLink> dls = manager.listDownloadLinks(storeId, path); LinkedHashMap<String, SharingLink> uls = manager.listUploadLinks(storeId, path); StoreFileSystem sfs = manager.getStoreFileSystem(storeId); for (FileObject fo : manager.listStoreFiles(StoreFileType.FILE_OR_FOLDER, storeId, path)) { if (!showHidden && VfsUtils.isFileObjectHidden(fo)) continue; // Relativize path and force trailing separator if file is a folder final String filePath = fo.isFolder() ? PathUtils.ensureTrailingSeparator(sfs.getRelativePath(fo), false) : sfs.getRelativePath(fo); final String fileId = new StoreNodeId(parentNodeId.getShareId(), parentNodeId.getStoreId(), filePath).toString(); final String fileHash = manager.generateStoreFileHash(storeId, filePath); items.add(new JsGridFile(folder, fo, fileId, dls.get(fileHash), uls.get(fileHash))); } new JsonResult("files", items).printTo(out); } } catch (Exception ex) { logger.error("Error in action ManageGridFiles", ex); new JsonResult(false, "Error").printTo(out); } }
From source file:maspack.fileutil.FileCacher.java
public File cache(URIx uri, File cacheFile, FileTransferMonitor monitor) throws FileSystemException { // For atomic operation, first download to temporary directory File tmpCacheFile = new File(cacheFile.getAbsolutePath() + TMP_EXTENSION); URIx cacheURI = new URIx(cacheFile.getAbsoluteFile()); URIx tmpCacheURI = new URIx(tmpCacheFile.getAbsoluteFile()); FileObject localTempFile = manager.resolveFile(tmpCacheURI.toString(true)); FileObject localCacheFile = manager.resolveFile(cacheURI.toString(true)); FileObject remoteFile = null; // will resolve next // loop through authenticators until we either succeed or cancel boolean cancel = false; while (remoteFile == null && cancel == false) { remoteFile = resolveRemote(uri); }//from w ww. ja va2s .c o m if (remoteFile == null || !remoteFile.exists()) { throw new FileSystemException("Cannot find remote file <" + uri.toString() + ">", new FileNotFoundException("<" + uri.toString() + ">")); } // monitor the file transfer progress if (monitor != null) { monitor.monitor(localTempFile, remoteFile, -1, cacheFile.getName()); monitor.start(); monitor.fireStartEvent(localTempFile); } // transfer content try { if (remoteFile.isFile()) { localTempFile.copyFrom(remoteFile, Selectors.SELECT_SELF); } else if (remoteFile.isFolder()) { // final FileObject fileSystem = manager.createFileSystem(remoteFile); localTempFile.copyFrom(remoteFile, new AllFileSelector()); // fileSystem.close(); } if (monitor != null) { monitor.fireCompleteEvent(localTempFile); } } catch (Exception e) { // try to delete local file localTempFile.delete(); throw new RuntimeException( "Failed to complete transfer of " + remoteFile.getURL() + " to " + localTempFile.getURL(), e); } finally { // close files if we need to localTempFile.close(); remoteFile.close(); if (monitor != null) { monitor.release(localTempFile); monitor.stop(); } } // now that the copy is complete, do a rename operation try { if (tmpCacheFile.isDirectory()) { SafeFileUtils.moveDirectory(tmpCacheFile, cacheFile); } else { SafeFileUtils.moveFile(tmpCacheFile, cacheFile); } } catch (Exception e) { localCacheFile.delete(); // delete if possible throw new RuntimeException("Failed to atomically move " + "to " + localCacheFile.getURL(), e); } return cacheFile; }
From source file:com.sonicle.webtop.vfs.VfsManager.java
public String createStoreFile(StoreFileType fileType, int storeId, String parentPath, String name) throws FileSystemException, WTException { FileObject tfo = null, ntfo = null; try {//from w w w.j a v a 2 s .c om checkRightsOnStoreElements(storeId, "UPDATE"); // Rights check! tfo = getTargetFileObject(storeId, parentPath); if (!tfo.isFolder()) throw new IllegalArgumentException("Please provide a valid parentPath"); String newPath = FilenameUtils.separatorsToUnix(FilenameUtils.concat(parentPath, name)); ntfo = getTargetFileObject(storeId, newPath); logger.debug("Creating store file [{}, {}]", storeId, newPath); if (fileType.equals(StoreFileType.FOLDER)) { ntfo.createFolder(); } else { ntfo.createFile(); } return newPath; } catch (Exception ex) { logger.warn("Error creating store file", ex); throw ex; } finally { IOUtils.closeQuietly(tfo); IOUtils.closeQuietly(ntfo); } }
From source file:com.sonicle.webtop.vfs.VfsManager.java
public String createStoreFileFromStream(int storeId, String parentPath, String name, InputStream is, boolean overwrite) throws IOException, FileSystemException, WTException { FileObject tfo = null; NewTargetFile ntf = null;// w w w. j av a2 s.c om OutputStream os = null; try { checkRightsOnStoreElements(storeId, "UPDATE"); // Rights check! tfo = getTargetFileObject(storeId, parentPath); if (!tfo.isFolder()) throw new IllegalArgumentException("Please provide a valid parentPath"); ntf = getNewTargetFileObject(storeId, parentPath, name, overwrite); logger.debug("Creating store file from stream [{}, {}]", storeId, ntf.path); ntf.tfo.createFile(); try { os = ntf.tfo.getContent().getOutputStream(); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(os); } return ntf.path; } catch (Exception ex) { logger.warn("Error creating store file from stream", ex); throw ex; } finally { IOUtils.closeQuietly(tfo); if (ntf != null) IOUtils.closeQuietly(ntf.tfo); } }
From source file:maspack.fileutil.FileCacher.java
public boolean copy(URIx from, URIx to, FileTransferMonitor monitor) throws FileSystemException { FileObject fromFile = null; FileObject toFile = null;/*w ww . j a v a 2s . c o m*/ // clear authenticators setAuthenticator(fsOpts, null); setIdentityFactory(fsOpts, null); // loop through authenticators until we either succeed or cancel boolean cancel = false; while (toFile == null && cancel == false) { toFile = resolveRemote(to); } cancel = false; while (fromFile == null && cancel == false) { fromFile = resolveRemote(from); } if (fromFile == null || !fromFile.exists()) { throw new FileSystemException("Cannot find source file <" + from.toString() + ">", new FileNotFoundException("<" + from.toString() + ">")); } if (toFile == null) { throw new FileSystemException("Cannot find destination <" + to.toString() + ">", new FileNotFoundException("<" + to.toString() + ">")); } // monitor the file transfer progress if (monitor != null) { monitor.monitor(fromFile, toFile, -1, fromFile.getName().getBaseName()); monitor.start(); monitor.fireStartEvent(toFile); } // transfer content try { if (fromFile.isFile()) { toFile.copyFrom(fromFile, Selectors.SELECT_SELF); } else if (fromFile.isFolder()) { // final FileObject fileSystem = manager.createFileSystem(remoteFile); toFile.copyFrom(fromFile, new AllFileSelector()); // fileSystem.close(); } if (monitor != null) { monitor.fireCompleteEvent(toFile); } } catch (Exception e) { throw new FileTransferException( "Failed to complete transfer of " + fromFile.getURL() + " to " + toFile.getURL(), e); } finally { // close files if we need to fromFile.close(); toFile.close(); if (monitor != null) { monitor.release(toFile); monitor.stop(); } } return true; }
From source file:org.luwrain.app.commander.InfoAndProperties.java
static public long getTotalSize(FileObject fileObj) throws org.apache.commons.vfs2.FileSystemException { NullCheck.notNull(fileObj, "fileObj"); if (!fileObj.isFolder() && !fileObj.isFile()) return 0; if (fileObj instanceof org.apache.commons.vfs2.provider.local.LocalFile && java.nio.file.Files.isSymbolicLink(java.nio.file.Paths.get(fileObj.getName().getPath()))) return 0; if (!fileObj.isFolder()) return fileObj.getContent().getSize(); long res = 0; for (FileObject child : fileObj.getChildren()) res += getTotalSize(child);// w w w. ja v a2 s . com return res; }
From source file:org.pentaho.di.job.entries.googledrive.JobEntryGoogleDriveExport.java
protected void checkFolderExists(FileObject folder, boolean createIfNot) throws KettleException { if (folder == null) { return;/*from w ww .j a va 2 s. c om*/ } try { if (!folder.exists() && createIfNot) { if (log.isDetailed()) { log.logDetailed(BaseMessages.getString(PKG, "GoogleDriveExport.Log.CreatingTargetFolder")); } folder.createFolder(); return; } else { if (!folder.exists()) { throw new KettleException(BaseMessages.getString(PKG, "GoogleDriveExport.Error.FolderNotExist", folder.getName())); } else if (!folder.isFolder()) { throw new KettleException( BaseMessages.getString(PKG, "GoogleDriveExport.Error.NotAFolder", folder.getName())); } } } catch (FileSystemException e) { throw new KettleException(e); } }
From source file:org.pentaho.di.trans.steps.pentahoreporting.urlrepository.FileObjectContentLocation.java
/** * Creates a new location for the given parent and directory. * * @param parent the parent location./*from w ww. j a va 2 s . c om*/ * @param backend the backend. * @throws ContentIOException if an error occured or the file did not point to a directory. */ public FileObjectContentLocation(final ContentLocation parent, final FileObject backend) throws ContentIOException { super(parent, backend); boolean error; try { error = backend.exists() == false || backend.isFolder() == false; } catch (FileSystemException e) { throw new RuntimeException(e); } if (error) { throw new ContentIOException("The given backend-file is not a directory."); } }