Search in sources :

Example 1 with Content

use of v7db.files.spi.Content in project v7files by thiloplanz.

the class ZipFile method index.

/**
 * index all individual files found in a zip archive already in storage
 *
 * @throws IOException
 */
public static final void index(ContentStorage storage, ContentPointer zipFile) throws IOException {
    Content zip = storage.getContent(zipFile);
    if (zip == null)
        throw new IllegalArgumentException("invalid ContentPointer " + zipFile);
    File tmp = File.createTempFile("v7files_zipfile_extractfile_", ".zip");
    try {
        OutputStream f = new FileOutputStream(tmp);
        IOUtils.copy(zip.getInputStream(), f);
        f.close();
        // open up the zip file
        HeaderReader r = new HeaderReader(new RandomAccessFile(tmp, "r"));
        ZipModel model = r.readAllHeaders();
        model.setZipFile(tmp.getAbsolutePath());
        Map<String, Object> map = zipFile.serialize();
        List<?> fhs = model.getCentralDirectory().getFileHeaders();
        for (Object _fh : fhs) {
            FileHeader fh = (FileHeader) _fh;
            UnzipEngine en = new UnzipEngine(model, fh);
            // this will read the local file header
            en.getInputStream();
            LocalFileHeader lh = en.getLocalFileHeader();
            store(storage, map, fh, lh);
        }
    } catch (ZipException e) {
        throw new IllegalArgumentException("ContentPointer does not refer to a zip file: " + zipFile, e);
    } finally {
        tmp.delete();
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) UnzipEngine(net.lingala.zip4j.unzip.UnzipEngine) ZipException(net.lingala.zip4j.exception.ZipException) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) ZipModel(net.lingala.zip4j.model.ZipModel) RandomAccessFile(java.io.RandomAccessFile) InlineContent(v7db.files.spi.InlineContent) Content(v7db.files.spi.Content) FileOutputStream(java.io.FileOutputStream) HeaderReader(net.lingala.zip4j.core.HeaderReader) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) FileHeader(net.lingala.zip4j.model.FileHeader)

Example 2 with Content

use of v7db.files.spi.Content in project v7files by thiloplanz.

the class ZipFile method extractFile.

/**
 * find the data indicated by the ContentPointer, treats it as a zip
 * archive, extracts the named file inside the archive, stores a reference
 * to it in the ContentStorage and returns a ContentPointer to it.
 *
 * @throws FileNotFoundException
 *             if the archive exists, but does not contain the named file
 * @throws IllegalArgumentException
 *             if the ContentPointer does not refer to a zip archive
 */
public static final ContentPointer extractFile(ContentStorage storage, ContentPointer zipFile, String fileName) throws IOException {
    Content zip = storage.getContent(zipFile);
    if (zip == null)
        throw new IllegalArgumentException("invalid ContentPointer " + zipFile);
    File tmp = File.createTempFile("v7files_zipfile_extractfile_", ".zip");
    try {
        OutputStream f = new FileOutputStream(tmp);
        IOUtils.copy(zip.getInputStream(), f);
        f.close();
        // open up the zip file
        HeaderReader r = new HeaderReader(new RandomAccessFile(tmp, "r"));
        ZipModel model = r.readAllHeaders();
        model.setZipFile(tmp.getAbsolutePath());
        List<?> fhs = model.getCentralDirectory().getFileHeaders();
        for (Object _fh : fhs) {
            FileHeader fh = (FileHeader) _fh;
            if (fileName.equals(fh.getFileName())) {
                UnzipEngine en = new UnzipEngine(model, fh);
                // this will read the local file header
                en.getInputStream();
                LocalFileHeader lh = en.getLocalFileHeader();
                return store(storage, zipFile.serialize(), fh, lh);
            }
        }
    } catch (ZipException e) {
        throw new IllegalArgumentException("ContentPointer does not refer to a zip file: " + zipFile, e);
    } finally {
        tmp.delete();
    }
    throw new FileNotFoundException("ContentPointer does not contain " + fileName + ": " + zipFile);
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) UnzipEngine(net.lingala.zip4j.unzip.UnzipEngine) FileNotFoundException(java.io.FileNotFoundException) ZipException(net.lingala.zip4j.exception.ZipException) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) ZipModel(net.lingala.zip4j.model.ZipModel) RandomAccessFile(java.io.RandomAccessFile) InlineContent(v7db.files.spi.InlineContent) Content(v7db.files.spi.Content) FileOutputStream(java.io.FileOutputStream) HeaderReader(net.lingala.zip4j.core.HeaderReader) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LocalFileHeader(net.lingala.zip4j.model.LocalFileHeader) FileHeader(net.lingala.zip4j.model.FileHeader)

Example 3 with Content

use of v7db.files.spi.Content in project v7files by thiloplanz.

the class BucketsServlet method doEchoPutGet.

private void doEchoPutGet(HttpServletRequest request, HttpServletResponse response, BSONObject bucket, byte[] sha) throws IOException {
    Content content = storage.getContent(sha);
    if (content == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "Bucket '" + bucket.get("_id") + "' does not have a file matching digest '" + Hex.encodeHexString(sha) + "'");
        return;
    }
    BSONObject file = new BasicBSONObject("sha", sha);
    String customFilename = request.getParameter("filename");
    if (StringUtils.isNotBlank(customFilename))
        file.put("filename", customFilename);
    sendFile(request, response, sha, file, content);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) InlineContent(v7db.files.spi.InlineContent) Content(v7db.files.spi.Content) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 4 with Content

use of v7db.files.spi.Content in project v7files by thiloplanz.

the class MongoContentStorage method getContent.

public Content getContent(ContentPointer pointer) throws IOException {
    if (pointer == null)
        return null;
    if (pointer instanceof InlineContent)
        return (Content) pointer;
    if (pointer instanceof ContentSHA) {
        ContentSHA p = (ContentSHA) pointer;
        byte[] sha = p.getSHA();
        Content base = getContent(sha);
        if (base == null)
            throw new IllegalArgumentException("base SHA not found: " + Hex.encodeHexString(sha));
        return base;
    }
    if (pointer instanceof StoredContent) {
        StoredContent p = (StoredContent) pointer;
        byte[] sha = p.getBaseSHA();
        Content base = getContent(sha);
        if (base == null)
            throw new IllegalArgumentException("base SHA not found: " + Hex.encodeHexString(sha));
        if (p.getLength() != base.getLength()) {
            return new OffsetAndLength(base, 0, p.getLength());
        }
        return base;
    }
    throw new IllegalArgumentException(pointer.getClass().toString());
}
Also used : OffsetAndLength(v7db.files.spi.OffsetAndLength) ContentSHA(v7db.files.spi.ContentSHA) InlineContent(v7db.files.spi.InlineContent) GzippedContent(v7db.files.spi.GzippedContent) Content(v7db.files.spi.Content) StoredContent(v7db.files.spi.StoredContent) InlineContent(v7db.files.spi.InlineContent) StoredContent(v7db.files.spi.StoredContent)

Example 5 with Content

use of v7db.files.spi.Content in project v7files by thiloplanz.

the class CatCommand method main.

public static void main(String[] args) throws MongoException, IOException {
    if (args.length != 3) {
        System.err.println("Output the contents of a file:");
        System.err.println("  by name:   cat <root> <path>");
        System.err.println("  by hash:   cat -sha <shaHex>");
        System.exit(1);
    }
    if ("-sha".equals(args[1])) {
        MongoContentStorage storage = new MongoContentStorage(Configuration.getMongo().getDB(Configuration.getProperty("mongo.db")));
        String sha = args[2];
        try {
            Content file = findContentByPrefix(storage, sha);
            if (file == null) {
                System.err.println("file not found");
                System.exit(1);
            }
            IOUtils.copy(file.getInputStream(), System.out);
        } catch (DecoderException e) {
            System.err.println("invalid parameter :" + sha + " is not a hex-encoded SHA-1 prefix");
            System.exit(1);
        }
    } else {
        V7GridFS fs = new V7GridFS(Configuration.getMongo().getDB(Configuration.getProperty("mongo.db")));
        String root = args[1];
        String path = args[2];
        String[] fullPath = ArrayUtils.add(StringUtils.split(path, '/'), 0, root);
        V7File file = fs.getFile(fullPath);
        if (file == null) {
            System.err.println("file not found");
            System.exit(1);
        }
        IOUtils.copy(file.getInputStream(), System.out);
    }
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) V7File(v7db.files.mongodb.V7File) Content(v7db.files.spi.Content) V7GridFS(v7db.files.mongodb.V7GridFS)

Aggregations

Content (v7db.files.spi.Content)10 InlineContent (v7db.files.spi.InlineContent)6 BasicBSONObject (org.bson.BasicBSONObject)4 Mongo (com.mongodb.Mongo)3 ContentStorage (v7db.files.spi.ContentStorage)3 BasicDBObject (com.mongodb.BasicDBObject)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 HeaderReader (net.lingala.zip4j.core.HeaderReader)2 ZipException (net.lingala.zip4j.exception.ZipException)2 FileHeader (net.lingala.zip4j.model.FileHeader)2 LocalFileHeader (net.lingala.zip4j.model.LocalFileHeader)2 ZipModel (net.lingala.zip4j.model.ZipModel)2 UnzipEngine (net.lingala.zip4j.unzip.UnzipEngine)2 BSONObject (org.bson.BSONObject)2 GzippedContent (v7db.files.spi.GzippedContent)2 StoredContent (v7db.files.spi.StoredContent)2 DBObject (com.mongodb.DBObject)1