Search in sources :

Example 1 with MimeTypeResult

use of org.webpieces.router.impl.compression.MimeTypes.MimeTypeResult in project webpieces by deanhiller.

the class ProdCompressionCacheSetup method maybeAddFileToCache.

private boolean maybeAddFileToCache(Properties properties, File src, File destination, String urlPath) {
    String name = src.getName();
    int indexOf = name.lastIndexOf(".");
    if (indexOf < 0) {
        pathToFileMeta.put(urlPath, new FileMeta());
        //do nothing
        return false;
    }
    String extension = name.substring(indexOf + 1);
    MimeTypeResult mimeType = mimeTypes.extensionToContentType(extension, "application/octet-stream");
    Compression compression = lookup.createCompressionStream(encodings, extension, mimeType);
    if (compression == null) {
        pathToFileMeta.put(urlPath, new FileMeta());
        return false;
    }
    //before we do the below, do a quick timestamp check to avoid reading in the files when not necessary
    long lastModifiedSrc = src.lastModified();
    long lastModified = destination.lastModified();
    //if hash is not there, the user may have changed the url so need to recalculate new hashes for new keys
    //There is a test for this...
    String previousHash = properties.getProperty(urlPath);
    if (lastModified > lastModifiedSrc && previousHash != null) {
        log.info("timestamp later than src so skipping writing to=" + destination);
        pathToFileMeta.put(urlPath, new FileMeta(previousHash));
        //no need to check anything as destination was written after this source file
        return false;
    }
    try {
        byte[] allData = fileUtil.readFileContents(urlPath, src);
        String hash = Security.hash(allData);
        if (previousHash != null) {
            if (hash.equals(previousHash)) {
                if (!destination.exists())
                    throw new IllegalStateException("Previously existing file is missing=" + destination + " Your file cache was " + "corrupted.  You will need to delete the whole cache directory");
                log.info("Previous file is the same, no need to compress to=" + destination + " hash=" + hash);
                pathToFileMeta.put(urlPath, new FileMeta(previousHash));
                return false;
            }
        }
        //open, write, and close file with new data
        writeFile(destination, compression, allData, urlPath, src);
        //if file writing succeeded, set the hash
        properties.setProperty(urlPath, hash);
        FileMeta existing = pathToFileMeta.get(urlPath);
        if (existing != null)
            throw new IllegalStateException("this urlpath=" + urlPath + " is referencing two files.  hash1=" + existing.getHash() + " hash2=" + hash + "  You should search your logs for this hash");
        pathToFileMeta.put(urlPath, new FileMeta(hash));
        log.info("compressed " + src.length() + " bytes to=" + destination.length() + " to file=" + destination + " hash=" + hash);
        return true;
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : MimeTypeResult(org.webpieces.router.impl.compression.MimeTypes.MimeTypeResult) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 2 with MimeTypeResult

use of org.webpieces.router.impl.compression.MimeTypes.MimeTypeResult in project webpieces by deanhiller.

the class ProdCompressionCacheSetup method maybeAddFileToCache.

private boolean maybeAddFileToCache(Properties properties, VirtualFile src, File destination, String urlPath) {
    String name = src.getName();
    int indexOf = name.lastIndexOf(".");
    if (indexOf < 0) {
        pathToFileMeta.put(urlPath, new FileMeta());
        // do nothing
        return false;
    }
    String extension = name.substring(indexOf + 1);
    MimeTypeResult mimeType = mimeTypes.extensionToContentType(extension, "application/octet-stream");
    Compression compression = lookup.createCompressionStream(encodings, mimeType);
    if (compression == null) {
        pathToFileMeta.put(urlPath, new FileMeta());
        return false;
    }
    // before we do the below, do a quick timestamp check to avoid reading in the files when not necessary
    long lastModifiedSrc = src.lastModified();
    long lastModified = destination.lastModified();
    // if hash is not there, the user may have changed the url so need to recalculate new hashes for new keys
    // There is a test for this...
    String previousHash = properties.getProperty(urlPath);
    if (lastModified > lastModifiedSrc && previousHash != null) {
        if (log.isDebugEnabled())
            log.debug("timestamp later than src so skipping writing to=" + destination);
        pathToFileMeta.put(urlPath, new FileMeta(previousHash));
        // no need to check anything as destination was written after this source file
        return false;
    }
    try {
        byte[] allData = fileUtil.readFileContents(urlPath, src);
        String hash = Security.hash(allData);
        if (previousHash != null) {
            if (hash.equals(previousHash)) {
                if (!destination.exists())
                    throw new IllegalStateException("Previously existing file is missing=" + destination + " Your file cache was " + "corrupted.  You will need to delete the whole cache directory");
                log.info("Previous file is the same, no need to compress to=" + destination + " hash=" + hash);
                pathToFileMeta.put(urlPath, new FileMeta(previousHash));
                return false;
            }
        }
        // open, write, and close file with new data
        writeFile(destination, compression, allData, urlPath, src);
        // if file writing succeeded, set the hash
        properties.setProperty(urlPath, hash);
        FileMeta existing = pathToFileMeta.get(urlPath);
        if (existing != null)
            throw new IllegalStateException("this urlpath=" + urlPath + " is referencing two files.  hash1=" + existing.getHash() + " hash2=" + hash + "  You should search your logs for this hash");
        pathToFileMeta.put(urlPath, new FileMeta(hash));
        log.info("compressed " + src.length() + " bytes to=" + destination.length() + " to file=" + destination + " hash=" + hash);
        return true;
    } catch (IOException e) {
        throw SneakyThrow.sneak(e);
    }
}
Also used : MimeTypeResult(org.webpieces.router.impl.compression.MimeTypes.MimeTypeResult) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 MimeTypeResult (org.webpieces.router.impl.compression.MimeTypes.MimeTypeResult)2 FileNotFoundException (java.io.FileNotFoundException)1