Skip to content

Commit

Permalink
feat(browserAction): [WIP] Quick Upload of local files
Browse files Browse the repository at this point in the history
Initial PoC that does not work yet due to Firefox bug

See #184 and https://bugzilla.mozilla.org/show_bug.cgi?id=1292701
  • Loading branch information
lidel committed Feb 12, 2017
1 parent e4c8ade commit 8ac6a9e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
25 changes: 16 additions & 9 deletions add-on/src/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,25 @@ browser.contextMenus.create({
title: browser.i18n.getMessage(contextMenuUploadToIpfs),
contexts: ['image', 'video', 'audio'],
onclick: (info, tab) => {
ipfs.util.addFromURL(info.srcUrl, (err, result) => {
if (err) {
notify('Unable to upload to IPFS API', `${err}`)
return
}
browser.tabs.create({
'url': new URL(state.gwURLString + '/ipfs/' + result[0].hash).toString()
})
})
ipfs.util.addFromURL(info.srcUrl, uploadResultHandler)
}
})

function uploadResultHandler (err, result) {
if (err || !result) {
notify('Unable to upload to IPFS API', `${err}`)
return console.error('ipfs add error', err, result)
}
result.forEach(function (file) {
if (file && file.hash) {
browser.tabs.create({
'url': new URL(state.gwURLString + '/ipfs/' + file.hash).toString()
})
console.log('successfully stored', file.hash)
}
})
}

function updateContextMenus () {
browser.contextMenus.update(contextMenuUploadToIpfs, {enabled: state.peerCount > 0})
}
Expand Down
5 changes: 3 additions & 2 deletions add-on/src/popup/browser-action.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
</div>

<ul id="operations">
<li class="action" id="open-webui" data-i18n="panel_open-webui"></span></li>
<li class="action" id="open-preferences" data-i18n="panel_open-preferences"></span></li>
<li class="action" id="quick-upload"><label for="quickUploadInput" data-i18n="panel_quick-upload"></label><input type="file" id="quickUploadInput" style="display: none" /></li>
<li class="action" id="open-webui" data-i18n="panel_open-webui"></li>
<li class="action" id="open-preferences" data-i18n="panel_open-preferences"></li>
<li class="action" id="toggle-gateway-redirect">
<span id="enable-gateway-redirect" data-i18n="panel_toggle-gateway-redirect-enable"></span>
<span id="disable-gateway-redirect" data-i18n="panel_toggle-gateway-redirect-disable"></span>
Expand Down
25 changes: 25 additions & 0 deletions add-on/src/popup/browser-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const enableRedirect = document.getElementById('enable-gateway-redirect')
const disableRedirect = document.getElementById('disable-gateway-redirect')
const openWebUI = document.getElementById('open-webui')
const openPreferences = document.getElementById('open-preferences')
const quickUpload = document.getElementById('quick-upload')
const quickUploadInput = document.getElementById('quickUploadInput')

const ipfsIcon = document.getElementById('icon')
const ipfsIconOn = '../../icons/ipfs-logo-on.svg'
Expand All @@ -23,6 +25,26 @@ function set (id, value) {
document.getElementById(id).innerHTML = value
}

function onQuickUploadInputChange () {
browser.runtime.getBackgroundPage()
.then(bg => {
bg.ipfs.add(new Buffer(quickUploadInput.value), bg.uploadResultHandler)
})
.catch(error => { console.error(`Unable to perform quick upload due to ${error}`) })
}

function updateQuickUpload (enabled) {
if (enabled) {
quickUploadInput.onchange = onQuickUploadInputChange
quickUpload.style.opacity = 1
quickUploadInput.disabled = undefined
} else {
quickUploadInput.onchange = undefined
quickUpload.style.opacity = 0.5
quickUploadInput.disabled = 'disabled'
}
}

enableRedirect.onclick = () => browser.storage.local.set({useCustomGateway: true})
.then(updatePopup)
.catch(error => { console.error(`Unable to update redirect state due to ${error}`) })
Expand Down Expand Up @@ -83,8 +105,11 @@ function updatePopup () {
// update swarm peer count
background.getSwarmPeerCount()
.then(peerCount => {
// update peer counter
set('swarm-peers-val', peerCount < 0 ? offline : peerCount)
ipfsIcon.src = peerCount > 0 ? ipfsIconOn : ipfsIconOff
// enable/disable quick upload
updateQuickUpload(peerCount > 0)
})
.catch(error => {
console.error(`Unable update peer count due to ${error}`)
Expand Down

0 comments on commit 8ac6a9e

Please sign in to comment.