Search in sources :

Example 1 with V7GridFS

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

the class MkdirCommand method main.

public static void main(String[] args) throws IOException {
    if (args.length != 3) {
        System.err.println("Create a directory/folder");
        System.err.println("  mkdir <root> <path>");
        System.exit(1);
    }
    DB db = Configuration.getMongo().getDB(Configuration.getProperty("mongo.db"));
    V7GridFS fs = new V7GridFS(db);
    String[] path = CopyCommand.getPath(args[1], args[2]);
    V7File existing = fs.getFile(path);
    if (existing != null) {
        if (existing.hasContent()) {
            throw new IOException(args[2] + " already exists (and is a file)");
        }
        throw new IOException("directory " + args[2] + " already exists");
    }
    V7File parent = CopyCommand.getParent(fs, path);
    fs.addFolder(parent.getId(), path[path.length - 1]);
}
Also used : V7File(v7db.files.mongodb.V7File) IOException(java.io.IOException) DB(com.mongodb.DB) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 2 with V7GridFS

use of v7db.files.mongodb.V7GridFS 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);
    }
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) V7File(v7db.files.mongodb.V7File) Content(v7db.files.spi.Content) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 3 with V7GridFS

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

the class CopyCommandTest method testCopyDirectory.

public void testCopyDirectory() throws IOException {
    V7GridFS fs = new V7GridFS(getMongo().getDB("test"));
    ObjectId dir = fs.addFolder("test", "folder");
    fs.addFile("test".getBytes(), dir, "test.txt", "text/plain");
    CopyCommand.main(new String[] { "copy", "test", "folder", "x", "copy" });
    V7File file = fs.getFile("x", "copy", "test.txt");
    assertEquals("test", IOUtils.toString(file.getInputStream()));
    assertEquals("text/plain", file.getContentType());
}
Also used : V7File(v7db.files.mongodb.V7File) ObjectId(org.bson.types.ObjectId) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 4 with V7GridFS

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

the class CopyCommandTest method testCopyFile.

public void testCopyFile() throws IOException {
    V7GridFS fs = new V7GridFS(getMongo().getDB("test"));
    ObjectId dir = fs.addFolder("test", "folder");
    fs.addFile("test".getBytes(), dir, "test.txt", "text/plain");
    CopyCommand.main(new String[] { "copy", "test", "folder/test.txt", "x", "copy.txt" });
    V7File file = fs.getFile("x", "copy.txt");
    assertEquals("test", IOUtils.toString(file.getInputStream()));
    assertEquals("text/plain", file.getContentType());
}
Also used : V7File(v7db.files.mongodb.V7File) ObjectId(org.bson.types.ObjectId) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 5 with V7GridFS

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

the class MkdirCommandTest method testExisting.

public void testExisting() throws IOException {
    V7GridFS fs = new V7GridFS(getMongo().getDB("test"));
    fs.addFolder("x", "123");
    try {
        MkdirCommand.main(new String[] { "mkdir", "x", "123" });
        fail("should have croaked on existing directory");
    } catch (IOException e) {
    }
}
Also used : IOException(java.io.IOException) V7GridFS(v7db.files.mongodb.V7GridFS)

Aggregations

V7GridFS (v7db.files.mongodb.V7GridFS)11 V7File (v7db.files.mongodb.V7File)8 IOException (java.io.IOException)3 MongoContentStorage (v7db.files.mongodb.MongoContentStorage)3 DB (com.mongodb.DB)2 DecoderException (org.apache.commons.codec.DecoderException)2 ObjectId (org.bson.types.ObjectId)2 ContentSHA (v7db.files.spi.ContentSHA)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Properties (java.util.Properties)1 BasicBSONObject (org.bson.BasicBSONObject)1 Content (v7db.files.spi.Content)1