From bff65f370400766c746f768c7f01455201445167 Mon Sep 17 00:00:00 2001 From: Alexander Chabin Date: Fri, 12 Jan 2024 07:06:48 +0500 Subject: [PATCH] Fix Notion GUID detection --- build-scripts/prepare-static/index.js | 22 +++++++++------------- build-scripts/prepare-static/utils.js | 5 +++-- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/build-scripts/prepare-static/index.js b/build-scripts/prepare-static/index.js index 92337fa..1c7f823 100644 --- a/build-scripts/prepare-static/index.js +++ b/build-scripts/prepare-static/index.js @@ -17,7 +17,7 @@ async function init() { await clearCachedImages(IMAGES_URLS_PATH); console.log('Get list from Notion') - const items = await getPlacemarksData(); + const items = (await getPlacemarksData()).slice(0,5); const imageUrls = getImagesUrls(items); console.log(`Download ${imageUrls.length} images`); @@ -48,12 +48,10 @@ async function clearCachedImages(path) { async function downloadImages(urls) { const downloadImage = (url) => new Promise((resolve, reject) => { - const [filename] = new URL(url).pathname.split('/').slice(-1); - const ext = filename.split('.').slice(-1)[0]; - const notionGUID = getNotionGUID(url); - const outputFilename = notionGUID ? `${notionGUID}.${ext}` : filename; - - const file = fs.createWriteStream(IMAGES_URLS_PATH + outputFilename); + const ext = new URL(url).pathname.split('.').at(-1); + const guid = getNotionGUID(url); + const filename = `${guid}.${ext}`; + const file = fs.createWriteStream(IMAGES_URLS_PATH + filename); https.get(url, (response) => { response.pipe(file); @@ -61,11 +59,11 @@ async function downloadImages(urls) { file.on('finish', () => { file.close(); - resolve({ id: notionGUID, path: outputFilename }); + resolve({ id: guid, path: filename }); }); file.on('error', (e) => { - console.error(`Not loaded: ${outputFilename}`, e); + console.error(`Not loaded: ${filename}`, e); reject(); }); }); @@ -100,10 +98,8 @@ function saveMetadata(cachPath, items, images) { ...item, images: item.images .map((url) => { - const notionGUID = url.includes('notion-static.com') - ? url.match(/secure.notion-static.com\/(.*)\//)[1] - : ''; - const image = imagesById[notionGUID]; + const guid = getNotionGUID(url); + const image = imagesById[guid]; if (image) { return { diff --git a/build-scripts/prepare-static/utils.js b/build-scripts/prepare-static/utils.js index 66f2ebf..48f661f 100644 --- a/build-scripts/prepare-static/utils.js +++ b/build-scripts/prepare-static/utils.js @@ -5,5 +5,6 @@ export function log(message = '', getMessage = () => '') { }; } -export const getNotionGUID = (url) => - url.includes('notion-static.com') ? url.match(/secure.notion-static.com\/(.*)\//)[1] : ''; +export function getNotionGUID(notionFileUrl) { + return new URL(notionFileUrl).pathname.split('/').at(-2); +}