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());
}
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);
}
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);
}
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);
}
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);
}
Aggregations