Skip to content

Commit 3c8c57d

Browse files
authored
feat: copy shareable link during file import (#834)
1 parent ebcc3fa commit 3c8c57d

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

add-on/src/lib/copier.js

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ async function copyTextToClipboard (text, notify) {
3333

3434
function createCopier (notify, ipfsPathValidator) {
3535
return {
36+
async copyTextToClipboard (text) {
37+
await copyTextToClipboard(text, notify)
38+
},
39+
3640
async copyCanonicalAddress (context, contextType) {
3741
const url = await findValueForContext(context, contextType)
3842
const ipfsPath = ipfsPathValidator.resolveToIpfsPath(url)

add-on/src/lib/ipfs-companion.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ module.exports = async function init () {
6969

7070
dnslinkResolver = createDnslinkResolver(getState)
7171
ipfsPathValidator = createIpfsPathValidator(getState, getIpfs, dnslinkResolver)
72-
ipfsImportHandler = createIpfsImportHandler(getState, getIpfs, ipfsPathValidator, runtime)
7372
copier = createCopier(notify, ipfsPathValidator)
73+
ipfsImportHandler = createIpfsImportHandler(getState, getIpfs, ipfsPathValidator, runtime, copier)
7474
inspector = createInspector(notify, ipfsPathValidator, getState)
7575
contextMenus = createContextMenus(getState, runtime, ipfsPathValidator, {
7676
onAddFromContext,
@@ -319,6 +319,7 @@ module.exports = async function init () {
319319
}
320320
return
321321
}
322+
ipfsImportHandler.copyShareLink(result)
322323
ipfsImportHandler.preloadFilesAtPublicGateway(result)
323324
if (state.ipfsNodeType === 'embedded' || !state.openViaWebUI) {
324325
return ipfsImportHandler.openFilesAtGateway({ result, openRootInNewTab: true })

add-on/src/lib/ipfs-import.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const browser = require('webextension-polyfill')
55

66
const { redirectOptOutHint } = require('./ipfs-request')
77

8-
function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime) {
8+
function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime, copier) {
99
const ipfsImportHandler = {
1010
formatImportDirectory (path) {
1111
path = path.replace(/\/$|$/, '/')
@@ -87,7 +87,24 @@ function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime)
8787
http.send()
8888
})
8989
},
90-
preloadFilesAtPublicGateway (files) {
90+
async copyShareLink (files) {
91+
if (!files || !copier) return
92+
const root = files.find(file => file.path === '')
93+
if (!root) return
94+
let path
95+
if (files.length === 2) {
96+
// share path to a single file in a dir
97+
const file = files.find(file => file.path !== '')
98+
path = `/ipfs/${root.hash}/${file.path}`
99+
} else {
100+
// share wrapping dir
101+
path = `/ipfs/${root.hash}/`
102+
}
103+
const state = getState()
104+
const url = ipfsPathValidator.resolveToPublicUrl(path, state.pubGwURLString)
105+
await copier.copyTextToClipboard(url)
106+
},
107+
async preloadFilesAtPublicGateway (files) {
91108
files.forEach(file => {
92109
if (file && file.hash) {
93110
const { path } = this.getIpfsPathAndNativeAddress(file.hash)

add-on/src/popup/quick-import.js

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ async function processFiles (state, emitter, files) {
9191
state.progress = 'Completed'
9292
emitter.emit('render')
9393
console.log(`Successfully imported ${streams.length} files`)
94+
ipfsImportHandler.copyShareLink(result)
9495
ipfsImportHandler.preloadFilesAtPublicGateway(result)
9596
// open web UI at proper directory
9697
// unless and embedded node is in use (no access to web UI)

0 commit comments

Comments
 (0)