added metadata on url model

This commit is contained in:
2023-01-13 18:54:58 -03:00
parent 856e01d339
commit 63ec99e6a5
4 changed files with 12 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
/* eslint-disable quotes */ /* eslint-disable quotes */
import getMetaData from 'metadata-scraper'
import { decrypt } from '../../scripts/crypto' import { decrypt } from '../../scripts/crypto'
import generateId from '../../scripts/generateId' import generateId from '../../scripts/generateId'
import isUrl from '../../scripts/isUrl' import isUrl from '../../scripts/isUrl'
@@ -35,11 +36,13 @@ export async function createUrl(
if ( if (
decrypt({ content: user.password, iv: user.crypto.iv }) === password decrypt({ content: user.password, iv: user.crypto.iv }) === password
) { ) {
const metaData = await getMetaData(url)
const newUrl: IUrl = { const newUrl: IUrl = {
id: generateId(5), id: generateId(5),
url, url,
dateCreated: new Date(), dateCreated: new Date(),
uploadedByUser: user.id, uploadedByUser: user.id,
metaData,
} }
return UrlModel.create(newUrl) return UrlModel.create(newUrl)
} }

View File

@@ -5,6 +5,7 @@ export interface IUrl {
url: string url: string
dateCreated: Date dateCreated: Date
uploadedByUser: string uploadedByUser: string
metaData: Object
} }
export const urlSchema = new Schema<IUrl>( export const urlSchema = new Schema<IUrl>(
@@ -27,6 +28,10 @@ export const urlSchema = new Schema<IUrl>(
type: String, type: String,
required: true, required: true,
}, },
metaData: {
type: Object,
required: true,
},
}, },
{ collection: 'url', timestamps: true } { collection: 'url', timestamps: true }
) )

View File

@@ -1,4 +1,4 @@
export default function (metaData: any) { export default function (metaData: any, url: string) {
return ` return `
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
@@ -10,7 +10,7 @@ export default function (metaData: any) {
</head> </head>
<body> <body>
<script> <script>
window.location.href = "${metaData.url}"; window.location.href = "${url}";
</script> </script>
</body> </body>
</html> </html>

View File

@@ -1,5 +1,4 @@
import { Router, Request, Response } from 'express' import { Router, Request, Response } from 'express'
import getMetaData from 'metadata-scraper'
import { getUrlById } from '../../models/queries/Url.queries' import { getUrlById } from '../../models/queries/Url.queries'
import generateGetUrlPage from '../../pages/getUrl/generateGetUrlPage' import generateGetUrlPage from '../../pages/getUrl/generateGetUrlPage'
import mongoErrorService from '../../services/mongoErrorService' import mongoErrorService from '../../services/mongoErrorService'
@@ -14,11 +13,10 @@ router.get('/:urlId', async (req: Request, res: Response) => {
}) })
} }
try { try {
const { url } = await getUrlById({ const { metaData, url } = await getUrlById({
id: urlId, id: urlId,
}) })
const metaData = await getMetaData(url) res.send(generateGetUrlPage(metaData, url))
res.send(generateGetUrlPage(metaData))
} catch (error) { } catch (error) {
console.log(error) console.log(error)
res.status(400).json({ res.status(400).json({