updated publish flow and returned to image-size

This commit is contained in:
bill bittner 2018-07-27 12:31:08 -07:00
parent 0037addcda
commit 3a46a3714e
3 changed files with 40 additions and 33 deletions

View file

@ -47,29 +47,38 @@ const authenticateChannelCredentials = (channelName, channelId, userPassword) =>
}; };
const authenticateUser = (channelName, channelId, channelPassword, user) => { const authenticateUser = (channelName, channelId, channelPassword, user) => {
return new Promise((resolve, reject) => {
// case: no channelName or channel Id are provided (anonymous), regardless of whether user token is provided // case: no channelName or channel Id are provided (anonymous), regardless of whether user token is provided
if (!channelName && !channelId) { if (!channelName && !channelId) {
return { resolve({
channelName : null, channelName : null,
channelClaimId: null, channelClaimId: null,
}; });
return;
} }
// case: channelName or channel Id are provided with user token // case: channelName or channel Id are provided with user token
if (user) { if (user) {
if (channelName && channelName !== user.channelName) { if (channelName && channelName !== user.channelName) {
throw new Error('the provided channel name does not match user credentials'); reject(new Error('the provided channel name does not match user credentials'));
return;
} }
if (channelId && channelId !== user.channelClaimId) { if (channelId && channelId !== user.channelClaimId) {
throw new Error('the provided channel id does not match user credentials'); reject(new Error('the provided channel id does not match user credentials'));
return;
} }
return { resolve({
channelName : user.channelName, channelName : user.channelName,
channelClaimId: user.channelClaimId, channelClaimId: user.channelClaimId,
}; });
return;
} }
// case: channelName or channel Id are provided with password instead of user token // case: channelName or channel Id are provided with password instead of user token
if (!channelPassword) throw new Error('no channel password provided'); if (!channelPassword) {
return authenticateChannelCredentials(channelName, channelId, channelPassword); reject(new Error('no channel password provided'));
return;
}
resolve(authenticateChannelCredentials(channelName, channelId, channelPassword));
});
}; };
module.exports = authenticateUser; module.exports = authenticateUser;

View file

@ -54,8 +54,6 @@ const claimPublish = ({ body, files, headers, ip, originalUrl, user, tor }, res)
// check channel authorization // check channel authorization
authenticateUser(channelName, channelId, channelPassword, user) authenticateUser(channelName, channelId, channelPassword, user)
.then(({ channelName, channelClaimId }) => { .then(({ channelName, channelClaimId }) => {
// add channel details to the publish params
return Promise.all([ return Promise.all([
checkClaimAvailability(name), checkClaimAvailability(name),
createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName, channelClaimId), createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName, channelClaimId),
@ -63,7 +61,7 @@ const claimPublish = ({ body, files, headers, ip, originalUrl, user, tor }, res)
]) ])
}) })
.then(([ claimAvailable, publishParams, thumbnailPublishParams ]) => { .then(([ claimAvailable, publishParams, thumbnailPublishParams ]) => {
if (!claimAvailabile) { if (!claimAvailable) {
throw { throw {
name: CLAIM_TAKEN, name: CLAIM_TAKEN,
message: 'That claim name is already taken' message: 'That claim name is already taken'

View file

@ -1,8 +1,9 @@
const im = require('imagemagick'); const imageMagick = require('imagemagick');
const sizeOf = require('image-size');
const getImageMetadata = (filePath) => { const getImageMetadata = (filePath) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
im.readMetadata(filePath, (err, metadata) => { imageMagick.readMetadata(filePath, (err, metadata) => {
if (err) { if (err) {
reject(err); reject(err);
} }
@ -13,7 +14,7 @@ const getImageMetadata = (filePath) => {
const getImageDetails = (filePath) => { const getImageDetails = (filePath) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
im.identify(filePath, (err, details) => { imageMagick.identify(filePath, (err, details) => {
if (err) { if (err) {
reject(err); reject(err);
} }
@ -24,13 +25,12 @@ const getImageDetails = (filePath) => {
const getImageHeightAndWidth = (filePath) => { const getImageHeightAndWidth = (filePath) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
im.identify(filePath, (err, details) => { try {
if (err) { const { height, width } = sizeOf(filePath);
reject(err);
}
const { height, width } = details;
resolve([height, width]); resolve([height, width]);
}); } catch (error) {
reject(error);
}
}); });
}; };