use of v7db.files.mongodb.MongoContentStorage 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);
}
}
use of v7db.files.mongodb.MongoContentStorage in project v7files by thiloplanz.
the class ZipFileTest method testPullOutFileFromZip.
public void testPullOutFileFromZip() throws MongoException, IOException, ZipException, DecoderException {
ContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
ContentPointer zip = storage.storeContent(getClass().getResourceAsStream("mongodb.epub"));
ContentPointer png = ZipFile.extractFile(storage, zip, "images/img0.png");
assertEquals("fc012bb0439382f709d3caebab958ff592811d17", DigestUtils.shaHex(storage.getContent(png).getInputStream()));
}
use of v7db.files.mongodb.MongoContentStorage in project v7files by thiloplanz.
the class BucketsServletTest method testEchoPutGET.
public void testEchoPutGET() throws IOException, SAXException {
ServletUnitClient sc = sr.newClient();
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", "1234");
try {
sc.getResponse(request);
fail("bucket not found => 404");
} catch (HttpNotFoundException e) {
assertEquals("Bucket '1' not found", e.getResponseMessage());
}
}
prepareBucket("1", "EchoPut", null, null);
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", "1234");
try {
sc.getResponse(request);
fail("bucket not found => 404");
} catch (HttpNotFoundException e) {
assertEquals("Bucket '1' does not have a file matching digest '1234'", e.getResponseMessage());
}
}
MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
request.setParameter("filename", "a.txt");
WebResponse response = sc.getResponse(request);
assertEquals("test", response.getText());
assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
assertEquals(4, response.getContentLength());
assertEquals("attachment; filename=\"a.txt\"", response.getHeaderField("Content-Disposition"));
}
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
request.setHeaderField("If-None-Match", sha.getDigest());
WebResponse response = sc.getResponse(request);
assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getResponseCode());
}
}
use of v7db.files.mongodb.MongoContentStorage in project v7files by thiloplanz.
the class ZipFileTest method testIndexZipFile.
public void testIndexZipFile() throws MongoException, IOException, ZipException, DecoderException {
ContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
ContentPointer zip = storage.storeContent(getClass().getResourceAsStream("mongodb.epub"));
ZipFile.index(storage, zip);
assertEquals("fc012bb0439382f709d3caebab958ff592811d17", DigestUtils.shaHex(storage.getContent(decodeHex("fc012bb0439382f709d3caebab958ff592811d17".toCharArray())).getInputStream()));
}
use of v7db.files.mongodb.MongoContentStorage in project v7files by thiloplanz.
the class BucketsServletTest method testFormPostGET.
public void testFormPostGET() throws IOException, SAXException {
BasicBSONObject bucket = prepareBucket("1", "FormPost", null, null);
MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
ServletUnitClient sc = sr.newClient();
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
try {
sc.getResponse(request);
fail("bucket not found => 404");
} catch (HttpNotFoundException e) {
assertEquals(String.format("Bucket '1' does not have a file matching digest '%s'", sha.getDigest()), e.getResponseMessage());
}
}
bucket.append("FormPost", new BasicBSONObject("data", new BasicBSONObject("files", new BasicBSONObject("file", new BasicBSONObject("filename", "a.txt").append("sha", sha.getSHA())))));
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
WebResponse response = sc.getResponse(request);
assertEquals("test", response.getText());
assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
assertEquals(4, response.getContentLength());
assertEquals("attachment; filename=\"a.txt\"", response.getHeaderField("Content-Disposition"));
}
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
request.setParameter("filename", "x.txt");
WebResponse response = sc.getResponse(request);
assertEquals("test", response.getText());
assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
assertEquals(4, response.getContentLength());
assertEquals("attachment; filename=\"x.txt\"", response.getHeaderField("Content-Disposition"));
}
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
request.setHeaderField("If-None-Match", sha.getDigest());
WebResponse response = sc.getResponse(request);
assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getResponseCode());
}
}
Aggregations