From cbeea1c66530b4baf4c7ff43595053283a0c5e04 Mon Sep 17 00:00:00 2001 From: neobooru <50623835+neobooru@users.noreply.github.com> Date: Sun, 30 Jan 2022 19:10:12 +0100 Subject: [PATCH] Fix bug where videos couldn't be uploaded from a direct link Possibly a firefox-only issue. --- src/Utility.test.ts | 11 ++++++++++- src/Utility.ts | 8 ++++++++ src/engines/Fallback.ts | 18 ++++++++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Utility.test.ts b/src/Utility.test.ts index 56b0465..c77f2a3 100644 --- a/src/Utility.test.ts +++ b/src/Utility.test.ts @@ -1,4 +1,13 @@ -import { guessContentType } from "./Utility"; +import { getUrlExtension, guessContentType } from "./Utility"; + +test("getUrlExtension", () => { + expect(getUrlExtension("image.png")).toBe("png"); + expect(getUrlExtension("image.jpeg?34")).toBe("jpeg"); + expect(getUrlExtension("image.php?q=test&p=123%204")).toBe("php"); + + // I would probably rather have this return an empty string, but maybe some code relies on this functionality? + expect(getUrlExtension("http://site/index")).toBe("http://site/index"); +}); test("guessContentType", () => { expect(guessContentType("video.mp4")).toBe("video"); diff --git a/src/Utility.ts b/src/Utility.ts index c33062b..53a8efc 100644 --- a/src/Utility.ts +++ b/src/Utility.ts @@ -39,9 +39,17 @@ export const videoExtensions = [ ]; export function getUrlExtension(url: string) { + // https://stackoverflow.com/a/47767860 return url.split(/[#?]/)[0].split(".").pop()!.trim(); } +/** + * Returns "image" when the guessed type is NOT a video. + * This means that it will also return "image" when the content type + * could not be guessed! + * @param url + * @returns "image" | "video" + */ export function guessContentType(url: string): ContentType { if (url != undefined) { const ext = getUrlExtension(url); diff --git a/src/engines/Fallback.ts b/src/engines/Fallback.ts index 7edb5ea..76fddef 100644 --- a/src/engines/Fallback.ts +++ b/src/engines/Fallback.ts @@ -1,4 +1,5 @@ import { ScrapeEngine, ScrapeResult, ScrapedPost, ContentType } from "../ScrapeEngine"; +import { guessContentType } from "../Utility"; class MediaElement { constructor( @@ -16,6 +17,21 @@ export default class Fallback implements ScrapeEngine { } scrapeDocument(document: Document): ScrapeResult { + let result = new ScrapeResult(this.name); + + // Check if the current page is a link to a video file. + // If it is then use the document.location.href as media source and immediately return. + // This is needed because firefox does not add a to a