@@ -19,9 +19,11 @@ async function makeScreenshotDir (ipfs) {
19
19
}
20
20
}
21
21
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 } `
25
27
clipboard . writeText ( url )
26
28
27
29
notify ( {
@@ -65,14 +67,15 @@ function handleScreenshot (ctx) {
65
67
try {
66
68
await makeScreenshotDir ( ipfs )
67
69
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 } `
72
75
73
76
if ( isDir ) {
74
77
baseName += '/'
75
- await ipfs . files . mkdir ( baseName )
78
+ await ipfs . files . mkdir ( baseName , { parents : true } )
76
79
} else {
77
80
baseName += '.png'
78
81
}
@@ -83,12 +86,13 @@ function handleScreenshot (ctx) {
83
86
for ( const { name, image } of output ) {
84
87
const img = nativeImage . createFromDataURL ( image )
85
88
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 )
87
91
lastImage = img
88
92
}
89
93
90
94
logger . info ( `[screenshot] completed: writing screenshots to ${ baseName } ` )
91
- onSucess ( ipfs , launchWebUI , baseName , lastImage )
95
+ onSuccess ( ipfs , launchWebUI , baseName , lastImage )
92
96
} catch ( e ) {
93
97
onError ( e )
94
98
}
0 commit comments