uploads.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use strict";
  2. var path = require('path'),
  3. Promise = require('bluebird'),
  4. fs = Promise.promisifyAll(require('fs-extra')),
  5. multer = require('multer'),
  6. _ = require('lodash');
  7. var regFolderName = new RegExp("^[a-z0-9][a-z0-9\-]*[a-z0-9]$");
  8. /**
  9. * Uploads
  10. */
  11. module.exports = {
  12. _uploadsPath: './repo/uploads',
  13. _uploadsThumbsPath: './data/thumbs',
  14. /**
  15. * Initialize Local Data Storage model
  16. *
  17. * @param {Object} appconfig The application config
  18. * @return {Object} Uploads model instance
  19. */
  20. init(appconfig) {
  21. this._uploadsPath = path.resolve(ROOTPATH, appconfig.paths.repo, 'uploads');
  22. this._uploadsThumbsPath = path.resolve(ROOTPATH, appconfig.paths.data, 'thumbs');
  23. return this;
  24. },
  25. /**
  26. * Gets the thumbnails folder path.
  27. *
  28. * @return {String} The thumbs path.
  29. */
  30. getThumbsPath() {
  31. return this._uploadsThumbsPath;
  32. },
  33. /**
  34. * Gets the uploads folders.
  35. *
  36. * @return {Array<String>} The uploads folders.
  37. */
  38. getUploadsFolders() {
  39. return db.UplFolder.find({}, 'name').sort('name').exec().then((results) => {
  40. return (results) ? _.map(results, 'name') : [{ name: '' }];
  41. });
  42. },
  43. /**
  44. * Creates an uploads folder.
  45. *
  46. * @param {String} folderName The folder name
  47. * @return {Promise} Promise of the operation
  48. */
  49. createUploadsFolder(folderName) {
  50. let self = this;
  51. folderName = _.kebabCase(_.trim(folderName));
  52. if(_.isEmpty(folderName) || !regFolderName.test(folderName)) {
  53. return Promise.resolve(self.getUploadsFolders());
  54. }
  55. return fs.ensureDirAsync(path.join(self._uploadsPath, folderName)).then(() => {
  56. return db.UplFolder.findOneAndUpdate({
  57. _id: 'f:' + folderName
  58. }, {
  59. name: folderName
  60. }, {
  61. upsert: true
  62. });
  63. }).then(() => {
  64. return self.getUploadsFolders();
  65. });
  66. },
  67. /**
  68. * Check if folder is valid and exists
  69. *
  70. * @param {String} folderName The folder name
  71. * @return {Boolean} True if valid
  72. */
  73. validateUploadsFolder(folderName) {
  74. return db.UplFolder.findOne({ name: folderName }).then((f) => {
  75. return (f) ? path.resolve(this._uploadsPath, folderName) : false;
  76. })
  77. },
  78. /**
  79. * Adds one or more uploads files.
  80. *
  81. * @param {Array<Object>} arrFiles The uploads files
  82. * @return {Void} Void
  83. */
  84. addUploadsFiles(arrFiles) {
  85. if(_.isArray(arrFiles) || _.isPlainObject(arrFiles)) {
  86. //this._uploadsDb.Files.insert(arrFiles);
  87. }
  88. return;
  89. },
  90. /**
  91. * Gets the uploads files.
  92. *
  93. * @param {String} cat Category type
  94. * @param {String} fld Folder
  95. * @return {Array<Object>} The files matching the query
  96. */
  97. getUploadsFiles(cat, fld) {
  98. return db.UplFile.find({
  99. category: cat,
  100. folder: 'f:' + fld
  101. }).sort('filename').exec();
  102. },
  103. /**
  104. * Deletes an uploads file.
  105. *
  106. * @param {string} uid The file unique ID
  107. * @return {Promise} Promise of the operation
  108. */
  109. deleteUploadsFile(uid) {
  110. let self = this;
  111. return db.UplFile.findOneAndRemove({ _id: uid }).then((f) => {
  112. if(f) {
  113. fs.remove(path.join(self._uploadsThumbsPath, uid + '.png'));
  114. fs.remove(path.resolve(self._uploadsPath, f.folder.slice(2), f.filename));
  115. }
  116. return true;
  117. })
  118. }
  119. };