Skip to content

Commit

Permalink
feat: copy shareable link during file import (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel authored Dec 12, 2019
1 parent ebcc3fa commit 3c8c57d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions add-on/src/lib/copier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 })
Expand Down
21 changes: 19 additions & 2 deletions add-on/src/lib/ipfs-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/\/$|$/, '/')
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions add-on/src/popup/quick-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3c8c57d

Please sign in to comment.