Skip to content

Commit 2112a49

Browse files
authored
feat: screenshot improvements (#1689)
1 parent 3e74cd0 commit 2112a49

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/take-screenshot.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ async function makeScreenshotDir (ipfs) {
1919
}
2020
}
2121

22-
async function onSucess (ipfs, launchWebUI, path, img) {
23-
const stats = await ipfs.files.stat(path)
24-
const url = `https://share.ipfs.io/#/${stats.hash}`
22+
async function onSuccess (ipfs, launchWebUI, path, img) {
23+
// preserve filename if single file is shared
24+
const filename = path.endsWith('.png') ? `?filename=${encodeURIComponent(path.split('/').pop())}` : ''
25+
const { cid } = await ipfs.files.stat(path)
26+
const url = `https://dweb.link/ipfs/${cid}${filename}`
2527
clipboard.writeText(url)
2628

2729
notify({
@@ -65,14 +67,15 @@ function handleScreenshot (ctx) {
6567
try {
6668
await makeScreenshotDir(ipfs)
6769
const isDir = output.length > 1
68-
const rawDate = new Date()
69-
const date = `${rawDate.getFullYear()}-${rawDate.getMonth()}-${rawDate.getDate()}`
70-
const time = `${rawDate.getHours()}.${rawDate.getMinutes()}.${rawDate.getMilliseconds()}`
71-
let baseName = `/screenshots/${date} ${time}`
70+
const d = new Date()
71+
const pad = n => String(n).padStart(2, '0')
72+
const date = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
73+
const time = `${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getMilliseconds())}`
74+
let baseName = `/screenshots/${date}_${time}`
7275

7376
if (isDir) {
7477
baseName += '/'
75-
await ipfs.files.mkdir(baseName)
78+
await ipfs.files.mkdir(baseName, { parents: true })
7679
} else {
7780
baseName += '.png'
7881
}
@@ -83,12 +86,13 @@ function handleScreenshot (ctx) {
8386
for (const { name, image } of output) {
8487
const img = nativeImage.createFromDataURL(image)
8588
const path = isDir ? `${baseName}${name}.png` : baseName
86-
await ipfs.files.write(path, img.toPNG(), { create: true })
89+
const { cid } = await ipfs.add(img.toPNG(), { pin: false }) // no low level pin, presence in MFS will be enough to keep it around
90+
await ipfs.files.cp(cid, path)
8791
lastImage = img
8892
}
8993

9094
logger.info(`[screenshot] completed: writing screenshots to ${baseName}`)
91-
onSucess(ipfs, launchWebUI, baseName, lastImage)
95+
onSuccess(ipfs, launchWebUI, baseName, lastImage)
9296
} catch (e) {
9397
onError(e)
9498
}

0 commit comments

Comments
 (0)