Skip to content

Commit 7f1fd40

Browse files
committed
fix: fallback to getting image file extension from url
1 parent a73e040 commit 7f1fd40

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

src/MakeImagePersistencePlan.ts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,51 @@ export function makeImagePersistencePlan(
88
imageOutputRootPath: string,
99
imagePrefix: string
1010
): void {
11-
if (imageSet.fileType?.ext) {
12-
// Since most images come from pasting screenshots, there isn't normally a filename. That's fine, we just make a hash of the url
13-
// Images that are stored by notion come to us with a complex url that changes over time, so we pick out the UUID that doesn't change. Example:
14-
// https://s3.us-west-2.amazonaws.com/secure.notion-static.com/d1058f46-4d2f-4292-8388-4ad393383439/Untitled.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220516%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220516T233630Z&X-Amz-Expires=3600&X-Amz-Signature=f215704094fcc884d37073b0b108cf6d1c9da9b7d57a898da38bc30c30b4c4b5&X-Amz-SignedHeaders=host&x-id=GetObject
15-
// But around Sept 2023, they changed the url to be something like:
16-
// https://prod-files-secure.s3.us-west-2.amazonaws.com/d9a2b712-cf69-4bd6-9d65-87a4ceeacca2/d1bcdc8c-b065-4e40-9a11-392aabeb220e/Untitled.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20230915%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20230915T161258Z&X-Amz-Expires=3600&X-Amz-Signature=28fca48e65fba86d539c3c4b7676fce1fa0857aa194f7b33dd4a468ecca6ab24&X-Amz-SignedHeaders=host&x-id=GetObject
17-
// The thing we want is the last UUID before the ?
11+
const urlBeforeQuery = imageSet.primaryUrl.split("?")[0];
1812

19-
const urlBeforeQuery = imageSet.primaryUrl.split("?")[0];
20-
const thingToHash = findLastUuid(urlBeforeQuery) ?? urlBeforeQuery;
13+
let imageFileExtension: string | undefined = imageSet.fileType?.ext;
14+
if (!imageFileExtension) {
15+
// Try to get the extension from the url
16+
imageFileExtension = urlBeforeQuery.split(".").pop();
2117

22-
const hash = hashOfString(thingToHash);
23-
imageSet.outputFileName = `${hash}.${imageSet.fileType.ext}`;
24-
25-
imageSet.primaryFileOutputPath = Path.posix.join(
26-
imageOutputRootPath?.length > 0
27-
? imageOutputRootPath
28-
: imageSet.pathToParentDocument!,
29-
imageSet.outputFileName
30-
);
31-
32-
if (imageOutputRootPath && imageSet.localizedUrls.length) {
18+
if (!imageFileExtension) {
3319
error(
34-
"imageOutputPath was declared, but one or more localizedUrls were found too. If you are going to localize screenshots, then you can't declare an imageOutputPath."
20+
`Something wrong with the filetype extension on the blob we got from ${imageSet.primaryUrl}`
3521
);
3622
exit(1);
3723
}
24+
}
25+
26+
// Since most images come from pasting screenshots, there isn't normally a filename. That's fine, we just make a hash of the url
27+
// Images that are stored by notion come to us with a complex url that changes over time, so we pick out the UUID that doesn't change. Example:
28+
// https://s3.us-west-2.amazonaws.com/secure.notion-static.com/d1058f46-4d2f-4292-8388-4ad393383439/Untitled.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220516%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220516T233630Z&X-Amz-Expires=3600&X-Amz-Signature=f215704094fcc884d37073b0b108cf6d1c9da9b7d57a898da38bc30c30b4c4b5&X-Amz-SignedHeaders=host&x-id=GetObject
29+
// But around Sept 2023, they changed the url to be something like:
30+
// https://prod-files-secure.s3.us-west-2.amazonaws.com/d9a2b712-cf69-4bd6-9d65-87a4ceeacca2/d1bcdc8c-b065-4e40-9a11-392aabeb220e/Untitled.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20230915%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20230915T161258Z&X-Amz-Expires=3600&X-Amz-Signature=28fca48e65fba86d539c3c4b7676fce1fa0857aa194f7b33dd4a468ecca6ab24&X-Amz-SignedHeaders=host&x-id=GetObject
31+
// The thing we want is the last UUID before the ?
3832

39-
imageSet.filePathToUseInMarkdown =
40-
(imagePrefix?.length > 0 ? imagePrefix : ".") +
41-
"/" +
42-
imageSet.outputFileName;
43-
} else {
33+
const thingToHash = findLastUuid(urlBeforeQuery) ?? urlBeforeQuery;
34+
35+
const hash = hashOfString(thingToHash);
36+
imageSet.outputFileName = `${hash}.${imageFileExtension}`;
37+
38+
imageSet.primaryFileOutputPath = Path.posix.join(
39+
imageOutputRootPath?.length > 0
40+
? imageOutputRootPath
41+
: imageSet.pathToParentDocument!,
42+
imageSet.outputFileName
43+
);
44+
45+
if (imageOutputRootPath && imageSet.localizedUrls.length) {
4446
error(
45-
`Something wrong with the filetype extension on the blob we got from ${imageSet.primaryUrl}`
47+
"imageOutputPath was declared, but one or more localizedUrls were found too. If you are going to localize screenshots, then you can't declare an imageOutputPath."
4648
);
4749
exit(1);
4850
}
51+
52+
imageSet.filePathToUseInMarkdown =
53+
(imagePrefix?.length > 0 ? imagePrefix : ".") +
54+
"/" +
55+
imageSet.outputFileName;
4956
}
5057

5158
function findLastUuid(url: string): string | null {

src/makeImagePersistencePlan.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ test("primary file with defaults for image output path and prefix", () => {
4242
);
4343
expect(imageSet.filePathToUseInMarkdown).toBe(`./${expectedHash}.png`);
4444
});
45+
test("falls back to getting file extension from url if not in fileType", () => {
46+
const imageSet: ImageSet = {
47+
primaryUrl: "https://s3.us-west-2.amazonaws.com/primaryImage.png",
48+
localizedUrls: [],
49+
pathToParentDocument: "/pathToParentSomewhere/",
50+
};
51+
makeImagePersistencePlan(imageSet, "", "");
52+
const expectedHash = hashOfString(
53+
"https://s3.us-west-2.amazonaws.com/primaryImage.png"
54+
);
55+
expect(imageSet.outputFileName).toBe(`${expectedHash}.png`);
56+
});
4557

4658
test("properly extract UUID from old-style notion image url", () => {
4759
const imageSet: ImageSet = {

0 commit comments

Comments
 (0)