diff --git a/helpers/cdn.helper.js b/helpers/cdn.helper.js index 60dbbfc..b97e761 100644 --- a/helpers/cdn.helper.js +++ b/helpers/cdn.helper.js @@ -2,6 +2,7 @@ const Path = require("path"); const config = require('../config'); const sanitize = require("sanitize-filename"); const safename = require("safename"); +const unf = require('unique-file-name'); const assetsUrl = config.cdn.hostname; @@ -16,6 +17,14 @@ const CDN_PATHS = { const sanitizeFilename = (filename) => safename(sanitize(filename, { replacement: "_" })); +const getUniqueName = function (path, filename) { + const namer = unf({ + format: '%32b_%6r%8e', + dir: path + }); + return namer(filename) +} + const getCDNMediaUrl = (mediaUri) => { if (mediaUri) { const pathParsed = mediaUri.indexOf("media") == 0 ? mediaUri.substr("media/".length, mediaUri.length) : mediaUri; @@ -28,7 +37,7 @@ const getCDNMediaUrl = (mediaUri) => { const getCDNCityMediaUrl = (cityName) => encodeURI(`${assetsUrl}/${CDN_PATHS.CITIES}/${cityName}.jpg`); -const getCDNFilenameWithPath = (filename, type) => { +const getCDNPath = (type = '') => { var cdnPath = ''; switch (type) { case 'speaker': @@ -43,14 +52,41 @@ const getCDNFilenameWithPath = (filename, type) => { break; default: - throw new Error('Multimedia type unknown'); + var _date = new Date(); + cdnPath = _date.getFullYear() + '/' + _date.getMonth() + '/'; + break; + } + return Path.join(config.uploads.path, cdnPath); +} + +const getCDNFilenameWithPath = (filename, type) => { + var cdnPath = ''; + switch (type) { + case 'speaker': + cdnPath = CDN_PATHS.SPEAKERS; + break; + case 'event': + cdnPath = CDN_PATHS.EVENT; + break; + + case 'post': + cdnPath = CDN_PATHS.BLOG; + break; + + default: + var _date = new Date(); + cdnPath = _date.getFullYear() + '/' + _date.getMonth() + '/'; + break; } return Path.join(cdnPath, sanitizeFilename(filename)); } module.exports = { + sanitizeFilename, CDN_PATHS, getCDNCityMediaUrl, getCDNMediaUrl, - getCDNFilenameWithPath + getCDNPath, + getCDNFilenameWithPath, + getUniqueName } \ No newline at end of file diff --git a/middlewares/uploader.js b/middlewares/uploader.js index 654192c..9a50a34 100644 --- a/middlewares/uploader.js +++ b/middlewares/uploader.js @@ -1,6 +1,7 @@ var multer = require('multer'); var mkdirp = require('mkdirp'); var config = require('../config'); +var cdnHelper = require('../helpers/cdn.helper'); /*var upload = function (path) { return multer({storage: storage(path)}); @@ -19,24 +20,22 @@ var makePath = function(path) { var cdnStorage = function(path) { return multer.diskStorage({ destination: function (req, file, cb) { - console.log(req); - console.log(file); cb(null, path) }, filename: function (req, file, cb) { - cb(null, file.originalname) + cdnHelper.getUniqueName(path, file.originalname) + .then(function(betterName) { + cb(null, betterName) + }); } }); } var cdnUpload = function() { - var _date = new Date(); - var pathWithDate = config.uploads.path + '/' + _date.getFullYear() + '/' + _date.getMonth() + '/'; - - makePath(pathWithDate); - + var cdnPath = cdnHelper.getCDNPath(); + makePath(cdnPath); return multer({ - storage: cdnStorage(pathWithDate) + storage: cdnStorage(cdnPath) }); } diff --git a/modules/multimedia/multimedia_file.controller.js b/modules/multimedia/multimedia_file.controller.js index cb1e93c..3bd68be 100644 --- a/modules/multimedia/multimedia_file.controller.js +++ b/modules/multimedia/multimedia_file.controller.js @@ -1,5 +1,6 @@ 'use strict'; const httpStatus = require('http-status'); +var configuration = require('../../config'); const generateControllers = require('../../core/controllers'); const multimediaFileService = require('./multimedia_file.service'); const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper'); @@ -43,10 +44,14 @@ const extraControllers = { create: (config) => { return async (req, res, next) => { + var data = req.body; + try { + if (req.file) { + data.url = req.file.path.replace(configuration.uploads.path, ''); + } var data = req.body; console.log(data); - console.log(req.file); const context = buildContext(req, config); const result = await multimediaFileService.create(data, context); diff --git a/package.json b/package.json index bca8ef7..13d72da 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "bcrypt": "^3.0.6", "body-parser": "^1.18.3", "buffer": "^5.2.1", + "chai": "^4.2.0", "cheerio": "^1.0.0-rc.3", "compression": "^1.7.4", "cors": "^2.8.5", @@ -63,6 +64,7 @@ "sanitize-filename": "^1.6.2", "sequelize": "^5.6.1", "sharp": "^0.23.0", + "unique-file-name": "^1.0.0", "vimeo": "^2.1.1", "vm": "^0.1.0", "winston": "^3.2.1"