feat: Upload remote-hosted images on paste (#8301)

* First pass

* fix

* tidy, tidy

* Determine dimensions

* docs

* test getFileNameFromUrl

* PR feedback

* tsc
This commit is contained in:
Tom Moor
2025-01-30 20:24:07 -05:00
committed by GitHub
parent abaeba5952
commit 28aebc9fbf
18 changed files with 304 additions and 106 deletions

View File

@@ -1,7 +1,7 @@
import { bytesToHumanReadable } from "./files";
import { bytesToHumanReadable, getFileNameFromUrl } from "./files";
describe("bytesToHumanReadable", () => {
test("Outputs readable string", () => {
it("outputs readable string", () => {
expect(bytesToHumanReadable(0)).toBe("0 Bytes");
expect(bytesToHumanReadable(0.0)).toBe("0 Bytes");
expect(bytesToHumanReadable(33)).toBe("33 Bytes");
@@ -15,3 +15,20 @@ describe("bytesToHumanReadable", () => {
expect(bytesToHumanReadable(undefined)).toBe("0 Bytes");
});
});
describe("getFileNameFromUrl", () => {
it("returns the filename from a URL", () => {
expect(getFileNameFromUrl("https://example.com/file")).toBe("file");
expect(getFileNameFromUrl("https://example.com/file.txt")).toBe("file.txt");
expect(
getFileNameFromUrl("https://example.com/file.txt?query=string")
).toBe("file.txt");
expect(getFileNameFromUrl("https://example.com/file.txt#hash")).toBe(
"file.txt"
);
expect(
getFileNameFromUrl("https://example.com/file.txt?query=string#hash")
).toBe("file.txt");
expect(getFileNameFromUrl("https://example.com/")).toBe("");
});
});

View File

@@ -112,3 +112,20 @@ export function getEventFiles(
? Array.prototype.slice.call(event.target.files)
: [];
}
/**
* Get the likely filename from a URL
*
* @param url The URL to get the filename from
* @returns The filename or null if it could not be determined
*/
export function getFileNameFromUrl(url: string) {
try {
const urlObj = new URL(url);
const pathname = urlObj.pathname;
const filename = pathname.substring(pathname.lastIndexOf("/") + 1);
return filename;
} catch (error) {
return null;
}
}