From 47c15bce66db06f0dd52d52030c118d94e2fe74b Mon Sep 17 00:00:00 2001 From: iskaktoltay Date: Mon, 5 Aug 2024 19:18:20 +0500 Subject: [PATCH] added media files extensions to urls in exported markdown --- frontend/apps/desktop/src/main.ts | 125 ++++++++++------- .../apps/desktop/src/save-markdown-file.tsx | 131 +++++++++++------- 2 files changed, 155 insertions(+), 101 deletions(-) diff --git a/frontend/apps/desktop/src/main.ts b/frontend/apps/desktop/src/main.ts index ebc1c545f..1b87b72e1 100644 --- a/frontend/apps/desktop/src/main.ts +++ b/frontend/apps/desktop/src/main.ts @@ -165,7 +165,7 @@ ipcMain.on( fs.mkdirSync(filePath) } - documents.forEach(({title, markdown}) => { + for (const {title, markdown} of documents) { const {markdownContent, mediaFiles} = markdown const camelTitle = title .split(' ') @@ -187,64 +187,87 @@ ipcMain.on( fs.mkdirSync(mediaDir) } - // Save Markdown file + let updatedMarkdownContent = markdownContent + + const uploadMediaFile = ({ + url, + filename, + }: { + url: string + filename: string + }) => { + return new Promise((resolve, reject) => { + const regex = /ipfs:\/\/(.+)/ + const match = url.match(regex) + const cid = match ? match[1] : null + const request = net.request(`${API_HTTP_URL}/ipfs/${cid}`) + + request.on('response', (response) => { + const mimeType = response.headers['content-type'] + const extension = Array.isArray(mimeType) + ? mime.extension(mimeType[0]) + : mime.extension(mimeType) + const filenameWithExt = `${filename}.${extension}` + if (response.statusCode === 200) { + const chunks: Buffer[] = [] + + response.on('data', (chunk) => { + chunks.push(chunk) + }) + + response.on('end', () => { + const data = Buffer.concat(chunks) + if (!data || data.length === 0) { + reject(`Error: No data received for ${filenameWithExt}`) + return + } + + const mediaFilePath = path.join(mediaDir, filenameWithExt) + try { + fs.writeFileSync(mediaFilePath, data) + debug(`Media file successfully saved: ${mediaFilePath}`) + // Update the markdown content with the correct file name + updatedMarkdownContent = updatedMarkdownContent.replace( + filename, + filenameWithExt, + ) + resolve() + } catch (e) { + reject(e) + } + }) + } else { + reject(`Error: Invalid status code ${response.statusCode}`) + } + }) + + request.on('error', (err) => { + reject(err.message) + }) + + request.end() + }) + } + + // Process all media files + const uploadPromises = mediaFiles.map(uploadMediaFile) + try { + await Promise.all(uploadPromises) + } catch (e) { + error('Error processing media files:', e) + } + + // Save the updated Markdown file after all media files are processed const markdownFilePath = path.join(documentDir, `${camelTitle}.md`) - fs.writeFile(markdownFilePath, markdownContent, (err) => { + fs.writeFile(markdownFilePath, updatedMarkdownContent, (err) => { if (err) { error('Error saving file:', err) return } debug('Markdown file successfully saved:', markdownFilePath) }) - - // Save Media files using CID - for (const {url, filename} of mediaFiles) { - const regex = /ipfs:\/\/(.+)/ - const match = url.match(regex) - const cid = match ? match[1] : null - const request = net.request(`${API_HTTP_URL}/ipfs/${cid}`) - - request.on('response', (response) => { - const mimeType = response.headers['content-type'] - const extension = Array.isArray(mimeType) - ? mime.extension(mimeType[0]) - : mime.extension(mimeType) - const filenameWithExt = `${filename}.${extension}` - if (response.statusCode === 200) { - const chunks: Buffer[] = [] - - response.on('data', (chunk) => { - chunks.push(chunk) - }) - - response.on('end', () => { - const data = Buffer.concat(chunks) - if (!data || data.length === 0) { - error(`Error: No data received for ${filenameWithExt}`) - return - } - - const mediaFilePath = path.join(mediaDir, filenameWithExt) - try { - fs.writeFileSync(mediaFilePath, data) - debug(`Media file successfully saved: ${mediaFilePath}`) - } catch (e) { - error(`Failed to save media file ${filenameWithExt}`, e) - } - }) - } else { - error(`Error: Invalid status code ${response.statusCode}`) - } - }) - - request.on('error', (err) => { - error('Error:', err.message) - }) - - request.end() - } } - }) + } } }, ) diff --git a/frontend/apps/desktop/src/save-markdown-file.tsx b/frontend/apps/desktop/src/save-markdown-file.tsx index 399f3973a..8ea940e43 100644 --- a/frontend/apps/desktop/src/save-markdown-file.tsx +++ b/frontend/apps/desktop/src/save-markdown-file.tsx @@ -6,7 +6,14 @@ import path from 'node:path' const {debug, error} = console -export async function saveMarkdownFile(event, args) { +export async function saveMarkdownFile( + event: any, + args: { + title: string + markdownContent: string + mediaFiles: {url: string; filename: string}[] + }, +) { const {title, markdownContent, mediaFiles} = args const camelTitle = title .split(' ') @@ -36,61 +43,85 @@ export async function saveMarkdownFile(event, args) { fs.mkdirSync(mediaDir) } - // Save Markdown file + let updatedMarkdownContent = markdownContent + + const uploadMediaFile = ({ + url, + filename, + }: { + url: string + filename: string + }) => { + return new Promise((resolve, reject) => { + const regex = /ipfs:\/\/(.+)/ + const match = url.match(regex) + const cid = match ? match[1] : null + const request = net.request(`${API_HTTP_URL}/ipfs/${cid}`) + + request.on('response', (response) => { + const mimeType = response.headers['content-type'] + const extension = Array.isArray(mimeType) + ? mime.extension(mimeType[0]) + : mime.extension(mimeType) + const filenameWithExt = `${filename}.${extension}` + if (response.statusCode === 200) { + const chunks: Buffer[] = [] + + response.on('data', (chunk) => { + chunks.push(chunk) + }) + + response.on('end', () => { + const data = Buffer.concat(chunks) + if (!data || data.length === 0) { + reject(`Error: No data received for ${filenameWithExt}`) + return + } + + const mediaFilePath = path.join(mediaDir, filenameWithExt) + try { + fs.writeFileSync(mediaFilePath, data) + debug(`Media file successfully saved: ${mediaFilePath}`) + // Update the markdown content with the correct file name + updatedMarkdownContent = updatedMarkdownContent.replace( + filename, + filenameWithExt, + ) + resolve() + } catch (e) { + error(`Failed to save media file ${filenameWithExt}`, e) + reject(e) + } + }) + } else { + reject(`Error: Invalid status code ${response.statusCode}`) + } + }) + + request.on('error', (err) => { + reject(err.message) + }) + + request.end() + }) + } + + // Process all media files + const uploadPromises = mediaFiles.map(uploadMediaFile) + try { + await Promise.all(uploadPromises) + } catch (e) { + error('Error processing media files:', e) + } + + // Save the updated Markdown file after all media files are processed const markdownFilePath = path.join(documentDir, `${camelTitle}.md`) - fs.writeFile(markdownFilePath, markdownContent, (err) => { + fs.writeFile(markdownFilePath, updatedMarkdownContent, (err) => { if (err) { error('Error saving file:', err) return } debug('Markdown file successfully saved:', markdownFilePath) }) - - // Save Media files using CID - for (const {url, filename} of mediaFiles) { - const regex = /ipfs:\/\/(.+)/ - const match = url.match(regex) - const cid = match ? match[1] : null - const request = net.request(`${API_HTTP_URL}/ipfs/${cid}`) - - request.on('response', (response) => { - const mimeType = response.headers['content-type'] - const extension = Array.isArray(mimeType) - ? mime.extension(mimeType[0]) - : mime.extension(mimeType) - const filenameWithExt = `${filename}.${extension}` - if (response.statusCode === 200) { - const chunks: Buffer[] = [] - - response.on('data', (chunk) => { - chunks.push(chunk) - }) - - response.on('end', () => { - const data = Buffer.concat(chunks) - if (!data || data.length === 0) { - error(`Error: No data received for ${filenameWithExt}`) - return - } - - const mediaFilePath = path.join(mediaDir, filenameWithExt) - try { - fs.writeFileSync(mediaFilePath, data) - debug(`Media file successfully saved: ${mediaFilePath}`) - } catch (e) { - error(`Failed to save media file ${filenameWithExt}`, e) - } - }) - } else { - error(`Error: Invalid status code ${response.statusCode}`) - } - }) - - request.on('error', (err) => { - error('Error:', err.message) - }) - - request.end() - } } }