Added process for recognizing yt urls
This commit is contained in:
parent
52cf92b5e5
commit
8eaa52d83d
|
@ -5,6 +5,7 @@ require('dotenv').config()
|
||||||
let config = {
|
let config = {
|
||||||
domain: process.env.DOMAIN || "localhost:3000",
|
domain: process.env.DOMAIN || "localhost:3000",
|
||||||
https: (process.env.SSL === 'true') || false,
|
https: (process.env.SSL === 'true') || false,
|
||||||
|
recognizeProviders: true,
|
||||||
getBaseUrl(){
|
getBaseUrl(){
|
||||||
if(config.https){
|
if(config.https){
|
||||||
return `https://${config.domain}`;
|
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 it doesn't exist: Generate a new code and proceed to creating a new db entry.
|
||||||
*/
|
*/
|
||||||
if (!shortcode) {
|
if (!shortcode) {
|
||||||
|
if(config.recognizeProviders){
|
||||||
|
const response = checkKnownProviders(target);
|
||||||
|
if(response){
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
const exists = await knex.select('shortcode')
|
const exists = await knex.select('shortcode')
|
||||||
.from('urls')
|
.from('urls')
|
||||||
.where('target', '=', target)
|
.where('target', '=', target)
|
||||||
|
@ -119,7 +126,23 @@ fastify.post('/new', { newUrlSchema }, async (req, res) => {
|
||||||
shortcode,
|
shortcode,
|
||||||
target
|
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!
|
// Run the server!
|
||||||
const start = async () => {
|
const start = async () => {
|
||||||
|
|
Loading…
Reference in New Issue