Added process for recognizing yt urls

This commit is contained in:
Nicolai Ort 2021-08-12 21:23:40 +02:00
parent 52cf92b5e5
commit 8eaa52d83d
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
1 changed files with 24 additions and 1 deletions

View File

@ -5,6 +5,7 @@ require('dotenv').config()
let config = {
domain: process.env.DOMAIN || "localhost:3000",
https: (process.env.SSL === 'true') || false,
recognizeProviders: true,
getBaseUrl(){
if(config.https){
return `https://${config.domain}`;
@ -81,6 +82,12 @@ fastify.post('/new', { newUrlSchema }, async (req, res) => {
* If it doesn't exist: Generate a new code and proceed to creating a new db entry.
*/
if (!shortcode) {
if(config.recognizeProviders){
const response = checkKnownProviders(target);
if(response){
return response;
}
}
const exists = await knex.select('shortcode')
.from('urls')
.where('target', '=', target)
@ -119,7 +126,23 @@ fastify.post('/new', { newUrlSchema }, async (req, res) => {
shortcode,
target
}
})
});
/**
* Checks for some default providers with custom url schemes (amazon and youtube r/n)
* @param {string} target The target URL
* @returns Standard shortening response if provider recognized or null
*/
function checkKnownProviders(target){
if(/^(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/.test(target)){
const shortcode = `yt/${target.match(/(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/)[1]}`
return {
url: `${config.getBaseUrl()}/${shortcode}`,
shortcode,
target
}
}
}
// Run the server!
const start = async () => {