From 7f2b6e253037c6e5f946966583219b7662e5b826 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Tue, 24 Jan 2017 15:12:49 +0200 Subject: [PATCH] Initial commit --- .gitignore | 21 +++++++ pom.xml | 33 ++++++++++ .../java/eu/mikroskeem/mongohackery/Main.java | 62 +++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/eu/mikroskeem/mongohackery/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40d56b9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +# ---> Java +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# IntelliJ IDEA +*.iml +.idea/ + +# Maven +target/ +dependency-reduced-pom.xml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..998a4e4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + eu.mikroskeem + mongohackery + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + + + + org.mongodb + mongo-java-driver + 3.4.1 + + + + \ No newline at end of file diff --git a/src/main/java/eu/mikroskeem/mongohackery/Main.java b/src/main/java/eu/mikroskeem/mongohackery/Main.java new file mode 100644 index 0000000..6f920c6 --- /dev/null +++ b/src/main/java/eu/mikroskeem/mongohackery/Main.java @@ -0,0 +1,62 @@ +package eu.mikroskeem.mongohackery; + +import com.mongodb.MongoClient; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.gridfs.GridFSBucket; +import com.mongodb.client.gridfs.GridFSBuckets; +import com.mongodb.client.gridfs.model.GridFSUploadOptions; +import org.bson.Document; +import org.bson.types.ObjectId; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.stream.Collectors; + +import static com.mongodb.client.model.Filters.eq; + +public class Main { + public static void main(String... args){ + new Main(); + } + + public Main(){ + MongoClient client = new MongoClient(); + MongoDatabase leTest = client.getDatabase("leTest"); + GridFSBucket gridfs = GridFSBuckets.create(leTest, "files"); + + /* + * Ransom data + * Should use file here actually + */ + InputStream is = new ByteArrayInputStream("topkek".getBytes()); + + /* Remove (if present) */ + try { + gridfs.delete(gridfs.find(eq("filename", "topkek")).first().getId()); + } + catch(NullPointerException ignored){} + + /* Insert */ + GridFSUploadOptions options = new GridFSUploadOptions() + .chunkSizeBytes(1024) + .metadata(new Document("contentType", "text/plain")); + /* + * NOTE: 'contentType' isn't standard! It is here, because I want to see + * It is there, because I tend to use it with GridFS + */ + + ObjectId id = gridfs.uploadFromStream("topkek", is, options); + System.out.println(id); + + /* Now read */ + InputStream os = gridfs.openDownloadStream(gridfs.find(eq("filename", "topkek")).first().getId()); + String res = new BufferedReader(new InputStreamReader(os)) + .lines().collect(Collectors.joining("\n")); + System.out.println(res); + + /* Remove */ + gridfs.delete(gridfs.find(eq("filename", "topkek")).first().getId()); + } +}