Skip to content

Commit

Permalink
refactor shortlink
Browse files Browse the repository at this point in the history
  • Loading branch information
gegehprast committed Jul 3, 2020
1 parent 15650d5 commit b937e96
Showing 1 changed file with 62 additions and 28 deletions.
90 changes: 62 additions & 28 deletions Shortlinks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const Handler = require('../exceptions/Handler')
const ShortlinkModel = require('../Models/Shortlink')
const Queue = require('../Queue')

const WITH_DATABASE = process.env.WITH_DATABASE === 'true'
const shortlinkFiles = fs.readdirSync(path.join(__dirname, './')).filter(file => file !== 'index.js' && file.endsWith('.js'))

const shortlinks = []

for (const file of shortlinkFiles) {
Expand All @@ -17,26 +17,56 @@ class Shortlink {
this.shorterners = shortlinks
}

async parse(link, options = {}) {
if (process.env.WITH_DATABASE === 'true') {
const fromDB = await ShortlinkModel.findOne({
original: link
})
/**
* Save new parsed shortlink to the database.
*
* @param {String} originalLink
* @param {String} parsedLink
*/
async cacheShortlink(originalLink, parsedLink) {
const newParsed = new ShortlinkModel({
original: originalLink,
parsed: parsedLink
})

await newParsed.save()
}

if (fromDB) {
return {
success: true,
cached: true,
id: fromDB._id,
original: fromDB.original,
url: fromDB.parsed,
createdAt: fromDB.createdAt,
updatedAt: fromDB.updatedAt,
}
}
/**
* Get parsed shortlink by the original link from the database.
*
* @param {String} link
*/
async getCachedShortlink(link) {
return await ShortlinkModel.findOne({
original: link
})
}

/**
* Response with the cached shortlink.
*
* @param cachedShortlink
*/
async responseWithCached(cachedShortlink) {
return {
success: true,
cached: true,
id: cachedShortlink._id,
original: cachedShortlink.original,
url: cachedShortlink.parsed,
createdAt: cachedShortlink.createdAt,
updatedAt: cachedShortlink.updatedAt,
}
}

let shorterner = null, parsed
/**
* Select the correct parser for the link.
*
* @param {String} link
*/
selectParser(link) {
let shorterner = null

for (const i of this.shorterners) {
if (Array.isArray(i.marker)) {
Expand All @@ -58,10 +88,21 @@ class Shortlink {
}
}

if (!shorterner) {
return Handler.error('Error: Unknown shortlink.')
return shorterner
}

async parse(link, options = {}) {
if (WITH_DATABASE) {
const cachedShortlink = await this.getCachedShortlink(link)

if (cachedShortlink) return this.responseWithCached(cachedShortlink)
}

let parsed
let shorterner = this.selectParser(link)

if (!shorterner) return Handler.error('Error: Unknown shortlink.')

if (options.queue) {
const job = Queue.register(async () => {
return await shorterner.parse(link)
Expand All @@ -76,14 +117,7 @@ class Shortlink {
parsed.success = true
parsed.cached = false

if (process.env.WITH_DATABASE === 'true') {
const newParsed = new ShortlinkModel({
original: link,
parsed: parsed.url
})

await newParsed.save()
}
if (WITH_DATABASE) await this.cacheShortlink(link, parsed.url)
}

return parsed
Expand Down

0 comments on commit b937e96

Please sign in to comment.