Search in sources :

Example 6 with V7GridFS

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

the class ResourceFactory method init.

public void init(ApplicationConfig config, HttpManager manager) {
    try {
        endpoint = config.getInitParameter("webdav.endpoint");
        endpointName = defaultIfBlank(substringAfterLast(endpoint, "/"), "/");
        mongo = Configuration.getMongo();
        endpointProperties = new Properties(Configuration.getEndpointProperties(endpoint));
        // need to adjust mongo.db in case of multi-tenant mode
        endpointProperties.put("mongo.db", dbName);
        fs = new V7GridFS(mongo.getDB(dbName));
        ROOT = getProperty("root");
        if (ROOT == null)
            ROOT = endpoint;
        authentication = getAuthenticationProvider();
        authorisation = AuthorisationProviderFactory.getAuthorisationProvider(endpointProperties);
        fakeLocking = "fake".equals(getProperty("locking.provider"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Properties(java.util.Properties) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 7 with V7GridFS

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

the class V7GridFSTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    // the root folder
    prepareMockData("test.v7files.files", new BasicBSONObject("_id", "root"));
    gridFS = new V7GridFS(getMongo().getDB("test"));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 8 with V7GridFS

use of v7db.files.mongodb.V7GridFS 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 9 with V7GridFS

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

the class MkdirCommandTest method testSimple.

public void testSimple() throws IOException {
    V7GridFS fs = new V7GridFS(getMongo().getDB("test"));
    MkdirCommand.main(new String[] { "mkdir", "x", "123" });
    {
        V7File check = fs.getFile("x", "123");
        assertFalse(check.hasContent());
        assertEquals("[]", check.getChildren().toString());
    }
    MkdirCommand.main(new String[] { "mkdir", "x", "123/456" });
    {
        V7File check = fs.getFile("x", "123", "456");
        assertFalse(check.hasContent());
        assertEquals("[]", check.getChildren().toString());
    }
}
Also used : V7File(v7db.files.mongodb.V7File) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 10 with V7GridFS

use of v7db.files.mongodb.V7GridFS 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)

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