/
gridfs.coffee
44 lines (38 loc) · 1.15 KB
/
gridfs.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
mongoose = require "mongoose"
request = require "request"
GridStore = mongoose.mongo.GridStore
Grid = mongoose.mongo.Grid
ObjectID = mongoose.mongo.BSONPure.ObjectID
exports.get = (id, fn) ->
db = mongoose.connection.db
id = new ObjectID(id)
store = new GridStore(db, id, "r",
root: "fs"
)
store.open (err, store) ->
return fn(err) if err
# band-aid
if "#{store.filename}" == "#{store.fileId}" and store.metadata and store.metadata.filename
store.filename = store.metadata.filename
fn null, store
exports.put = (buf, name, options..., fn) ->
db = mongoose.connection.db
options = parse(options)
options.metadata.filename = name
new GridStore(db, name, "w", options).open (err, file) ->
return fn(err) if err
file.write buf, true, fn
exports.putFile = (path, name, options, fn) ->
db = mongoose.connection.db
options = parse(options)
options.metadata.filename = name
new GridStore(db, name, "w", options).open (err, file) ->
return fn(err) if err
file.writeFile path, fn
parse = (options) ->
opts = {}
if options.length > 0
opts = options[0]
if !opts.metadata
opts.metadata = {}
opts