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