Search in sources :

Example 16 with URIx

use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.

the class FileManager method putRemote.

/**
 * Uploads a local file to the remote destination if it exists. If dest is null or a
 * directory, appends source filename
 *
 * @param source
 * the source file
 * @param dest
 * the destination uri
 * @throws FileTransferException
 * if uploading the file fails
 */
public void putRemote(File source, URIx dest) throws FileTransferException {
    // ensure that source is a file, and not a directory
    if (source.isDirectory()) {
        // || srcFile.endsWith("/")) {
        throw new IllegalArgumentException("Source file must refer to a file: <" + source + ">");
    }
    if (dest == null) {
        // if source is relative, take that
        if (!source.isAbsolute()) {
            dest = new URIx(source.getPath());
        } else {
            // otherwise, simply extract the file name from source
            dest = new URIx(source.getName());
        }
    } else if (isDirectory(dest)) {
        if (source.isAbsolute()) {
            dest = new URIx(dest, source.getName());
        } else {
            dest = new URIx(dest, source.getPath());
        }
    }
    // make absolute
    dest = getAbsoluteURI(dest);
    source = getAbsoluteFile(source);
    try {
        cacher.initialize();
        logger.debug("Uploading file " + source.getAbsolutePath() + " to " + dest.toString() + "...");
        // download file
        cacher.copy(source, dest, myTransferMonitor);
    } catch (FileSystemException e) {
        String msg = decodeVFSMessage(e);
        throw new FileTransferException(msg, e);
    } finally {
        cacher.release();
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) URIx(maspack.fileutil.uri.URIx)

Example 17 with URIx

use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.

the class FileManager method get.

/**
 * Returns a file handle to a local version of the requested file. Downloads
 * from URI if required (according to options). Works with absolute paths and
 * source URIs, otherwise combines path and source URI with downloadDir and
 * remoteSource, respectively. If the destination is null or a directory,
 * then the filename of source is appended.
 *
 * If there is any internal problem, (such as failing to obtain a hash, or
 * failing to download a file), the function will log the error message and
 * continue.
 *
 * @param dest
 * the local path (relative or absolute) to download file to
 * @param source
 * the remote URI to cache
 * @param options
 * set of options, either FORCE_REMOTE or CHECK_HASH
 * @return File handle to local file
 * @throws FileTransferException only if there is no local copy of the file
 * at the end of the function call
 */
public File get(File dest, URIx source, int options) throws FileTransferException {
    // default destination if none provided
    if (dest == null) {
        if (source.isRelative()) {
            dest = new File(source.getPath(false));
        } else {
            dest = new File(extractFileName(source));
        }
    } else if (dest.isDirectory()) {
        if (source.isRelative()) {
            dest = new File(dest, source.getRawPath());
        } else {
            dest = new File(dest, extractFileName(source));
        }
    }
    // convert to absolute
    dest = getAbsoluteFile(dest);
    source = getAbsoluteURI(source);
    // download zip file first if requested
    if (source.isZip() && (options & DOWNLOAD_ZIP) != 0) {
        // get zip file
        URIx zipSource = source.getBaseURI();
        File zipDest = getAbsoluteFile(extractFileName(zipSource));
        File zipFile = get(zipDest, zipSource, options);
        // replace source URI
        source.setBaseURI(new URIx(zipFile));
    // XXX no longer need to check hash, since zip's hash would
    // have changed, although we do need to replace if re-downloaded zip
    // options = options & (~CHECK_HASH);
    }
    // check if we need to actually fetch file
    boolean fetch = true;
    if ((options & FORCE_REMOTE) == 0) {
        // check if file exists
        if (dest.canRead()) {
            // check hash if options say so
            if ((options & CHECK_HASH) != 0) {
                try {
                    fetch = !equalsHash(dest, source);
                    if (fetch) {
                        logger.debug("Hash matches");
                    }
                } catch (FileTransferException e) {
                    logger.debug("Cannot obtain hash, assuming it doesn't match, " + e.getMessage());
                    exceptionStack.add(e);
                    fetch = true;
                }
            } else {
                // file exists, so let it be
                fetch = false;
            }
        }
    }
    // download file if we need to
    if (fetch) {
        try {
            dest = getRemote(dest, source);
        } catch (FileTransferException e) {
            String msg = "Failed to fetch remote file <" + source + ">";
            logger.error(msg + ", " + e.getMessage());
            exceptionStack.add(e);
        }
    } else {
        logger.debug("File '" + dest + "' exists and does not need to be cached.");
    }
    // and we have no local copy
    if (dest == null || !dest.exists()) {
        String msg = "Unable to find or create file " + dest + " <" + source.toString() + ">";
        throw new FileTransferException(msg);
    }
    lastFile = dest;
    lastWasRemote = fetch;
    // return file
    return dest;
}
Also used : URIx(maspack.fileutil.uri.URIx) File(java.io.File)

Aggregations

URIx (maspack.fileutil.uri.URIx)17 File (java.io.File)11 FileSystemException (org.apache.commons.vfs2.FileSystemException)4 IOException (java.io.IOException)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 FileObject (org.apache.commons.vfs2.FileObject)2 JSchException (com.jcraft.jsch.JSchException)1 FileNotFoundException (java.io.FileNotFoundException)1 FileCacher (maspack.fileutil.FileCacher)1 URIxMatcher (maspack.fileutil.uri.URIxMatcher)1 PolygonalMesh (maspack.geometry.PolygonalMesh)1 WavefrontReader (maspack.geometry.io.WavefrontReader)1 AxisAngle (maspack.matrix.AxisAngle)1 RigidTransform3d (maspack.matrix.RigidTransform3d)1 Vector3d (maspack.matrix.Vector3d)1 AllFileSelector (org.apache.commons.vfs2.AllFileSelector)1