Skip to content

Commit

Permalink
Fix Notion GUID detection
Browse files Browse the repository at this point in the history
  • Loading branch information
sashachabin committed Jan 12, 2024
1 parent 271dfbf commit bff65f3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
22 changes: 9 additions & 13 deletions build-scripts/prepare-static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -48,24 +48,22 @@ 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);
});

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();
});
});
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions build-scripts/prepare-static/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

1 comment on commit bff65f3

@ekbdev
Copy link

@ekbdev ekbdev commented on bff65f3 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for design-map ready!

✅ Preview
https://design-1xmvbdkow-ekbdev.vercel.app
https://emap-ekbdev-fix-save-images.vercel.app

Built with commit bff65f3.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.