Search in sources :

Example 61 with BSONObject

use of org.bson.BSONObject in project v7files by thiloplanz.

the class MongoContentStorageTest method testSaveAsChunks.

public void testSaveAsChunks() throws IOException {
    byte[] data = new byte[10 * 1024 * 1024];
    new Random(12345).nextBytes(data);
    byte[] sha = DigestUtils.sha(data);
    Mongo mongo = getMongo();
    MongoContentStorage storage = new MongoContentStorage(mongo.getDB("test"));
    ContentPointer pointer = storage.storeContent(new ByteArrayInputStream(data));
    BSONObject doc = assertMockMongoContainsDocument("test.v7files.content", sha);
    assertEquals("cat", doc.get("store"));
    assertEquals(data.length, storage.getContent(pointer).getLength());
    assertEquals(Hex.encodeHexString(sha), DigestUtils.shaHex(storage.getContent(pointer).getInputStream()));
    ContentSHA storeAgain = storage.storeContent(new ByteArrayInputStream(data));
    assertEquals(Hex.encodeHexString(sha), storeAgain.getDigest());
}
Also used : Random(java.util.Random) Mongo(com.mongodb.Mongo) ByteArrayInputStream(java.io.ByteArrayInputStream) ContentSHA(v7db.files.spi.ContentSHA) ContentPointer(v7db.files.spi.ContentPointer) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 62 with BSONObject

use of org.bson.BSONObject in project v7files by thiloplanz.

the class V7GridFS method updateContents.

private void updateContents(DBObject metaData, InputStream contents) throws IOException {
    Object fileId = metaData.get("_id");
    ContentPointer oldContents = getContentPointer(metaData);
    String filename = (String) metaData.get("filename");
    String contentType = (String) metaData.get("contentType");
    BSONObject newContent = storage.insertContentsAndBackRefs(contents, fileId, filename, contentType);
    // check if it has changed
    ContentPointer newContents = getContentPointer(newContent);
    if (newContents.contentEquals(oldContents))
        return;
    metaData.removeField("sha");
    metaData.removeField("length");
    metaData.removeField("in");
    metaData.putAll(newContent);
    updateMetaData(metaData);
}
Also used : ContentPointer(v7db.files.spi.ContentPointer) BSONObject(org.bson.BSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject)

Example 63 with BSONObject

use of org.bson.BSONObject in project v7files by thiloplanz.

the class V7GridFS method updateContents.

private void updateContents(DBObject metaData, byte[] contents, int offset, int len) throws IOException {
    Object fileId = metaData.get("_id");
    ContentPointer oldContents = getContentPointer(metaData);
    String filename = (String) metaData.get("filename");
    String contentType = (String) metaData.get("contentType");
    // for up to 55 bytes, storing the complete file inline
    // takes less space than just storing the SHA-1 and length
    // 20 (SHA-1) + 1 (sha - in) + 6 (length) + 4 (int32) + 2*12
    // (ObjectId back-references)
    BSONObject newContent = storage.inlineOrInsertContentsAndBackRefs(55, contents, offset, len, fileId, filename, contentType);
    // check if it has changed
    ContentPointer newContents = getContentPointer(newContent);
    if (newContents.contentEquals(oldContents))
        return;
    metaData.removeField("sha");
    metaData.removeField("length");
    metaData.removeField("in");
    metaData.putAll(newContent);
    updateMetaData(metaData);
}
Also used : ContentPointer(v7db.files.spi.ContentPointer) BSONObject(org.bson.BSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject)

Example 64 with BSONObject

use of org.bson.BSONObject in project v7files by thiloplanz.

the class V7GridFS method insertContents.

void insertContents(DBObject metaData, ContentPointer newContents) throws IOException {
    String filename = (String) metaData.get("filename");
    String contentType = (String) metaData.get("contentType");
    Object fileId = metaData.get("_id");
    if (newContents != null) {
        BSONObject newContent = storage.updateBackRefs(newContents, fileId, filename, contentType);
        metaData.removeField("sha");
        metaData.removeField("length");
        metaData.removeField("in");
        metaData.putAll(newContent);
    }
    insertMetaData(metaData);
}
Also used : BSONObject(org.bson.BSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject)

Example 65 with BSONObject

use of org.bson.BSONObject 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

BSONObject (org.bson.BSONObject)101 BasicBSONObject (org.bson.BasicBSONObject)49 Test (org.junit.Test)34 BasicDBObject (com.mongodb.BasicDBObject)19 SerializableString (com.fasterxml.jackson.core.SerializableString)14 SerializedString (com.fasterxml.jackson.core.io.SerializedString)14 LinkedHashMap (java.util.LinkedHashMap)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)11 DBObject (com.mongodb.DBObject)10 ArrayList (java.util.ArrayList)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Map (java.util.Map)6 BasicBSONDecoder (org.bson.BasicBSONDecoder)6 BSONDecoder (org.bson.BSONDecoder)5 BSONEncoder (org.bson.BSONEncoder)5 BasicBSONEncoder (org.bson.BasicBSONEncoder)5 LazyBSONObject (org.bson.LazyBSONObject)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 BSONFileSplit (com.mongodb.hadoop.input.BSONFileSplit)4