|
| 1 | +var mongoose = require("mongoose") |
| 2 | + , request = require('request') |
| 3 | + |
| 4 | +var GridStore = mongoose.mongo.GridStore; |
| 5 | +var Grid = mongoose.mongo.Grid; |
| 6 | + |
| 7 | + exports.get = function(id, fn) { |
| 8 | + var db, store; |
| 9 | + db = mongoose.connection.db; |
| 10 | + id = new mongoose.mongo.BSONPure.ObjectID(id); |
| 11 | + store = new GridStore(db, id, "r", { |
| 12 | + root: "fs" |
| 13 | + }); |
| 14 | + return store.open(function(err, store) { |
| 15 | + if (err) return fn(err); |
| 16 | + return store.readBuffer(fn); |
| 17 | + }); |
| 18 | + }; |
| 19 | + exports.pipe = function(id, outstream) { |
| 20 | + var db, store; |
| 21 | + db = mongoose.connection.db; |
| 22 | + id = new mongoose.mongo.BSONPure.ObjectID(id); |
| 23 | + store = new GridStore(db, id, "r", { |
| 24 | + root: "fs" |
| 25 | + }); |
| 26 | + store.stream(true).pipe(outstream) |
| 27 | + }; |
| 28 | + |
| 29 | + exports.put = function(buf, name, fn) { |
| 30 | + var db; |
| 31 | + db = mongoose.connection.db; |
| 32 | + return new GridStore(db, name, "w").open(function(err, file) { |
| 33 | + if (err) return fn(err); |
| 34 | + return file.write(buf, true, fn); |
| 35 | + }); |
| 36 | + }; |
| 37 | + |
| 38 | + exports.putFile = function(path, name, options, fn) { |
| 39 | + var db = mongoose.connection.db; |
| 40 | + return new GridStore(db, name, "w", options).open(function(err, file) { |
| 41 | + if (err) return fn(err); |
| 42 | + return file.writeFile(path, fn); |
| 43 | + }); |
| 44 | + }; |
| 45 | + |
| 46 | + exports.deleteFile = function(id, fn) { |
| 47 | + var db, store; |
| 48 | + console.log("deleting " + id); |
| 49 | + db = mongoose.connection.db; |
| 50 | + id = new mongoose.mongo.BSONPure.ObjectID(id); |
| 51 | + store = new GridStore(db, id, "r", { |
| 52 | + root: "fs" |
| 53 | + }); |
| 54 | + return store.unlink(function(err, result) { |
| 55 | + if (err) { |
| 56 | + console.log(err); |
| 57 | + if (err) return fn(err); |
| 58 | + } |
| 59 | + return true; |
| 60 | + }); |
| 61 | + }; |
0 commit comments