Fix bug where videos couldn't be uploaded from a direct link

Possibly a firefox-only issue.
pull/16/head
neobooru 2022-01-30 19:10:12 +01:00
parent 5322d0158a
commit cbeea1c665
3 changed files with 34 additions and 3 deletions

View File

@ -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", () => { test("guessContentType", () => {
expect(guessContentType("video.mp4")).toBe("video"); expect(guessContentType("video.mp4")).toBe("video");

View File

@ -39,9 +39,17 @@ export const videoExtensions = [
]; ];
export function getUrlExtension(url: string) { export function getUrlExtension(url: string) {
// https://stackoverflow.com/a/47767860
return url.split(/[#?]/)[0].split(".").pop()!.trim(); 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 { export function guessContentType(url: string): ContentType {
if (url != undefined) { if (url != undefined) {
const ext = getUrlExtension(url); const ext = getUrlExtension(url);

View File

@ -1,4 +1,5 @@
import { ScrapeEngine, ScrapeResult, ScrapedPost, ContentType } from "../ScrapeEngine"; import { ScrapeEngine, ScrapeResult, ScrapedPost, ContentType } from "../ScrapeEngine";
import { guessContentType } from "../Utility";
class MediaElement { class MediaElement {
constructor( constructor(
@ -16,6 +17,21 @@ export default class Fallback implements ScrapeEngine {
} }
scrapeDocument(document: Document): ScrapeResult { 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 <source> to a <video> element
// when going to a direct link. It looks like chromium-based browsers do not have this issue.
if (guessContentType(document.location.href) == "video") {
let post = new ScrapedPost();
post.pageUrl = document.location.href;
post.contentUrl = document.location.href;
post.contentType = "video";
result.posts.push(post);
return result;
}
// Get all img elements. // Get all img elements.
const largestImgElement = Array.from(document.getElementsByTagName("img")).map( const largestImgElement = Array.from(document.getElementsByTagName("img")).map(
(x) => new MediaElement("image", x.src, x.width * x.height) (x) => new MediaElement("image", x.src, x.width * x.height)
@ -33,8 +49,6 @@ export default class Fallback implements ScrapeEngine {
a?.size > b?.size ? a : b a?.size > b?.size ? a : b
); );
let result = new ScrapeResult(this.name);
if (largestMediaElement) { if (largestMediaElement) {
let post = new ScrapedPost(); let post = new ScrapedPost();
post.pageUrl = document.location.href; post.pageUrl = document.location.href;