Skip to content

Commit

Permalink
fixed saving media in publications
Browse files Browse the repository at this point in the history
  • Loading branch information
iskaktoltay committed Aug 2, 2023
1 parent b0e1ff4 commit 83258dd
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 28 deletions.
9 changes: 9 additions & 0 deletions frontend/apps/electron/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Sentry from '@sentry/electron/main'
import os from 'os'
import {mainMenu, trpc} from './api'
import {mainDaemon} from './daemon'
import {saveCidAsFile} from './save-cid-as-file'

mainDaemon

Expand Down Expand Up @@ -59,6 +60,14 @@ ipcMain.handle('dark-mode:system', () => {
nativeTheme.themeSource = 'system'
})

ipcMain.handle('get-path', () => {
const path = app.getPath('appData')
console.log(path)
return path
})

ipcMain.on('save-file', saveCidAsFile)

app.on('ready', () => {
trpc.createAppWindow()
})
Expand Down
3 changes: 2 additions & 1 deletion frontend/apps/electron/src/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import superjson from 'superjson'
import {AppIPC} from '@mintter/app/src/app-ipc'
import {decodeRouteFromPath} from '@mintter/app/src/utils/route-encoding'
import {client} from './trpc'
import { app, ipcRenderer } from 'electron'

const trpcReact = createTRPCReact<AppRouter>()

Expand Down Expand Up @@ -97,7 +98,7 @@ function MainApp({
toast.error('Not implemented open: ' + url)
}}
saveCidAsFile={async (cid: string, name: string) => {
toast.error('Not implemented saveCidAsFile: ' + cid + name)
ipc.send?.('save-file', {cid, name})
}}
windowUtils={windowUtils}
>
Expand Down
45 changes: 45 additions & 0 deletions frontend/apps/electron/src/save-cid-as-file.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {BACKEND_HTTP_URL} from '@mintter/app/src/constants'
import {toast} from '@mintter/app/src/toast'
import { app, dialog, net } from 'electron'
import fs from 'fs'

export async function saveCidAsFile(event, args) {
const {cid, name} = args
const request = net.request(`${BACKEND_HTTP_URL}/ipfs/${cid}`);
console.log(app.getPath('downloads'))
request.on('response', (response) => {
if (response.statusCode === 200) {
const chunks: Buffer[] = [];

response.on('data', (chunk: Buffer) => {
chunks.push(chunk);
});

response.on('end', async () => {
const data = Buffer.concat(chunks);
const options = {
defaultPath: app.getPath('downloads') + '/' + name,
};
console.log(options.defaultPath)
const { filePath, canceled } = await dialog.showSaveDialog(options);
if (!canceled && filePath) {
try {
fs.writeFileSync(filePath, data, { encoding: 'binary' });
toast.success(`Successfully downloaded file ${name}`);
} catch (e) {
toast.error(`Failed to download file ${name}`);
console.log(e);
}
}
});
} else {
console.error('Error: Invalid status code', response.statusCode);
}
});

request.on('error', (error) => {
console.error('Error:', error.message);
});

request.end();
}
30 changes: 3 additions & 27 deletions frontend/packages/app/src/editor/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
ReactSlashMenuItem,
} from '@mintter/app/src/blocknote-react'
import {HDBlockSchema} from '@mintter/app/src/client/schema'
import {toast} from 'react-hot-toast'
import {
Button,
Form,
Expand All @@ -24,13 +23,10 @@ import {
XStack,
YStack,
} from '@mintter/ui'
import {save} from '@tauri-apps/api/dialog'
import {BaseDirectory, writeBinaryFile} from '@tauri-apps/api/fs'
import {getClient, ResponseType} from '@tauri-apps/api/http'
import {appDataDir} from '@tauri-apps/api/path'
import {ChangeEvent, useEffect, useState} from 'react'
import {RiImage2Fill} from 'react-icons/ri'
import {BACKEND_FILE_UPLOAD_URL, BACKEND_FILE_URL} from '../constants'
import { useAppContext } from '@mintter/app/src/app-context'

export const ImageBlock = createReactBlockSpec({
type: 'image',
Expand Down Expand Up @@ -136,29 +132,9 @@ function ImageComponent({
}
}, [selection])

const {saveCidAsFile} = useAppContext()
const saveImage = async () => {
const client = await getClient()
const data = (
await client.get(`${BACKEND_FILE_URL}/${block.props.url}`, {
responseType: ResponseType.Binary,
})
).data as any

const filePath = await save({
defaultPath: (await appDataDir()) + '/' + block.props.name,
})

if (filePath) {
try {
await writeBinaryFile(filePath ? filePath : 'mintter-image', data, {
dir: BaseDirectory.AppData,
})
toast.success(`Successfully downloaded image ${block.props.name}`)
} catch (e) {
toast.error(`Failed to download image ${block.props.name}`)
console.log(e)
}
}
await saveCidAsFile(block.props.url, block.props.name)
}

return (
Expand Down

0 comments on commit 83258dd

Please sign in to comment.