error checking for parsing things

This commit is contained in:
hyperdefined 2024-07-17 21:28:08 -04:00
parent 5723239f10
commit b219c3e9aa
No known key found for this signature in database
GPG Key ID: EB0B55B31E88AB03

View File

@ -16,7 +16,10 @@ export default async function(obj) {
if (!req) return { error: 'ErrorEmptyDownload' }; if (!req) return { error: 'ErrorEmptyDownload' };
const json = JSON.parse(req); let json;
try {
json = JSON.parse(req);
} catch { return { error: 'ErrorEmptyDownload' }; }
const highestQuality = Object.keys(json.sources)[0]; const highestQuality = Object.keys(json.sources)[0];
const video = json.sources[highestQuality][0].src const video = json.sources[highestQuality][0].src
@ -37,9 +40,17 @@ export default async function(obj) {
if (!req) return { error: 'ErrorEmptyDownload' }; if (!req) return { error: 'ErrorEmptyDownload' };
const title = req.match(/"name"\s*:\s*"([^"]+)"/)?.[1]; const titleMatch = req.match(/"name"\s*:\s*"([^"]+)"/);
const artist = req.match(/"artist"\s*:\s*"([^"]+)"/)?.[1]; const artistMatch = req.match(/"artist"\s*:\s*"([^"]+)"/);
const url = req.match(/"filename"\s*:\s*"([^"]+)"/)?.[1]?.replace(/\\\//g, '/'); const urlMatch = req.match(/"filename"\s*:\s*"([^"]+)"/);
if (!titleMatch || !artistMatch || !urlMatch) {
return { error: 'ErrorEmptyDownload' };
}
const title = titleMatch[1];
const artist = artistMatch[1];
const url = urlMatch[1].replace(/\\\//g, '/');
let fileMetadata = { let fileMetadata = {
title: cleanString(decodeURIComponent(title.trim())), title: cleanString(decodeURIComponent(title.trim())),
artist: cleanString(decodeURIComponent(artist.trim())), artist: cleanString(decodeURIComponent(artist.trim())),
@ -51,7 +62,8 @@ export default async function(obj) {
service: "newgrounds", service: "newgrounds",
id: obj.id, id: obj.id,
title: fileMetadata.title, title: fileMetadata.title,
author: fileMetadata.artist author: fileMetadata.artist,
fileMetadata
}, },
} }
} }