parent
a6544a1617
commit
6e43328941
3 changed files with 122 additions and 69 deletions
16
ChangeLog.md
16
ChangeLog.md
|
|
@ -2,7 +2,21 @@
|
||||||
|
|
||||||
### GridFS support improvements
|
### GridFS support improvements
|
||||||
|
|
||||||
`monger.gridfs/find-maps` and `monger.gridfs/find-one-as-map` were added. They serve the same purposes as `monger.collection/find-maps` and
|
Monger finally has a higher-level DSL for storing files on GridFS
|
||||||
|
|
||||||
|
``` clojure
|
||||||
|
(ns my.service
|
||||||
|
(:use [monger.gridfs :only [store-file make-input-file filename content-type metadata]]))
|
||||||
|
|
||||||
|
;; store a file from a local FS path with the given filename, content type and metadata
|
||||||
|
(store-file (make-input-file "/path/to/a/local/file.png")
|
||||||
|
(filename "image.png")
|
||||||
|
(metadata {:format "png"})
|
||||||
|
(content-type "image/png"))
|
||||||
|
```
|
||||||
|
|
||||||
|
There are also querying improvements: `monger.gridfs/find-maps` and `monger.gridfs/find-one-as-map` are new functions that were added.
|
||||||
|
They serve the same purposes as `monger.collection/find-maps` and
|
||||||
`monger.collection/find-one-as-map`, making it easy to work with Clojure data structures all the time.
|
`monger.collection/find-one-as-map`, making it easy to work with Clojure data structures all the time.
|
||||||
|
|
||||||
`monger.gridfs/files-as-maps` works the same way as `monger.gridfs/all-files` but returns results as Clojure maps. It is to
|
`monger.gridfs/files-as-maps` works the same way as `monger.gridfs/all-files` but returns results as Clojure maps. It is to
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,11 @@
|
||||||
([^GridFS fs query]
|
([^GridFS fs query]
|
||||||
(map converter (all-files fs (to-db-object query)))))
|
(map converter (all-files fs (to-db-object query)))))
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; Plumbing (low-level API)
|
||||||
|
;;
|
||||||
|
|
||||||
(defprotocol GridFSInputFileFactory
|
(defprotocol GridFSInputFileFactory
|
||||||
(^com.mongodb.gridfs.GridFSInputFile make-input-file [input] "Makes GridFSInputFile out of the given input"))
|
(^com.mongodb.gridfs.GridFSInputFile make-input-file [input] "Makes GridFSInputFile out of the given input"))
|
||||||
|
|
||||||
|
|
@ -98,6 +103,40 @@
|
||||||
(from-db-object f# true)))
|
(from-db-object f# true)))
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; "New" DSL, a higher-level API
|
||||||
|
;;
|
||||||
|
|
||||||
|
(defn save
|
||||||
|
[^GridFSInputFile input]
|
||||||
|
(.save input GridFS/DEFAULT_CHUNKSIZE)
|
||||||
|
(from-db-object input true))
|
||||||
|
|
||||||
|
(defn filename
|
||||||
|
[^GridFSInputFile input ^String s]
|
||||||
|
(.setFilename input s)
|
||||||
|
input)
|
||||||
|
|
||||||
|
(defn content-type
|
||||||
|
[^GridFSInputFile input ^String s]
|
||||||
|
(.setContentType input s)
|
||||||
|
input)
|
||||||
|
|
||||||
|
(defn metadata
|
||||||
|
[^GridFSInputFile input md]
|
||||||
|
(.setMetaData input (to-db-object md))
|
||||||
|
input)
|
||||||
|
|
||||||
|
(defmacro store-file
|
||||||
|
[^GridFSInputFile input & body]
|
||||||
|
`(let [f# (-> ~input ~@body)]
|
||||||
|
(save f#)))
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; Finders
|
||||||
|
;;
|
||||||
|
|
||||||
(defprotocol Finders
|
(defprotocol Finders
|
||||||
(find [input] "Finds multiple files using given input (an ObjectId, filename or query)")
|
(find [input] "Finds multiple files using given input (an ObjectId, filename or query)")
|
||||||
(find-one [input] "Finds one file using given input (an ObjectId, filename or query)")
|
(find-one [input] "Finds one file using given input (an ObjectId, filename or query)")
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
[monger.core :only [count]]
|
[monger.core :only [count]]
|
||||||
monger.test.fixtures
|
monger.test.fixtures
|
||||||
[monger operators conversion]
|
[monger operators conversion]
|
||||||
[monger.gridfs :only (store make-input-file)])
|
[monger.gridfs :only (store make-input-file store-file filename content-type metadata)])
|
||||||
(:require [monger.gridfs :as gridfs]
|
(:require [monger.gridfs :as gridfs]
|
||||||
[monger.test.helper :as helper]
|
[monger.test.helper :as helper]
|
||||||
[clojure.java.io :as io])
|
[clojure.java.io :as io])
|
||||||
|
|
@ -39,24 +39,24 @@
|
||||||
(deftest ^{:gridfs true} test-storing-files-to-gridfs-using-file-instances
|
(deftest ^{:gridfs true} test-storing-files-to-gridfs-using-file-instances
|
||||||
(let [input (io/as-file "./test/resources/mongo/js/mapfun1.js")]
|
(let [input (io/as-file "./test/resources/mongo/js/mapfun1.js")]
|
||||||
(is (= 0 (count (gridfs/all-files))))
|
(is (= 0 (count (gridfs/all-files))))
|
||||||
(store (make-input-file input)
|
(store-file (make-input-file input)
|
||||||
(.setFilename "monger.test.gridfs.file2")
|
(filename "monger.test.gridfs.file2")
|
||||||
(.setContentType "application/octet-stream"))
|
(content-type "application/octet-stream"))
|
||||||
(is (= 1 (count (gridfs/all-files))))))
|
(is (= 1 (count (gridfs/all-files))))))
|
||||||
|
|
||||||
(deftest ^{:gridfs true} test-storing-bytes-to-gridfs
|
(deftest ^{:gridfs true} test-storing-bytes-to-gridfs
|
||||||
(let [input (.getBytes "A string")
|
(let [input (.getBytes "A string")
|
||||||
md {:format "raw" :source "AwesomeCamera D95"}
|
md {:format "raw" :source "AwesomeCamera D95"}
|
||||||
filename "monger.test.gridfs.file3"
|
fname "monger.test.gridfs.file3"
|
||||||
ct "application/octet-stream"]
|
ct "application/octet-stream"]
|
||||||
(is (= 0 (count (gridfs/all-files))))
|
(is (= 0 (count (gridfs/all-files))))
|
||||||
(store (make-input-file input)
|
(store-file (make-input-file input)
|
||||||
(.setFilename filename)
|
(filename fname)
|
||||||
(.setMetaData (to-db-object md))
|
(metadata md)
|
||||||
(.setContentType "application/octet-stream"))
|
(content-type "application/octet-stream"))
|
||||||
(let [f (first (gridfs/files-as-maps))]
|
(let [f (first (gridfs/files-as-maps))]
|
||||||
(is (= ct (:contentType f)))
|
(is (= ct (:contentType f)))
|
||||||
(is (= filename (:filename f)))
|
(is (= fname (:filename f)))
|
||||||
(is (= md (:metadata f))))
|
(is (= md (:metadata f))))
|
||||||
(is (= 1 (count (gridfs/all-files))))))
|
(is (= 1 (count (gridfs/all-files))))))
|
||||||
|
|
||||||
|
|
@ -65,18 +65,18 @@
|
||||||
_ (spit tmp-file "Some content")
|
_ (spit tmp-file "Some content")
|
||||||
input (.getAbsolutePath tmp-file)]
|
input (.getAbsolutePath tmp-file)]
|
||||||
(is (= 0 (count (gridfs/all-files))))
|
(is (= 0 (count (gridfs/all-files))))
|
||||||
(store (make-input-file input)
|
(store-file (make-input-file input)
|
||||||
(.setFilename "monger.test.gridfs.file4")
|
(filename "monger.test.gridfs.file4")
|
||||||
(.setContentType "application/octet-stream"))
|
(content-type "application/octet-stream"))
|
||||||
(is (= 1 (count (gridfs/all-files))))))
|
(is (= 1 (count (gridfs/all-files))))))
|
||||||
|
|
||||||
(deftest ^{:gridfs true} test-storing-files-to-gridfs-using-input-stream
|
(deftest ^{:gridfs true} test-storing-files-to-gridfs-using-input-stream
|
||||||
(let [tmp-file (File/createTempFile "monger.test.gridfs" "test-storing-files-to-gridfs-using-input-stream")
|
(let [tmp-file (File/createTempFile "monger.test.gridfs" "test-storing-files-to-gridfs-using-input-stream")
|
||||||
_ (spit tmp-file "Some other content")]
|
_ (spit tmp-file "Some other content")]
|
||||||
(is (= 0 (count (gridfs/all-files))))
|
(is (= 0 (count (gridfs/all-files))))
|
||||||
(store (make-input-file (FileInputStream. tmp-file))
|
(store-file (make-input-file (FileInputStream. tmp-file))
|
||||||
(.setFilename "monger.test.gridfs.file4b")
|
(filename "monger.test.gridfs.file4b")
|
||||||
(.setContentType "application/octet-stream"))
|
(content-type "application/octet-stream"))
|
||||||
(is (= 1 (count (gridfs/all-files))))))
|
(is (= 1 (count (gridfs/all-files))))))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -86,56 +86,56 @@
|
||||||
(purge-gridfs*)
|
(purge-gridfs*)
|
||||||
(let [input "./test/resources/mongo/js/mapfun1.js"
|
(let [input "./test/resources/mongo/js/mapfun1.js"
|
||||||
ct "binary/octet-stream"
|
ct "binary/octet-stream"
|
||||||
filename "monger.test.gridfs.file5"
|
fname "monger.test.gridfs.file5"
|
||||||
md5 "14a09deabb50925a3381315149017bbd"
|
md5 "14a09deabb50925a3381315149017bbd"
|
||||||
stored (store (make-input-file input)
|
stored (store-file (make-input-file input)
|
||||||
(.setFilename filename)
|
(filename fname)
|
||||||
(.setContentType ct))]
|
(content-type ct))]
|
||||||
(is (= 1 (count (gridfs/all-files))))
|
(is (= 1 (count (gridfs/all-files))))
|
||||||
(is (:_id stored))
|
(is (:_id stored))
|
||||||
(is (:uploadDate stored))
|
(is (:uploadDate stored))
|
||||||
(is (= 62 (:length stored)))
|
(is (= 62 (:length stored)))
|
||||||
(is (= md5 (:md5 stored)))
|
(is (= md5 (:md5 stored)))
|
||||||
(is (= filename (:filename stored)))
|
(is (= fname (:filename stored)))
|
||||||
(is (= ct (:contentType stored)))
|
(is (= ct (:contentType stored)))
|
||||||
(are [a b] (is (= a (:md5 (from-db-object (gridfs/find-one b) true))))
|
(are [a b] (is (= a (:md5 (from-db-object (gridfs/find-one b) true))))
|
||||||
md5 (:_id stored)
|
md5 (:_id stored)
|
||||||
md5 filename
|
md5 fname
|
||||||
md5 (to-db-object {:md5 md5}))))
|
md5 (to-db-object {:md5 md5}))))
|
||||||
(testing "gridfs/find-one-as-map"
|
(testing "gridfs/find-one-as-map"
|
||||||
(purge-gridfs*)
|
(purge-gridfs*)
|
||||||
(let [input "./test/resources/mongo/js/mapfun1.js"
|
(let [input "./test/resources/mongo/js/mapfun1.js"
|
||||||
ct "binary/octet-stream"
|
ct "binary/octet-stream"
|
||||||
filename "monger.test.gridfs.file6"
|
fname "monger.test.gridfs.file6"
|
||||||
md5 "14a09deabb50925a3381315149017bbd"
|
md5 "14a09deabb50925a3381315149017bbd"
|
||||||
stored (store (make-input-file input)
|
stored (store-file (make-input-file input)
|
||||||
(.setFilename filename)
|
(filename fname)
|
||||||
(.setMetaData (to-db-object {:meta "data"}))
|
(metadata (to-db-object {:meta "data"}))
|
||||||
(.setContentType ct))]
|
(content-type ct))]
|
||||||
(is (= 1 (count (gridfs/all-files))))
|
(is (= 1 (count (gridfs/all-files))))
|
||||||
(is (:_id stored))
|
(is (:_id stored))
|
||||||
(is (:uploadDate stored))
|
(is (:uploadDate stored))
|
||||||
(is (= 62 (:length stored)))
|
(is (= 62 (:length stored)))
|
||||||
(is (= md5 (:md5 stored)))
|
(is (= md5 (:md5 stored)))
|
||||||
(is (= filename (:filename stored)))
|
(is (= fname (:filename stored)))
|
||||||
(is (= ct (:contentType stored)))
|
(is (= ct (:contentType stored)))
|
||||||
(let [m (gridfs/find-one-as-map {:filename filename})]
|
(let [m (gridfs/find-one-as-map {:filename fname})]
|
||||||
(is (= {:meta "data"} (:metadata m))))
|
(is (= {:meta "data"} (:metadata m))))
|
||||||
(are [a query] (is (= a (:md5 (gridfs/find-one-as-map query))))
|
(are [a query] (is (= a (:md5 (gridfs/find-one-as-map query))))
|
||||||
md5 (:_id stored)
|
md5 (:_id stored)
|
||||||
md5 filename
|
md5 fname
|
||||||
md5 {:md5 md5}))))
|
md5 {:md5 md5}))))
|
||||||
|
|
||||||
(deftest ^{:gridfs true} test-finding-multiple-files-on-gridfs
|
(deftest ^{:gridfs true} test-finding-multiple-files-on-gridfs
|
||||||
(let [input "./test/resources/mongo/js/mapfun1.js"
|
(let [input "./test/resources/mongo/js/mapfun1.js"
|
||||||
ct "binary/octet-stream"
|
ct "binary/octet-stream"
|
||||||
md5 "14a09deabb50925a3381315149017bbd"
|
md5 "14a09deabb50925a3381315149017bbd"
|
||||||
stored1 (store (make-input-file input)
|
stored1 (store-file (make-input-file input)
|
||||||
(.setFilename "monger.test.gridfs.file6")
|
(filename "monger.test.gridfs.file6")
|
||||||
(.setContentType ct))
|
(content-type ct))
|
||||||
stored2 (store (make-input-file input)
|
stored2 (store-file (make-input-file input)
|
||||||
(.setFilename "monger.test.gridfs.file7")
|
(filename "monger.test.gridfs.file7")
|
||||||
(.setContentType ct))
|
(content-type ct))
|
||||||
list1 (gridfs/find "monger.test.gridfs.file6")
|
list1 (gridfs/find "monger.test.gridfs.file6")
|
||||||
list2 (gridfs/find "monger.test.gridfs.file7")
|
list2 (gridfs/find "monger.test.gridfs.file7")
|
||||||
list3 (gridfs/find "888000___.monger.test.gridfs.file")
|
list3 (gridfs/find "888000___.monger.test.gridfs.file")
|
||||||
|
|
@ -153,12 +153,12 @@
|
||||||
(let [input "./test/resources/mongo/js/mapfun1.js"
|
(let [input "./test/resources/mongo/js/mapfun1.js"
|
||||||
ct "binary/octet-stream"
|
ct "binary/octet-stream"
|
||||||
md5 "14a09deabb50925a3381315149017bbd"
|
md5 "14a09deabb50925a3381315149017bbd"
|
||||||
stored1 (store (make-input-file input)
|
stored1 (store-file (make-input-file input)
|
||||||
(.setFilename "monger.test.gridfs.file8")
|
(filename "monger.test.gridfs.file8")
|
||||||
(.setContentType ct))
|
(content-type ct))
|
||||||
stored2 (store (make-input-file input)
|
stored2 (store-file (make-input-file input)
|
||||||
(.setFilename "monger.test.gridfs.file9")
|
(filename "monger.test.gridfs.file9")
|
||||||
(.setContentType ct))]
|
(content-type ct))]
|
||||||
(is (= 2 (count (gridfs/all-files))))
|
(is (= 2 (count (gridfs/all-files))))
|
||||||
(gridfs/remove { :filename "monger.test.gridfs.file8" })
|
(gridfs/remove { :filename "monger.test.gridfs.file8" })
|
||||||
(is (= 1 (count (gridfs/all-files))))
|
(is (= 1 (count (gridfs/all-files))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue