2014-02-27 06:44:09 +04:00
|
|
|
var moment = require('moment'),
|
2013-11-07 20:00:39 +04:00
|
|
|
path = require('path'),
|
|
|
|
when = require('when'),
|
|
|
|
baseStore;
|
|
|
|
|
|
|
|
// TODO: would probably be better to put these on the prototype and have proper constructors etc
|
|
|
|
baseStore = {
|
|
|
|
'getTargetDir': function (baseDir) {
|
|
|
|
var m = moment(new Date().getTime()),
|
|
|
|
month = m.format('MMM'),
|
|
|
|
year = m.format('YYYY');
|
|
|
|
|
|
|
|
if (baseDir) {
|
|
|
|
return path.join(baseDir, year, month);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.join(year, month);
|
|
|
|
},
|
|
|
|
'generateUnique': function (store, dir, name, ext, i, done) {
|
|
|
|
var self = this,
|
|
|
|
filename,
|
|
|
|
append = '';
|
|
|
|
|
|
|
|
if (i) {
|
|
|
|
append = '-' + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = path.join(dir, name + append + ext);
|
|
|
|
|
|
|
|
store.exists(filename).then(function (exists) {
|
|
|
|
if (exists) {
|
|
|
|
setImmediate(function () {
|
|
|
|
i = i + 1;
|
|
|
|
self.generateUnique(store, dir, name, ext, i, done);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
done.resolve(filename);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'getUniqueFileName': function (store, image, targetDir) {
|
|
|
|
var done = when.defer(),
|
|
|
|
ext = path.extname(image.name),
|
2014-03-24 00:56:02 +04:00
|
|
|
name = path.basename(image.name, ext).replace(/[\W]/gi, '-');
|
2013-11-07 20:00:39 +04:00
|
|
|
|
|
|
|
this.generateUnique(store, targetDir, name, ext, 0, done);
|
|
|
|
|
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-27 06:44:09 +04:00
|
|
|
module.exports = baseStore;
|