-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
214 lines (193 loc) · 6.73 KB
/
Copy pathindex.js
File metadata and controls
214 lines (193 loc) · 6.73 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const Promise = require('bluebird');
const async = require('async');
const _ = require('lodash');
const regExpQuote = require('regexp-quote');
const fs = require('fs');
// Efficiently sync a local folder of static files to uploadfs,
// with the ability to remove the folder and its contents later.
// This ensures your application will still work if you move
// it to s3, azure blob storage, etc. via uploadfs.
//
// It is meant for delivering the output of tools like backstop,
// sitemap generators, etc. that generate output as HTML files
// and associated assets.
//
// As long as you use this module, you won't have to worry about
// making changes to those tools on the day you switch to S3.
module.exports = () => {
let options = null;
const self = {
// options.db (a mongodb database object) and options.uploadfs (an
// instance of uploadfs) are required. If no callback is passed, a promise
// is returned. You must await that promise before invoking other methods.
init: (options, callback) => {
if (!options.db) {
throw new Error('db argument not given, mongodb connection object must be supplied');
}
if (!options.uploadfs) {
throw new Error('uploadfs option not given, uploadfs instance must be supplied');
}
self.uploadfs = options.uploadfs;
if (!callback) {
return Promise.promisify(body)();
} else {
return body(callback);
}
function body(callback) {
return options.db.collection(options.collectionName || 'cloudStatic', function(err, collection) {
self.db = collection;
return callback(err);
});
}
},
// Sync files from localFolder (on disk) to uploadfsPath (in uploadfs).
// The url(uploadfsPath) method may then be used to get a URL at which
// the folder can be viewed as if it were served as a static site, even
// though the site may be in the cloud, as long as uploadfs has been
// properly configured. Note that your cloud storage might or might not
// be configured to automatically serve `index.html` if that is not
// present in the URL.
//
// Existing files at or below uploadfsPath that do not exist in localFolder
// are REMOVED.
//
// If a file has not been modified since its last sync
// according to the filesystem, it MAY not be copied again, for
// performance reasons.
//
// This method is NOT guaranteed to clean up old content properly unless the previously
// uploaded content was also uploaded with this method.
//
// If no callback is passed, a promise is returned.
syncFolder: function(localFolder, uploadfsPath, callback) {
let files = null;
if (!callback) {
return Promise.promisify(body)();
} else {
return body(callback);
}
function body(callback) {
return async.series([
sync,
cleanup
], callback);
function sync(callback) {
return syncToUploadfs(localFolder, uploadfsPath, (err, copies) => {
if (err) {
return callback(err);
}
files = copies.map(copy => {
return {
_id: copy.to
};
});
return async.eachLimit(files, 5, (file, callback) => {
return self.db.update(file, file, { upsert: true }, callback);
}, callback);
});
}
function cleanup(callback) {
return self.removeFolder(uploadfsPath, _.map(files, '_id'), callback);
}
}
function syncToUploadfs(from, to, callback) {
var copies = [];
copies = enumerateCopies(from, to);
return performCopies(function(err) {
if (err) {
return callback(err);
}
return callback(null, copies);
});
function performCopies(callback) {
return async.eachLimit(copies, 5, function(copy, callback) {
return self.uploadfs.copyIn(copy.from, copy.to, function(err) {
return callback(err);
});
}, callback);
}
};
function enumerateCopies(from, to) {
var copies = [];
enumerateDir(from, to);
return copies;
function enumerateDir(from, to) {
var files = fs.readdirSync(from);
_.each(files, function(file) {
var fromFile = from + '/' + file;
var toFile = to + '/' + file;
var stat = fs.statSync(fromFile);
if (stat.isDirectory()) {
enumerateDir(fromFile, toFile);
} else {
enumerateFile(fromFile, toFile);
}
});
}
function enumerateFile(fromFile, toFile) {
copies.push({
from: fromFile,
to: toFile
});
}
};
},
// Recursively removes the contents of a folder previously synced up to the web
// with the `syncFolder` method. Any files whose uploadfs paths are in the `except`
// array are not removed. The `except` parameter may be completely omitted.
//
// This method is NOT guaranteed to work unless the folder was
// originally synced up using `syncFolder`.
//
// If no callback is passed, a promise is returned.
removeFolder: (uploadfsPath, except, callback) => {
if (!Array.isArray(except)) {
callback = except;
except = [];
}
if (!callback) {
return Promise.promisify(body)();
} else {
return body(callback);
}
function body(callback) {
return self.db.find({
$and: [
{
_id: new RegExp('^' + regExpQuote(uploadfsPath + '/'))
}, {
_id: { $nin: except }
}
]
}).toArray(function(err, files) {
if (err) {
return callback(err);
}
return async.eachLimit(files, 5, (file, callback) => {
return async.series([
fromUploadfs,
fromDb
], callback);
function fromUploadfs(callback) {
return self.uploadfs.remove(file._id, function(err) {
if (err) {
console.warn('File most likely already gone from uploadfs: ' + file._id);
}
return callback(null);
});
}
function fromDb(callback) {
return self.db.remove({ _id: file._id }, callback);
}
}, callback);
});
}
},
// Returns the public URL corresponding to an uploadfs path. Provided for
// convenience.
getUrl: (uploadfsPath) => {
return self.uploadfs.getUrl() + uploadfsPath;
}
};
return self;
};