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