@@ -8,9 +8,15 @@ log.error = debug('ipfs-companion:import:error')
8
8
const browser = require ( 'webextension-polyfill' )
9
9
10
10
const { redirectOptOutHint } = require ( './ipfs-request' )
11
+ const { ipfsContentPath } = require ( './ipfs-path' )
12
+
13
+ module . exports . browserActionFilesCpImportCurrentTab = 'browserActionFilesCpImportCurrentTab'
11
14
12
15
module . exports . createIpfsImportHandler = ( getState , getIpfs , ipfsPathValidator , runtime , copier ) => {
13
- const { resolveToPublicUrl } = ipfsPathValidator
16
+ const {
17
+ resolveToPublicUrl,
18
+ resolveToCid
19
+ } = ipfsPathValidator
14
20
const ipfsImportHandler = {
15
21
getIpfsPathAndNativeAddress ( cid ) {
16
22
const state = getState ( )
@@ -33,9 +39,8 @@ module.exports.createIpfsImportHandler = (getState, getIpfs, ipfsPathValidator,
33
39
34
40
async openFilesAtWebUI ( mfsPath ) {
35
41
const state = getState ( )
36
- await browser . tabs . create ( {
37
- url : `${ state . webuiRootUrl } #/files${ mfsPath } `
38
- } )
42
+ const url = `${ state . webuiRootUrl } #/files${ mfsPath } `
43
+ await browser . tabs . create ( { url } )
39
44
} ,
40
45
41
46
async copyImportResultsToFiles ( results , importDir ) {
@@ -82,7 +87,41 @@ module.exports.createIpfsImportHandler = (getState, getIpfs, ipfsPathValidator,
82
87
}
83
88
}
84
89
}
90
+ } ,
91
+
92
+ async filesCpImportCurrentTab ( browser ) {
93
+ try {
94
+ const {
95
+ copyShareLink,
96
+ preloadFilesAtPublicGateway,
97
+ openFilesAtWebUI
98
+ } = this
99
+ const currentTab = await browser . tabs . query ( { active : true , currentWindow : true } ) . then ( tabs => tabs [ 0 ] )
100
+ const importDir = module . exports . formatImportDirectory ( getState ( ) . importDir )
101
+ const ipfs = getIpfs ( )
102
+ const contentPath = ipfsContentPath ( currentTab . url , { keepURIParams : false } )
103
+ const pathSegments = contentPath . split ( '/' ) . filter ( Boolean )
104
+ const namedImport = typeof pathSegments [ 2 ] !== 'undefined'
105
+ const resolvedCid = await resolveToCid ( contentPath )
106
+
107
+ // best-effort name based on last path segment, or IPNS root
108
+ const name = namedImport
109
+ ? pathSegments . slice ( - 1 )
110
+ : `${ pathSegments [ 0 ] === 'ipns' ? pathSegments [ 1 ] : 'unnamed' } _${ resolvedCid . slice ( - 4 ) } `
111
+
112
+ // import to mfs
113
+ await ipfs . files . mkdir ( importDir , { parents : true } )
114
+ await ipfs . files . cp ( `/ipfs/${ resolvedCid } ` , `${ importDir } ${ name } ` )
115
+ await openFilesAtWebUI ( importDir )
116
+
117
+ // create fake ipfs.add results, so we can reuse code from quick-import feature
118
+ const { cid } = await ipfs . files . stat ( importDir , { hash : true } )
119
+ const results = [ { path : '' , cid } , { path : name , cid : resolvedCid } ]
120
+ await copyShareLink ( results )
121
+ await preloadFilesAtPublicGateway ( results )
122
+ } catch ( e ) { log . error ( 'unexpected error during filesCpImportCurrentTab' , e ) }
85
123
}
124
+
86
125
}
87
126
return ipfsImportHandler
88
127
}
0 commit comments