-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
117 lines (99 loc) · 3.06 KB
/
server.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
fs = Npm.require("fs");
path = Npm.require("path");
mv = Npm.require("mv");
mime = Npm.require("mime-types");
remove = Npm.require("remove");
Q = Npm.require("q");
var storageRootPath = "/filestore";
if (process.env.MEDBOOK_FILESTORE) {
storageRootPath = process.env.MEDBOOK_FILESTORE;
}
console.log("blobs storage root path:", storageRootPath);
function getFilePath (doc) {
return path.join(storageRootPath, doc.storage_path);
}
Blobs2 = new Meteor.Collection("blobs", {
transform: function (doc) {
doc.getFilePath = _.partial(getFilePath, doc);
return doc;
}
});
Blobs2.attachSchema(new SimpleSchema({
// original file name
file_name: { type: String },
mime_type: { type: String },
// security/associated + object
associated_object: {
type: new SimpleSchema({
collection_name: {
type: String,
// allowedValues: [
// // "Patients",
// ],
},
mongo_id: { type: String },
}),
optional: true,
},
// includes the file name, which is the `_id` of the document
storage_path: { type: String, optional: true },
// can put anything here
metadata: { type: Object, blackbox: true, optional: true },
}));
Blobs2.create = function (pathOnServer, associated_object,
metadata, callback) {
check(pathOnServer, String);
check(associated_object, new SimpleSchema({
collection_name: { type: String },
mongo_id: { type: String },
}));
check(metadata, Object);
if (typeof callback !== "function") throw new Meteor.Error("no-callback");
// create the blob
// NOTE: don't set associated_object until it's actually done so if there's
// an error somewhere along the line it's cleaned up
var file_name = path.basename(pathOnServer);
var blobId = Blobs2.insert({
file_name: file_name,
mime_type: mime.lookup(file_name),
});
// move the file to its new home
var storage_path = path.join(blobId.slice(0, 2), blobId.slice(2, 4), blobId);
// only throw an error if the problem is something other than the folder
// already existing
mv(pathOnServer, path.join(storageRootPath, storage_path), { mkdirp: true },
Meteor.bindEnvironment(function (err, out) {
if (err) {
callback(err);
} else {
Blobs2.update(blobId, {
$set: {
associated_object: associated_object,
storage_path: storage_path,
metadata: metadata,
}
});
callback(null, Blobs2.findOne(blobId));
}
}));
};
Blobs2.delete = function (selector, callback) {
if (typeof selector === "string") {
check(selector, String);
} else {
check(selector, Object);
}
if (typeof callback !== "function") throw new Meteor.Error("no-callback");
var rmPromises = [];
Blobs2.find(selector).forEach(function (blob) {
rmPromises.push(Q.nfcall(remove, blob.getFilePath()));
});
Q.all(rmPromises)
.then(Meteor.bindEnvironment(function (result) {
callback(null, rmPromises.length);
}))
.catch(function (yop) {
callback(new Meteor.Error("failed-to-remove-blobs",
"Failed to remove all blobs."));
});
};