2014-02-27 06:44:09 +04:00
|
|
|
var moment = require('moment'),
|
2014-08-29 07:48:49 +04:00
|
|
|
path = require('path');
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
function StorageBase() {
|
|
|
|
}
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
StorageBase.prototype.getTargetDir = function (baseDir) {
|
|
|
|
var m = moment(new Date().getTime()),
|
2014-09-03 07:24:12 +04:00
|
|
|
month = m.format('MM'),
|
2014-08-29 07:48:49 +04:00
|
|
|
year = m.format('YYYY');
|
|
|
|
|
|
|
|
if (baseDir) {
|
|
|
|
return path.join(baseDir, year, month);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.join(year, month);
|
|
|
|
};
|
|
|
|
|
|
|
|
StorageBase.prototype.generateUnique = function (store, dir, name, ext, i) {
|
|
|
|
var self = this,
|
|
|
|
filename,
|
|
|
|
append = '';
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
if (i) {
|
|
|
|
append = '-' + i;
|
2013-11-07 20:00:39 +04:00
|
|
|
}
|
2014-08-29 07:48:49 +04:00
|
|
|
|
|
|
|
filename = path.join(dir, name + append + ext);
|
|
|
|
|
|
|
|
return store.exists(filename).then(function (exists) {
|
|
|
|
if (exists) {
|
|
|
|
i = i + 1;
|
|
|
|
return self.generateUnique(store, dir, name, ext, i);
|
|
|
|
} else {
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
StorageBase.prototype.getUniqueFileName = function (store, image, targetDir) {
|
|
|
|
var ext = path.extname(image.name),
|
|
|
|
name = path.basename(image.name, ext).replace(/[\W]/gi, '-'),
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
return self.generateUnique(store, targetDir, name, ext, 0);
|
2013-11-07 20:00:39 +04:00
|
|
|
};
|
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
module.exports = StorageBase;
|