spee.ch/routes/api-routes.js

243 lines
8.8 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
const multipart = require('connect-multiparty');
const config = require('../config/speechConfig.js');
const multipartMiddleware = multipart({uploadDir: config.files.uploadDirectory});
2017-09-28 20:42:29 +02:00
const db = require('../models');
2017-08-16 20:00:17 +02:00
const { publish } = require('../controllers/publishController.js');
2017-11-21 00:51:05 +01:00
const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js');
2017-11-03 21:37:23 +01:00
const { createPublishParams, validateApiPublishRequest, validatePublishSubmission, cleanseChannelName, checkClaimNameAvailability, checkChannelAvailability } = require('../helpers/publishHelpers.js');
2017-08-03 02:13:02 +02:00
const errorHandlers = require('../helpers/errorHandlers.js');
2017-11-21 00:51:05 +01:00
const { postToStats } = require('../controllers/statsController.js');
2017-11-13 20:20:37 +01:00
const { authenticateOrSkip } = require('../auth/authentication.js');
2017-11-21 21:53:43 +01:00
function addGetResultsToFileRecord (fileInfo, getResult) {
fileInfo.fileName = getResult.file_name;
fileInfo.filePath = getResult.download_path;
return fileInfo;
}
function createFileRecord ({ name, claimId, outpoint, height, address, nsfw, contentType }) {
return {
name,
claimId,
outpoint,
height,
address,
fileName: '',
filePath: '',
fileType: contentType,
nsfw,
};
}
2017-09-20 23:39:20 +02:00
module.exports = (app) => {
// route to run a claim_list request on the daemon
2017-11-21 00:51:05 +01:00
app.get('/api/claim_list/:name', ({ ip, originalUrl, params }, res) => {
2017-08-16 20:00:17 +02:00
getClaimList(params.name)
.then(claimsList => {
postToStats('serve', originalUrl, ip, null, null, 'success');
res.status(200).json(claimsList);
})
.catch(error => {
2017-11-02 19:50:05 +01:00
errorHandlers.handleApiError('claim_list', originalUrl, ip, error, res);
2017-08-16 20:00:17 +02:00
});
});
// route to see if asset is available locally
app.get('/api/local/:name/:claimId', ({ ip, originalUrl, params }, res) => {
const name = params.name;
const claimId = params.claimId;
let isLocalFileAvailable = false;
db.File.findOne({where: {name, claimId}})
.then(result => {
if (result) {
isLocalFileAvailable = true;
}
res.status(200).json({status: 'success', isLocalFileAvailable});
})
.catch(error => {
errorHandlers.handleApiError('get', originalUrl, ip, error, res);
});
});
2017-11-21 00:51:05 +01:00
// route to get an asset
app.get('/api/get/:name/:claimId', ({ ip, originalUrl, params }, res) => {
2017-11-21 21:53:43 +01:00
let fileRecord;
// resolve and get the claim
2017-11-21 21:53:43 +01:00
db.Claim.resolveClaim(params.name, params.claimId)
.then(resolveResult => {
if (!resolveResult) {
throw new Error('No matching uri found in Claim table');
}
fileRecord = createFileRecord(resolveResult);
// get the claim
2017-11-21 21:53:43 +01:00
return getClaim(`${params.name}#${params.claimId}`);
})
.then(getResult => {
fileRecord = addGetResultsToFileRecord(fileRecord, getResult);
// insert a record for the claim into the File table
2017-11-21 21:53:43 +01:00
return db.File.create(fileRecord);
})
.then(() => {
res.status(200).json({status: 'success', message: 'content was successfully retrieved via lbrynet'});
2017-11-21 21:53:43 +01:00
logger.debug('File record successfully created');
2017-11-21 00:51:05 +01:00
})
.catch(error => {
errorHandlers.handleApiError('get', originalUrl, ip, error, res);
});
});
2017-07-03 23:48:35 +02:00
// route to check whether spee.ch has published to a claim
2017-10-10 03:29:40 +02:00
app.get('/api/isClaimAvailable/:name', ({ params }, res) => {
2017-09-19 21:54:23 +02:00
checkClaimNameAvailability(params.name)
2017-08-16 20:00:17 +02:00
.then(result => {
if (result === true) {
res.status(200).json(true);
} else {
2017-09-21 02:14:33 +02:00
logger.debug(`Rejecting '${params.name}' because that name has already been claimed on spee.ch`);
2017-08-16 20:00:17 +02:00
res.status(200).json(false);
}
})
.catch(error => {
res.status(500).json(error);
});
2017-07-03 23:48:35 +02:00
});
2017-09-19 17:47:24 +02:00
// route to check whether spee.ch has published to a channel
2017-09-19 21:54:23 +02:00
app.get('/api/isChannelAvailable/:name', ({ params }, res) => {
2017-09-19 17:47:24 +02:00
checkChannelAvailability(params.name)
.then(result => {
if (result === true) {
res.status(200).json(true);
} else {
2017-09-21 02:14:33 +02:00
logger.debug(`Rejecting '${params.name}' because that channel has already been claimed on spee.ch`);
2017-09-19 17:47:24 +02:00
res.status(200).json(false);
}
})
.catch(error => {
2017-09-19 21:54:23 +02:00
logger.debug('api/isChannelAvailable/ error', error);
2017-09-19 17:47:24 +02:00
res.status(500).json(error);
});
});
// route to run a resolve request on the daemon
2017-07-06 03:26:33 +02:00
app.get('/api/resolve/:uri', ({ headers, ip, originalUrl, params }, res) => {
2017-08-16 20:00:17 +02:00
resolveUri(params.uri)
.then(resolvedUri => {
postToStats('serve', originalUrl, ip, null, null, 'success');
res.status(200).json(resolvedUri);
})
.catch(error => {
2017-11-02 19:50:05 +01:00
errorHandlers.handleApiError('resolve', originalUrl, ip, error, res);
2017-08-16 20:00:17 +02:00
});
});
// route to run a publish request on the daemon
2017-11-03 21:37:23 +01:00
app.post('/api/publish', multipartMiddleware, ({ body, files, ip, originalUrl, user }, res) => {
let file, fileName, filePath, fileType, name, nsfw, license, title, description, thumbnail, anonymous, skipAuth, channelName, channelPassword;
2017-10-10 03:29:40 +02:00
// validate that mandatory parts of the request are present
2017-07-08 01:08:35 +02:00
try {
validateApiPublishRequest(body, files);
2017-07-08 01:08:35 +02:00
} catch (error) {
logger.debug('publish request rejected, insufficient request parameters');
res.status(400).json({success: false, message: error.message});
2017-07-08 01:08:35 +02:00
return;
}
2017-10-10 03:29:40 +02:00
// validate file, name, license, and nsfw
2017-11-03 21:37:23 +01:00
file = files.file;
fileName = file.path.substring(file.path.lastIndexOf('/') + 1);
2017-11-03 21:37:23 +01:00
filePath = file.path;
fileType = file.type;
name = body.name;
nsfw = (body.nsfw === 'true');
try {
validatePublishSubmission(file, name, nsfw);
} catch (error) {
logger.debug('publish request rejected');
res.status(400).json({success: false, message: error.message});
return;
}
// optional inputs
2017-11-03 21:37:23 +01:00
license = body.license || null;
title = body.title || null;
description = body.description || null;
thumbnail = body.thumbnail || null;
anonymous = (body.channelName === 'null') || (body.channelName === undefined);
if (user) {
channelName = user.channelName || null;
} else {
channelName = body.channelName || null;
}
channelPassword = body.channelPassword || null;
skipAuth = false;
// case 1: publish from spee.ch, client logged in
if (user) {
skipAuth = true;
if (anonymous) {
channelName = null;
}
// case 2: publish from api or spee.ch, client not logged in
} else {
if (anonymous) {
skipAuth = true;
channelName = null;
}
}
channelName = cleanseChannelName(channelName);
2017-11-13 20:20:37 +01:00
logger.debug(`/api/publish > name: ${name}, license: ${license} title: "${title}" description: "${description}" channelName: "${channelName}" channelPassword: "${channelPassword}" nsfw: "${nsfw}"`);
// check channel authorization
2017-11-13 20:20:37 +01:00
authenticateOrSkip(skipAuth, channelName, channelPassword)
2017-11-13 19:21:39 +01:00
.then(authenticated => {
if (!authenticated) {
throw new Error('Authentication failed, you do not have access to that channel');
}
// make sure the claim name is available
return checkClaimNameAvailability(name);
})
2017-09-28 19:51:02 +02:00
.then(result => {
if (!result) {
throw new Error('That name is already in use by spee.ch.');
2017-09-28 19:51:02 +02:00
}
// create publish parameters object
2017-10-10 03:29:40 +02:00
return createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName);
2017-09-28 19:51:02 +02:00
})
.then(publishParams => {
logger.debug('publishParams:', publishParams);
// publish the asset
2017-09-28 19:51:02 +02:00
return publish(publishParams, fileName, fileType);
})
2017-08-16 20:00:17 +02:00
.then(result => {
res.status(200).json({
success: true,
message: {
name,
url : `spee.ch/${result.claim_id}/${name}`,
lbryTx: result,
},
});
2017-08-16 20:00:17 +02:00
})
.catch(error => {
2017-11-02 19:50:05 +01:00
errorHandlers.handleApiError('publish', originalUrl, ip, error, res);
2017-08-16 20:00:17 +02:00
});
});
2017-09-28 20:42:29 +02:00
// route to get a short claim id from long claim Id
2017-11-21 00:51:05 +01:00
app.get('/api/shortClaimId/:longId/:name', ({ params }, res) => {
2017-11-04 01:10:08 +01:00
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
2017-09-28 20:42:29 +02:00
.then(shortId => {
res.status(200).json(shortId);
})
.catch(error => {
logger.error('api error getting short channel id', error);
res.status(400).json(error.message);
});
});
// route to get a short channel id from long channel Id
2017-11-02 19:50:05 +01:00
app.get('/api/shortChannelId/:longId/:name', ({ ip, originalUrl, params }, res) => {
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
2017-09-28 20:42:29 +02:00
.then(shortId => {
2017-10-30 05:01:45 +01:00
logger.debug('sending back short channel id', shortId);
2017-09-28 20:42:29 +02:00
res.status(200).json(shortId);
})
.catch(error => {
logger.error('api error getting short channel id', error);
2017-11-02 19:50:05 +01:00
errorHandlers.handleApiError('short channel id', originalUrl, ip, error, res);
2017-09-28 20:42:29 +02:00
});
});
};