Search in sources :

Example 11 with ContentSHA

use of v7db.files.spi.ContentSHA 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 12 with ContentSHA

use of v7db.files.spi.ContentSHA 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

ContentSHA (v7db.files.spi.ContentSHA)12 MongoContentStorage (v7db.files.mongodb.MongoContentStorage)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 BasicDBObject (com.mongodb.BasicDBObject)3 IOException (java.io.IOException)3 ContentPointer (v7db.files.spi.ContentPointer)3 InlineContent (v7db.files.spi.InlineContent)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 ArrayList (java.util.ArrayList)2 BSONObject (org.bson.BSONObject)2 BasicBSONObject (org.bson.BasicBSONObject)2 V7File (v7db.files.mongodb.V7File)2 V7GridFS (v7db.files.mongodb.V7GridFS)2 StoredContent (v7db.files.spi.StoredContent)2