Search in sources :

Example 6 with MongoContentStorage

use of v7db.files.mongodb.MongoContentStorage in project v7files by thiloplanz.

the class CatCommandTest method testSha.

public void testSha() throws IOException {
    MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
    ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
    CatCommand.main(new String[] { "cat", "-sha", sha.getDigest() });
    System.out.flush();
    assertEquals("test", out.toString());
}
Also used : MongoContentStorage(v7db.files.mongodb.MongoContentStorage) ContentSHA(v7db.files.spi.ContentSHA) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 7 with MongoContentStorage

use of v7db.files.mongodb.MongoContentStorage in project v7files by thiloplanz.

the class CopyCommandTest method testSHA.

public void testSHA() throws IOException {
    MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
    ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
    CopyCommand.main(new String[] { "copy", "-sha", sha.getDigest(), "x", "copy.txt" });
    V7GridFS fs = new V7GridFS(getMongo().getDB("test"));
    V7File file = fs.getFile("x", "copy.txt");
    assertEquals("test", IOUtils.toString(file.getInputStream()));
}
Also used : MongoContentStorage(v7db.files.mongodb.MongoContentStorage) V7File(v7db.files.mongodb.V7File) ContentSHA(v7db.files.spi.ContentSHA) ByteArrayInputStream(java.io.ByteArrayInputStream) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 8 with MongoContentStorage

use of v7db.files.mongodb.MongoContentStorage in project v7files by thiloplanz.

the class CopyCommand method main.

public static void main(String[] args) throws MongoException, IOException {
    if (args.length != 5) {
        System.err.println("Copy files");
        System.err.println("  by name:   copy <sourceRoot> <sourcePath> <targetRoot> <targetPath>");
        System.err.println("  by hash:   copy -sha <shaHex> <targetRoot> <targetPath>");
        System.exit(1);
    }
    DB db = Configuration.getMongo().getDB(Configuration.getProperty("mongo.db"));
    V7GridFS fs = new V7GridFS(db);
    if ("-sha".equals(args[1])) {
        MongoContentStorage storage = new MongoContentStorage(db);
        String sha = args[2];
        try {
            ContentSHA file = findContentByPrefix(storage, sha);
            if (file == null)
                throw new FileNotFoundException("-sha " + sha);
            String[] path = getPath(args[3], args[4]);
            createFile(fs, file, path, null);
        } catch (DecoderException e) {
            throw new IllegalArgumentException("invalid parameter :" + sha + " is not a hex-encoded SHA-1 prefix");
        }
    } else {
        String[] srcPath = getPath(args[1], args[2]);
        String[] targetPath = getPath(args[3], args[4]);
        V7File src = fs.getFile(srcPath);
        if (src == null) {
            throw new FileNotFoundException(args[1] + " " + args[2]);
        }
        if (src.hasContent()) {
            createFile(fs, src.getContentPointer(), targetPath, src.getContentType());
        } else {
            V7File existing = fs.getFile(targetPath);
            if (existing != null) {
                throw new IOException("copy target " + targetPath + " already exists");
            }
            V7File parent = getParent(fs, targetPath);
            src.copyTo(parent.getId(), targetPath[targetPath.length - 1]);
        }
    }
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) V7File(v7db.files.mongodb.V7File) ContentSHA(v7db.files.spi.ContentSHA) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DB(com.mongodb.DB) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 9 with MongoContentStorage

use of v7db.files.mongodb.MongoContentStorage in project v7files by thiloplanz.

the class UploadCommand method main.

public static void main(String[] args) throws MongoException, IOException {
    if (args.length < 2) {
        System.err.println("Upload the contents of one or more files and print their SHA digests:");
        System.err.println("   upload <file> [file] [file] [...]");
        System.exit(1);
    }
    ContentStorage storage = new MongoContentStorage(Configuration.getMongo().getDB(Configuration.getProperty("mongo.db")));
    for (int i = 1; i < args.length; i++) {
        File f = new File(args[i]);
        if (f.isFile() && f.canRead()) {
            try {
                ContentSHA up = storage.storeContent(new FileInputStream(f));
                // TODO: display if chunked or not
                System.out.format("-      %10d %80s %40s\n", f.length(), f.getName(), up.getDigest());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : MongoContentStorage(v7db.files.mongodb.MongoContentStorage) ContentSHA(v7db.files.spi.ContentSHA) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) ContentStorage(v7db.files.spi.ContentStorage) File(java.io.File) FileInputStream(java.io.FileInputStream) MongoException(com.mongodb.MongoException) IOException(java.io.IOException)

Aggregations

MongoContentStorage (v7db.files.mongodb.MongoContentStorage)9 ContentSHA (v7db.files.spi.ContentSHA)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 V7File (v7db.files.mongodb.V7File)3 V7GridFS (v7db.files.mongodb.V7GridFS)3 ContentStorage (v7db.files.spi.ContentStorage)3 GetMethodWebRequest (com.meterware.httpunit.GetMethodWebRequest)2 HttpNotFoundException (com.meterware.httpunit.HttpNotFoundException)2 PostMethodWebRequest (com.meterware.httpunit.PostMethodWebRequest)2 PutMethodWebRequest (com.meterware.httpunit.PutMethodWebRequest)2 WebRequest (com.meterware.httpunit.WebRequest)2 WebResponse (com.meterware.httpunit.WebResponse)2 ServletUnitClient (com.meterware.servletunit.ServletUnitClient)2 IOException (java.io.IOException)2 DecoderException (org.apache.commons.codec.DecoderException)2 ContentPointer (v7db.files.spi.ContentPointer)2 DB (com.mongodb.DB)1 MongoException (com.mongodb.MongoException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1