|
| 1 | +import * as fs from "fs-extra"; |
| 2 | +import FileType from "file-type"; |
| 3 | +import fetch from "node-fetch"; |
| 4 | + |
| 5 | +let existingImagesNotSeenYetInPull: string[] = []; |
| 6 | +let imageOutputPath = "not set yet"; |
| 7 | +let imagePrefix = "not set yet"; |
| 8 | + |
| 9 | +export async function initImageHandling( |
| 10 | + prefix: string, |
| 11 | + outputPath: string |
| 12 | +): Promise<void> { |
| 13 | + // If they gave us a trailing slash, remove it because we add it back later. |
| 14 | + // Note that it's up to the caller to have a *leading* slash or not. |
| 15 | + imagePrefix = prefix.replace(/\/$/, ""); |
| 16 | + imageOutputPath = outputPath; |
| 17 | + |
| 18 | + console.log("initimg " + imagePrefix + ", " + imageOutputPath); |
| 19 | + // Currently we don't delete the image directory, because if an image |
| 20 | + // changes, it gets a new id. This way can then prevent downloading |
| 21 | + // and image after the 1st time. The downside is currently we don't |
| 22 | + // have the smarts to remove unused images. |
| 23 | + await fs.mkdir(imageOutputPath, { recursive: true }); |
| 24 | +} |
| 25 | + |
| 26 | +async function saveImage( |
| 27 | + url: string, |
| 28 | + imageFolderPath: string |
| 29 | +): Promise<string> { |
| 30 | + const response = await fetch(url); |
| 31 | + const arrayBuffer = await response.arrayBuffer(); |
| 32 | + const buffer = Buffer.from(arrayBuffer); |
| 33 | + const fileType = await FileType.fromBuffer(buffer); |
| 34 | + if (fileType?.ext) { |
| 35 | + // Since most images come from pasting screenshots, there isn't normally a filename. That's fine, we just make a hash of the url |
| 36 | + // 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: |
| 37 | + // 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 |
| 38 | + |
| 39 | + let thingToHash = url; |
| 40 | + const m = /.*secure\.notion-static\.com\/(.*)\//gm.exec(url); |
| 41 | + if (m && m.length > 1) { |
| 42 | + thingToHash = m[1]; |
| 43 | + } |
| 44 | + |
| 45 | + const hash = hashOfString(thingToHash); |
| 46 | + const outputFileName = `${hash}.${fileType.ext}`; |
| 47 | + const path = imageFolderPath + "/" + outputFileName; |
| 48 | + imageWasSeen(path); |
| 49 | + if (!fs.pathExistsSync(path)) { |
| 50 | + // // I think that this ok that this is writing async as we continue |
| 51 | + console.log("Adding image " + path); |
| 52 | + fs.createWriteStream(path).write(buffer); |
| 53 | + } |
| 54 | + return outputFileName; |
| 55 | + } else { |
| 56 | + console.error( |
| 57 | + `Something wrong with the filetype extension on the blob we got from ${url}` |
| 58 | + ); |
| 59 | + return "error"; |
| 60 | + } |
| 61 | +} |
| 62 | +function hashOfString(s: string) { |
| 63 | + let hash = 0; |
| 64 | + for (let i = 0; i < s.length; ++i) |
| 65 | + hash = Math.imul(31, hash) + s.charCodeAt(i); |
| 66 | + |
| 67 | + return Math.abs(hash); |
| 68 | +} |
| 69 | + |
| 70 | +// Download the image if we don't have it, give it a good name, and |
| 71 | +// change the src to point to our copy of the image. |
| 72 | +export async function processImageBlock(b: any): Promise<void> { |
| 73 | + let url = ""; |
| 74 | + if ("file" in b.image) { |
| 75 | + url = b.image.file.url; // image saved on notion (actually AWS) |
| 76 | + } else { |
| 77 | + url = b.image.external.url; // image still pointing somewhere else. I've see this happen when copying a Google Doc into Notion. Notion kep pointing at the google doc. |
| 78 | + } |
| 79 | + |
| 80 | + const newPath = imagePrefix + "/" + (await saveImage(url, imageOutputPath)); |
| 81 | + |
| 82 | + // change the src to point to our copy of the image |
| 83 | + if ("file" in b.image) { |
| 84 | + b.image.file.url = newPath; |
| 85 | + } else { |
| 86 | + b.image.external.url = newPath; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function imageWasSeen(path: string) { |
| 91 | + existingImagesNotSeenYetInPull = existingImagesNotSeenYetInPull.filter( |
| 92 | + p => p !== path |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +export async function cleanupOldImages(): Promise<void> { |
| 97 | + for (const p of existingImagesNotSeenYetInPull) { |
| 98 | + console.log(`Removing old image: ${p}`); |
| 99 | + await fs.rm(p); |
| 100 | + } |
| 101 | +} |
0 commit comments