Skip to content

Commit

Permalink
wip: fixing urlSource import via context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed May 5, 2020
1 parent 01c1925 commit 3fadffa
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 322 deletions.
37 changes: 18 additions & 19 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ log.error = debug('ipfs-companion:main:error')
const browser = require('webextension-polyfill')
const { Buffer } = require('buffer')

const { urlSource } = require('ipfs-http-client')
const all = require('it-all')

const toMultiaddr = require('uri-to-multiaddr')
const pMemoize = require('p-memoize')
const { optionDefaults, storeMissingOptions, migrateOptions, guiURLString, safeURL } = require('./options')
Expand Down Expand Up @@ -293,29 +296,25 @@ module.exports = async function init () {
// --------------------
// Initially, this was a workaround due to https://github.com/ipfs/ipfs-companion/issues/227
// but now we have additional rules about keeping file name, so we can't use valilla ipfs.addFromURL
const fetchOptions = {
cache: 'force-cache',
referrer: context.pageUrl
}
// console.log('onAddFromContext.context', context)
// console.log('onAddFromContext.fetchOptions', fetchOptions)
const response = await fetch(dataSrc, fetchOptions)
const blob = await response.blob()
const buffer = await new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => resolve(Buffer.from(reader.result))
reader.onerror = reject
reader.readAsArrayBuffer(blob)
})
const url = new URL(response.url)
log('onAddFromContext.context', context) // TODO
log('onAddFromContext.dataSrc', dataSrc) // TODO
/* TODO: move below to import.js
const { pathname, hostname } = new URL(dataSrc)
// https://github.com/ipfs-shipyard/ipfs-companion/issues/599
const filename = url.pathname === '/'
? url.hostname
: url.pathname.replace(/[\\/]+$/, '').split('/').pop()
const filename = pathname === '/'
? hostname
: pathname.replace(/[\\/]+$/, '').split('/').pop()
const data = {
path: decodeURIComponent(filename),
content: buffer
content: (await concat(urlSource(dataSrc))).slice()
}
*/

// TODO: handle datauri(when right click on image)
// TODO: fix urlSource -- add test in js-ipfs repo
const data = await all(urlSource(dataSrc))
log('onAddFromContext.urlSource.data', data)

result = await ipfsImportHandler.importFiles(data, options, importDir)
}
} catch (error) {
Expand Down
3 changes: 3 additions & 0 deletions add-on/src/lib/ipfs-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ function createIpfsImportHandler (getState, getIpfs, ipfsPathValidator, runtime,
// and then copied to an MFS directory
// to ensure that CIDs for any created file
// remain the same for ipfs-companion and Web UI
// TODO: remove logs
log('importFiles', data)
log('importFiles', JSON.stringify(data))

const data2 = []
log('typeof data', (typeof data))

// Convert FileList items to objects that preserve filenames
if (typeof data.item === 'function') {
for (const file of data) {
data2.push({
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"pull-to-stream": "0.1.1"
},
"devDependencies": {
"@babel/core": "7.9.0",
"@babel/core": "7.9.6",
"@babel/preset-env": "7.9.5",
"babel-loader": "8.1.0",
"babel-plugin-syntax-async-generators": "6.13.0",
Expand All @@ -109,11 +109,10 @@
"sinon": "7.5.0",
"sinon-chrome": "3.0.1",
"standard": "14.3.1",
"tar": "5.0.5",
"terser": "4.6.12",
"terser-webpack-plugin": "2.3.5",
"terser": "4.6.13",
"terser-webpack-plugin": "3.0.0",
"transform-loader": "0.2.4",
"web-ext": "4.1.0",
"web-ext": "4.2.0",
"webpack": "4.43.0",
"webpack-bundle-analyzer": "3.6.1",
"webpack-cli": "3.3.11",
Expand All @@ -132,14 +131,14 @@
"get-port": "5.1.1",
"http-dns": "3.0.1",
"http-node": "1.2.0",
"ipfs": "0.43.0",
"ipfs": "0.43.1",
"ipfs-css": "0.13.1",
"ipfs-http-client": "44.0.0",
"ipfs-http-client": "44.0.1",
"ipfs-postmsg-proxy": "3.1.1",
"ipfsx": "0.17.0",
"is-fqdn": "1.0.1",
"is-ipfs": "1.0.3",
"it-all": "1.0.1",
"it-all": "1.0.2",
"it-buffer": "0.1.2",
"it-concat": "1.0.0",
"it-tar": "1.2.2",
Expand Down
Loading

0 comments on commit 3fadffa

Please sign in to comment.