diff --git a/add-on/src/lib/copier.js b/add-on/src/lib/copier.js index 8ab9956a7..cfe6d224c 100644 --- a/add-on/src/lib/copier.js +++ b/add-on/src/lib/copier.js @@ -33,6 +33,10 @@ async function copyTextToClipboard (text, notify) { function createCopier (notify, ipfsPathValidator) { return { + async copyTextToClipboard (text) { + await copyTextToClipboard(text, notify) + }, + async copyCanonicalAddress (context, contextType) { const url = await findValueForContext(context, contextType) const ipfsPath = ipfsPathValidator.resolveToIpfsPath(url) diff --git a/add-on/src/lib/ipfs-companion.js b/add-on/src/lib/ipfs-companion.js index 4c49cccce..32a06f6a7 100644 --- a/add-on/src/lib/ipfs-companion.js +++ b/add-on/src/lib/ipfs-companion.js @@ -69,8 +69,8 @@ module.exports = async function init () { dnslinkResolver = createDnslinkResolver(getState) ipfsPathValidator = createIpfsPathValidator(getState, getIpfs, dnslinkResolver) - ipfsImportHandler = createIpfsImportHandler(getState, getIpfs, ipfsPathValidator, runtime) copier = createCopier(notify, ipfsPathValidator) + ipfsImportHandler = createIpfsImportHandler(getState, getIpfs, ipfsPathValidator, runtime, copier) inspector = createInspector(notify, ipfsPathValidator, getState) contextMenus = createContextMenus(getState, runtime, ipfsPathValidator, { onAddFromContext, @@ -319,6 +319,7 @@ module.exports = async function init () { } return } + ipfsImportHandler.copyShareLink(result) ipfsImportHandler.preloadFilesAtPublicGateway(result) if (state.ipfsNodeType === 'embedded' || !state.openViaWebUI) { return ipfsImportHandler.openFilesAtGateway({ result, openRootInNewTab: true }) diff --git a/add-on/src/lib/ipfs-import.js b/add-on/src/lib/ipfs-import.js index 727d6b098..984b45880 100644 --- a/add-on/src/lib/ipfs-import.js +++ b/add-on/src/lib/ipfs-import.js @@ -5,7 +5,7 @@ const browser = require('webextension-polyfill') const { redirectOptOutHint } = require('./ipfs-request') -function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime) { +function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime, copier) { const ipfsImportHandler = { formatImportDirectory (path) { path = path.replace(/\/$|$/, '/') @@ -87,7 +87,24 @@ function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime) http.send() }) }, - preloadFilesAtPublicGateway (files) { + async copyShareLink (files) { + if (!files || !copier) return + const root = files.find(file => file.path === '') + if (!root) return + let path + if (files.length === 2) { + // share path to a single file in a dir + const file = files.find(file => file.path !== '') + path = `/ipfs/${root.hash}/${file.path}` + } else { + // share wrapping dir + path = `/ipfs/${root.hash}/` + } + const state = getState() + const url = ipfsPathValidator.resolveToPublicUrl(path, state.pubGwURLString) + await copier.copyTextToClipboard(url) + }, + async preloadFilesAtPublicGateway (files) { files.forEach(file => { if (file && file.hash) { const { path } = this.getIpfsPathAndNativeAddress(file.hash) diff --git a/add-on/src/popup/quick-import.js b/add-on/src/popup/quick-import.js index aac6df6ae..b56134db0 100644 --- a/add-on/src/popup/quick-import.js +++ b/add-on/src/popup/quick-import.js @@ -91,6 +91,7 @@ async function processFiles (state, emitter, files) { state.progress = 'Completed' emitter.emit('render') console.log(`Successfully imported ${streams.length} files`) + ipfsImportHandler.copyShareLink(result) ipfsImportHandler.preloadFilesAtPublicGateway(result) // open web UI at proper directory // unless and embedded node is in use (no access to web UI)