-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made exporting media files in a separate media folder
- Loading branch information
1 parent
4888b49
commit 748078a
Showing
6 changed files
with
159 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import {API_HTTP_URL} from '@mintter/shared' | ||
import {app, dialog, net} from 'electron' | ||
import fs from 'fs' | ||
import path from 'node:path' | ||
|
||
const {debug, error} = console | ||
|
||
export async function saveMarkdownFile(event, args) { | ||
const {title, markdownContent, mediaFiles} = args | ||
const formattedTitle = title | ||
.split(' ') | ||
.map( | ||
(word: string) => | ||
word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(), | ||
) | ||
.join('') | ||
|
||
const {filePath} = await dialog.showSaveDialog({ | ||
title: 'Save Markdown and Media', | ||
defaultPath: path.join(app.getPath('documents'), formattedTitle), | ||
buttonLabel: 'Save', | ||
filters: [{name: 'Markdown Files', extensions: ['md']}], | ||
}) | ||
|
||
if (filePath) { | ||
const dir = path.dirname(filePath) | ||
const documentDir = path.join(dir, formattedTitle) | ||
|
||
if (!fs.existsSync(documentDir)) { | ||
fs.mkdirSync(documentDir) | ||
} | ||
|
||
const mediaDir = path.join(documentDir, 'media') | ||
if (!fs.existsSync(mediaDir)) { | ||
fs.mkdirSync(mediaDir) | ||
} | ||
|
||
// Save Markdown file | ||
const markdownFilePath = path.join(documentDir, `${formattedTitle}.md`) | ||
fs.writeFile(markdownFilePath, markdownContent, (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) => { | ||
debug(response) | ||
if (response.statusCode === 200) { | ||
const chunks: Buffer[] = [] | ||
|
||
response.on('data', (chunk) => { | ||
chunks.push(chunk) | ||
}) | ||
|
||
response.on('end', () => { | ||
const data = Buffer.concat(chunks) | ||
debug('~~~~~~~~~~~~~HEREEEEEEEEEEEEEEEEE', chunks[0], data.toJSON()) | ||
if (!data || data.length === 0) { | ||
error(`Error: No data received for ${filename}`) | ||
return | ||
} | ||
|
||
const mediaFilePath = path.join(mediaDir, filename) | ||
try { | ||
fs.writeFileSync(mediaFilePath, data) | ||
debug(`Media file successfully saved: ${mediaFilePath}`) | ||
} catch (e) { | ||
error(`Failed to save media file ${filename}`, e) | ||
} | ||
}) | ||
} else { | ||
error(`Error: Invalid status code ${response.statusCode}`) | ||
} | ||
}) | ||
|
||
request.on('error', (err) => { | ||
error('Error:', err.message) | ||
}) | ||
|
||
request.end() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters