Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 2 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const fs = require('fs-extra');
  2. const debug = require('debug')('server-connect:upload');
  3. const { join, basename, extname } = require('path');
  4. const { toAppPath, toSystemPath, toSiteUrl, parseTemplate, getUniqFile } = require('../core/path');
  5. const diacritics = require('../core/diacritics');
  6. module.exports = {
  7. upload: async function(options) {
  8. let self = this;
  9. let fields = this.parse(options.fields || this.parse('{{$_POST}}'));
  10. let path = this.parseOptional(options.path, 'string', '/uploads');
  11. let overwrite = this.parseOptional(options.overwrite, 'boolean', false);
  12. let createPath = this.parseOptional(options.createPath, 'boolean', true);
  13. let throwErrors = this.parseOptional(options.throwErrors, 'boolean', false);
  14. let template = typeof options.template == 'string' ? options.template : false; //this.parseOptional(options.template, 'string', '');
  15. let replaceSpace = this.parseOptional(options.replaceSpace, 'boolean', false);
  16. let asciiOnly = this.parseOptional(options.asciiOnly, 'boolean', false);
  17. let replaceDiacritics = this.parseOptional(options.replaceDiacritics, 'boolean', false);
  18. if (throwErrors) {
  19. for (let field in this.req.files) {
  20. if (Array.isArray(this.req.files[field])) {
  21. for (let file of this.req.files[field]) {
  22. if (file.truncated) {
  23. throw new Error('Some files failed to upload.');
  24. }
  25. }
  26. } else {
  27. let file = this.req.files[field];
  28. if (file.truncated) {
  29. throw new Error('Some files failed to upload.');
  30. }
  31. }
  32. }
  33. }
  34. path = toSystemPath(path);
  35. if (!fs.existsSync(path)) {
  36. if (createPath) {
  37. await fs.ensureDir(path);
  38. } else {
  39. throw new Error(`Upload path doesn't exist.`);
  40. }
  41. }
  42. let files = this.req.files;
  43. let uploaded = [];
  44. if (files) {
  45. await processFields(fields);
  46. }
  47. return typeof fields == 'string' ? (uploaded.length ? uploaded[0] : null) : uploaded;
  48. async function processFields(fields) {
  49. debug('Process fields: %O', fields);
  50. if (typeof fields == 'object') {
  51. for (let i in fields) {
  52. await processFields(fields[i]);
  53. }
  54. } else if (typeof fields == 'string' && files[fields]) {
  55. let processing = files[fields];
  56. if (!Array.isArray(processing)) processing = [processing];
  57. for (let file of processing) {
  58. debug('Processing file: %O', file);
  59. if (!file.processed) {
  60. let name = file.name.replace(/[\x00-\x1f\x7f!%&#@$*()?:,;"'<>^`|+={}\[\]\\\/]/g, '');
  61. if (replaceSpace) name = name.replace(/\s+/g, '_');
  62. if (replaceDiacritics) name = diacritics.replace(name);
  63. if (asciiOnly) name = name.replace(/[^\x00-\x7e]/g, '');
  64. let filepath = join(path, name);
  65. if (template) {
  66. let _template = self.parse(template, self.scope.create({
  67. file: basename(filepath),
  68. name: basename(filepath, extname(filepath)),
  69. ext: extname(filepath)
  70. }));
  71. filepath = parseTemplate(filepath, _template);
  72. }
  73. if (fs.existsSync(filepath)) {
  74. if (overwrite) {
  75. await fs.unlink(filepath);
  76. } else {
  77. filepath = getUniqFile(filepath);
  78. }
  79. }
  80. await file.mv(filepath);
  81. uploaded.push({
  82. name: basename(filepath),
  83. path: toAppPath(filepath),
  84. url: toSiteUrl(filepath),
  85. type: file.mimetype,
  86. size: file.size
  87. });
  88. file.processed = true;
  89. }
  90. }
  91. }
  92. return uploaded;
  93. }
  94. }
  95. };