2019-07-25 07:37:04 +00:00
|
|
|
/* global Venue */
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
2019-08-07 18:32:13 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
|
const Path = require("path");
|
|
|
|
|
const sanitize = require("sanitize-filename");
|
|
|
|
|
const safename = require("safename");
|
|
|
|
|
const writeFile = require('write');
|
|
|
|
|
const imageType = require('image-type');
|
2022-12-12 11:26:58 +00:00
|
|
|
//const sharp = require('sharp');
|
2019-08-07 18:32:13 +00:00
|
|
|
|
|
|
|
|
const config = require('../../config');
|
2019-07-25 07:37:04 +00:00
|
|
|
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
|
|
|
|
|
const models = require('../../core/models');
|
2019-08-18 15:08:18 +00:00
|
|
|
const { determineProviderInfo, extractProviderInfo } = require('../../helpers/providers.helper');
|
2019-07-25 07:37:04 +00:00
|
|
|
|
|
|
|
|
const providerComposer = (multimedia) => {
|
2019-08-18 15:08:18 +00:00
|
|
|
return extractProviderInfo(multimedia.code)
|
2019-07-25 07:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-16 16:37:13 +00:00
|
|
|
/*const _generateThumbFileName = function (fileName, prefix) {
|
2019-08-07 18:32:13 +00:00
|
|
|
prefix = prefix || config.uploads.thumb_prefix;
|
|
|
|
|
return Path.join(
|
|
|
|
|
Path.dirname(fileName),
|
|
|
|
|
prefix + Path.basename(fileName)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-15 17:54:18 +00:00
|
|
|
const _uploadFile = function (file, filename, options) {
|
|
|
|
|
var defaultOptions = {
|
|
|
|
|
thumb: false,
|
|
|
|
|
thumbSize: config.uploads.thumb_size,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options = _.defaults(options || {}, defaultOptions);
|
|
|
|
|
var diskFilename = Path.join(config.uploads.path, filename);
|
|
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
if (!imageType(file.buffer)) {
|
|
|
|
|
reject('No es un fichero de imagen');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fs.writeFile(diskFilename, file.buffer, function (error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
reject(error);
|
|
|
|
|
} else {
|
|
|
|
|
if (options.thumb) {
|
|
|
|
|
var diskThumbFilename = _generateThumbFileName(diskfilename);
|
|
|
|
|
sharp(file.buffer).resize(options.thumbSize).toBuffer()
|
|
|
|
|
.then(function (data) {
|
|
|
|
|
fs.writeFile(diskThumbFilename, data, function (error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
reject(error);
|
|
|
|
|
} else {
|
|
|
|
|
resolve({
|
|
|
|
|
fileName: fileName,
|
|
|
|
|
diskFilename: diskFilename,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(function (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
reject(error);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-08-16 16:37:13 +00:00
|
|
|
}*/
|
2019-08-07 18:32:13 +00:00
|
|
|
|
2019-07-25 07:37:04 +00:00
|
|
|
const extraMethods = {
|
2019-08-07 18:32:13 +00:00
|
|
|
removeFile: function (filename) {
|
|
|
|
|
|
|
|
|
|
const diskFilename = Path.join(config.uploads.path, filename);
|
|
|
|
|
const diskThumbFilename = _generateThumbFileName(diskFilename);
|
|
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
|
fs.unlink(diskFilename, function (error) {
|
|
|
|
|
fs.unlink(diskThumbFilename, function (error) {
|
|
|
|
|
resolve();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
2019-08-15 17:54:18 +00:00
|
|
|
uploadFile: async function (file, filename, data, context) {
|
|
|
|
|
const upload = await _uploadFile(file, filename);
|
|
|
|
|
return await models.MultimediaFile.create({
|
|
|
|
|
name: data.name,
|
|
|
|
|
description: data.description,
|
|
|
|
|
provider: 'cdn',
|
|
|
|
|
class: 'picture',
|
|
|
|
|
url: filename,
|
|
|
|
|
userId: context && context.user ? context.user.id : undefined
|
|
|
|
|
})
|
2019-08-07 18:32:13 +00:00
|
|
|
},
|
|
|
|
|
|
2019-08-16 16:37:13 +00:00
|
|
|
beforeCreate: function(values, context) {
|
2019-08-20 17:26:42 +00:00
|
|
|
/*if (values.provider != 'cdn') {
|
2019-08-16 16:37:13 +00:00
|
|
|
var info = determineProviderInfo(values.url);
|
|
|
|
|
|
|
|
|
|
values.provider = info.provider;
|
|
|
|
|
values.code = info.code;
|
|
|
|
|
values.class = info.class;
|
2019-08-20 17:26:42 +00:00
|
|
|
}*/
|
|
|
|
|
console.log(values);
|
2019-08-16 16:37:13 +00:00
|
|
|
return values;
|
2019-08-15 17:54:18 +00:00
|
|
|
},
|
2019-08-07 18:32:13 +00:00
|
|
|
|
2019-07-25 07:37:04 +00:00
|
|
|
afterFetchOne: async (result, params, context) => {
|
|
|
|
|
if (!result) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const multimedia = result.toJSON();
|
2019-07-28 20:08:15 +00:00
|
|
|
|
2019-07-25 07:37:04 +00:00
|
|
|
|
|
|
|
|
if (multimedia.provider === 'vimeo') {
|
|
|
|
|
multimedia.providerInfo = await providerComposer(multimedia);
|
|
|
|
|
}
|
|
|
|
|
return multimedia;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 16:37:13 +00:00
|
|
|
module.exports = generateService(models.MultimediaFile, extraMethods);
|