Search in sources :

Example 6 with Content

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

the class MongoContentStorageTest method testReadChunkedData.

public void testReadChunkedData() throws MongoException, IOException {
    byte[] data1 = "first chunk".getBytes();
    byte[] sha1 = DigestUtils.sha(data1);
    byte[] data2 = " second chunk".getBytes();
    byte[] sha2 = DigestUtils.sha(data2);
    byte[] data = "first chunk second chunk".getBytes();
    byte[] sha = DigestUtils.sha(data);
    prepareMockData("test.v7files.content", new BasicBSONObject("_id", sha1).append("in", data1));
    prepareMockData("test.v7files.content", new BasicBSONObject("_id", sha2).append("in", data2));
    prepareMockData("test.v7files.content", new BasicBSONObject("_id", sha).append("store", "cat").append("base", Arrays.asList(new BasicBSONObject("sha", sha1).append("length", data1.length), new BasicBSONObject("sha", sha2).append("length", data2.length))));
    Mongo mongo = getMongo();
    ContentStorage storage = new MongoContentStorage(mongo.getDB("test").getCollection("v7files.content"));
    Content check = storage.getContent(sha);
    assertEquals(new String(data), IOUtils.toString(check.getInputStream()));
    assertEquals(data.length, check.getLength());
    mongo.close();
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) Mongo(com.mongodb.Mongo) ContentStorage(v7db.files.spi.ContentStorage) Content(v7db.files.spi.Content)

Example 7 with Content

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

the class MongoContentStorageTest method testRoundtrip.

public void testRoundtrip() throws MongoException, IOException {
    Mongo mongo = getMongo();
    ContentStorage storage = new MongoContentStorage(mongo.getDB("test").getCollection("v7files.content"));
    byte[] data = "abcdefghijklmnopqrstuvwxyz".getBytes();
    ContentPointer pointer = storage.storeContent(new ByteArrayInputStream(data));
    Content check = storage.getContent(pointer);
    assertEquals(new String(data), IOUtils.toString(check.getInputStream()));
    assertEquals(data.length, check.getLength());
    mongo.close();
}
Also used : Mongo(com.mongodb.Mongo) ByteArrayInputStream(java.io.ByteArrayInputStream) ContentStorage(v7db.files.spi.ContentStorage) Content(v7db.files.spi.Content) ContentPointer(v7db.files.spi.ContentPointer)

Example 8 with Content

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

the class MongoContentStorage method findContentPointerByPrefix.

public ContentSHA findContentPointerByPrefix(byte[] shaPrefix) throws IOException {
    if (shaPrefix.length == 20) {
        DBObject file = contentCollection.findOne(shaPrefix);
        if (file == null)
            return null;
        Content c = getContent(file);
        return ContentSHA.forDigestAndLength(shaPrefix, c.getLength());
    }
    if (shaPrefix.length > 20)
        throw new IllegalArgumentException();
    // 0-padded
    byte[] lower = Arrays.copyOf(shaPrefix, 20);
    // FF-padded
    byte[] higher = Arrays.copyOf(shaPrefix, 20);
    for (int i = shaPrefix.length; i < higher.length; i++) {
        higher[i] = (byte) 0xFF;
    }
    List<DBObject> files = contentCollection.find(QueryUtils.between(_ID, lower, higher), new BasicDBObject()).limit(2).toArray();
    if (files.isEmpty())
        return null;
    if (files.size() == 1) {
        Content c = getContent(files.get(0));
        return ContentSHA.forDigestAndLength((byte[]) files.get(0).get(_ID), c.getLength());
    }
    throw new IllegalArgumentException(Hex.encodeHexString(shaPrefix) + " is not a unique SHA prefix");
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) InlineContent(v7db.files.spi.InlineContent) GzippedContent(v7db.files.spi.GzippedContent) Content(v7db.files.spi.Content) StoredContent(v7db.files.spi.StoredContent) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 9 with Content

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

the class MongoContentStorageTest method testReadCompressedData.

public void testReadCompressedData() throws MongoException, IOException {
    byte[] data = "some data we are going to store compressed with gzip".getBytes();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(baos);
    gzip.write(data);
    gzip.close();
    byte[] compressed = baos.toByteArray();
    byte[] sha = DigestUtils.sha(data);
    prepareMockData("test.v7files.content", new BasicBSONObject("_id", sha).append("store", "gz").append("zin", compressed));
    Mongo mongo = getMongo();
    ContentStorage storage = new MongoContentStorage(mongo.getDB("test").getCollection("v7files.content"));
    Content check = storage.getContent(sha);
    assertEquals(new String(data), IOUtils.toString(check.getInputStream()));
    assertEquals(data.length, check.getLength());
    mongo.close();
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) GZIPOutputStream(java.util.zip.GZIPOutputStream) Mongo(com.mongodb.Mongo) ContentStorage(v7db.files.spi.ContentStorage) Content(v7db.files.spi.Content) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 10 with Content

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

the class BucketsServlet method doFormPostGet.

private void doFormPostGet(HttpServletRequest request, HttpServletResponse response, BSONObject bucket, byte[] sha) throws IOException {
    BSONObject file = null;
    data: for (Object o : BSONUtils.values(bucket, "FormPost.data")) {
        BSONObject upload = (BSONObject) o;
        for (Object f : BSONUtils.values(upload, "files")) {
            BSONObject bf = (BSONObject) f;
            for (String fn : bf.keySet()) {
                BSONObject x = (BSONObject) bf.get(fn);
                byte[] theSha = getSha(x);
                if (Arrays.equals(theSha, sha)) {
                    file = x;
                    break data;
                }
            }
        }
    }
    if (file == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "Bucket '" + bucket.get("_id") + "' does not have a file matching digest '" + Hex.encodeHexString(sha) + "'");
        return;
    }
    Content content = storage.getContent(sha);
    if (content == null) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Bucket '" + bucket.get("_id") + "' has a file matching digest '" + Hex.encodeHexString(sha) + "', but it could not be found in the content storage");
        return;
    }
    String customFilename = request.getParameter("filename");
    if (StringUtils.isNotBlank(customFilename))
        file.put("filename", customFilename);
    sendFile(request, response, sha, file, content);
}
Also used : InlineContent(v7db.files.spi.InlineContent) Content(v7db.files.spi.Content) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject)

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