spee.ch/server/helpers/publishHelpers.js

177 lines
5.3 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
const fs = require('fs');
2018-03-13 17:07:25 +01:00
2018-03-16 18:34:26 +01:00
const { details, publishing } = require('../../config/siteConfig.js');
module.exports = {
parsePublishApiRequestBody ({name, nsfw, license, title, description, thumbnail}) {
// validate name
if (!name) {
throw new Error('no name field found in request');
}
const invalidNameCharacters = /[^A-Za-z0-9,-]/.exec(name);
if (invalidNameCharacters) {
throw new Error('The claim name you provided is not allowed. Only the following characters are allowed: A-Z, a-z, 0-9, and "-"');
}
// optional parameters
nsfw = (nsfw === 'true');
license = license || null;
title = title || null;
description = description || null;
thumbnail = thumbnail || null;
// return results
return {
name,
nsfw,
license,
title,
description,
thumbnail,
};
},
2018-03-03 02:24:18 +01:00
parsePublishApiRequestFiles ({file, thumbnail}) {
// make sure a file was provided
if (!file) {
throw new Error('no file with key of [file] found in request');
}
if (!file.path) {
throw new Error('no file path found');
}
if (!file.type) {
throw new Error('no file type found');
}
if (!file.size) {
throw new Error('no file type found');
}
// validate the file name
if (/'/.test(file.name)) {
throw new Error('apostrophes are not allowed in the file name');
2017-07-08 01:08:35 +02:00
}
// validate the file
module.exports.validateFileTypeAndSize(file);
// return results
return {
2018-03-03 02:24:18 +01:00
fileName : file.name,
filePath : file.path,
fileType : file.type,
thumbnailFileName: (thumbnail ? thumbnail.name : null),
thumbnailFilePath: (thumbnail ? thumbnail.path : null),
thumbnailFileType: (thumbnail ? thumbnail.type : null),
};
},
validateFileTypeAndSize (file) {
2017-07-08 01:08:35 +02:00
// check file type and size
switch (file.type) {
case 'image/jpeg':
case 'image/jpg':
2017-07-08 01:08:35 +02:00
case 'image/png':
if (file.size > 10000000) {
logger.debug('publish > file validation > .jpeg/.jpg/.png was too big');
throw new Error('Sorry, images are limited to 10 megabytes.');
}
break;
2017-07-08 01:08:35 +02:00
case 'image/gif':
2017-07-31 19:14:27 +02:00
if (file.size > 50000000) {
logger.debug('publish > file validation > .gif was too big');
throw new Error('Sorry, .gifs are limited to 50 megabytes.');
2017-07-08 01:08:35 +02:00
}
break;
case 'video/mp4':
if (file.size > 50000000) {
logger.debug('publish > file validation > .mp4 was too big');
throw new Error('Sorry, videos are limited to 50 megabytes.');
2017-07-08 01:08:35 +02:00
}
break;
default:
logger.debug('publish > file validation > unrecognized file type');
throw new Error('The ' + file.type + ' content type is not supported. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.');
2017-07-08 01:08:35 +02:00
}
return file;
},
2018-03-06 00:38:26 +01:00
createBasicPublishParams (filePath, name, title, description, license, nsfw, thumbnail) {
logger.debug(`Creating Publish Parameters`);
// provide defaults for title
if (title === null || title.trim() === '') {
2017-08-21 01:56:37 +02:00
title = name;
}
// provide default for description
2017-09-28 19:51:02 +02:00
if (description === null || description.trim() === '') {
description = '';
2017-08-21 01:56:37 +02:00
}
// provide default for license
if (license === null || license.trim() === '') {
2017-10-27 19:58:08 +02:00
license = ' '; // default to empty string
}
2017-08-21 01:56:37 +02:00
// create the publish params
const publishParams = {
name,
file_path: filePath,
bid : 0.01,
metadata : {
2017-08-21 01:56:37 +02:00
description,
title,
author : details.title,
language: 'en',
license,
nsfw,
},
claim_address: publishing.primaryClaimAddress,
};
2017-10-10 03:29:40 +02:00
// add thumbnail to channel if video
2018-03-03 02:24:18 +01:00
if (thumbnail) {
2017-10-10 03:29:40 +02:00
publishParams['metadata']['thumbnail'] = thumbnail;
}
return publishParams;
},
2018-03-03 02:24:18 +01:00
createThumbnailPublishParams (thumbnailFilePath, claimName, license, nsfw) {
if (!thumbnailFilePath) {
return;
}
logger.debug(`Creating Thumbnail Publish Parameters`);
// create the publish params
return {
name : `${claimName}-thumb`,
file_path: thumbnailFilePath,
bid : 0.01,
metadata : {
title : `${claimName} thumbnail`,
description: `a thumbnail for ${claimName}`,
author : details.title,
2018-03-03 02:24:18 +01:00
language : 'en',
license,
nsfw,
},
claim_address: publishing.primaryClaimAddress,
channel_name : publishing.thumbnailChannel,
channel_id : publishing.thumbnailChannelId,
2018-03-03 02:24:18 +01:00
};
},
deleteTemporaryFile (filePath) {
fs.unlink(filePath, err => {
2017-10-12 22:10:44 +02:00
if (err) {
logger.error(`error deleting temporary file ${filePath}`);
throw err;
}
logger.debug(`successfully deleted ${filePath}`);
});
},
2018-01-23 21:08:53 +01:00
addGetResultsToFileData (fileInfo, getResult) {
fileInfo.fileName = getResult.file_name;
fileInfo.filePath = getResult.download_path;
return fileInfo;
},
createFileData ({ name, claimId, outpoint, height, address, nsfw, contentType }) {
return {
name,
claimId,
outpoint,
height,
address,
fileName: '',
filePath: '',
fileType: contentType,
nsfw,
};
},
};