Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: copy shareable link during file import #834

Merged
merged 1 commit into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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