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]);
}
}
}
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();
}
}
}
}
Aggregations