Skip to content

Commit

Permalink
fix: use direct link to gateway to download files
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
  • Loading branch information
hacdias committed Nov 10, 2022
1 parent 134cab8 commit 480b955
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 95 deletions.
3 changes: 1 addition & 2 deletions src/bundles/files/actions.js
Expand Up @@ -396,9 +396,8 @@ const actions = () => ({
* @param {FileStat[]} files
*/
doFilesDownloadLink: (files) => perform(ACTIONS.DOWNLOAD_LINK, async (ipfs, { store }) => {
const apiUrl = store.selectApiUrl()
const gatewayUrl = store.selectGatewayUrl()
return await getDownloadLink(files, gatewayUrl, apiUrl, ipfs)
return await getDownloadLink(files, gatewayUrl, ipfs)
}),

/**
Expand Down
28 changes: 5 additions & 23 deletions src/files/FilesPage.js
Expand Up @@ -6,7 +6,6 @@ import { withTranslation, Trans } from 'react-i18next'
import ReactJoyride from 'react-joyride'
// Lib
import { filesTour } from '../lib/tours'
import downloadFile from './download-file'
// Components
import ContextMenu from './context-menu/ContextMenu'
import withTour from '../components/tour/withTour'
Expand All @@ -27,8 +26,6 @@ const FilesPage = ({
files, filesPathInfo, pinningServices, toursEnabled, handleJoyrideCallback, isCliTutorModeEnabled, cliOptions, t
}) => {
const contextMenuRef = useRef()
const [downloadAbort, setDownloadAbort] = useState(null)
const [downloadProgress, setDownloadProgress] = useState(null)
const [modals, setModals] = useState({ show: null, files: null })
const [contextMenu, setContextMenu] = useState({
isOpen: false,
Expand Down Expand Up @@ -58,28 +55,14 @@ const FilesPage = ({
*/

const onDownload = async (files) => {
if (downloadProgress !== null) {
return downloadAbort()
}

const { url, filename, method } = await doFilesDownloadLink(files)

if (method === 'GET') {
const link = document.createElement('a')
link.href = url
link.click()
} else {
const updater = (v) => setDownloadProgress(v)
const { abort } = await downloadFile(url, filename, updater, method)
setDownloadAbort(() => abort)
}
const { url, filename } = await doFilesDownloadLink(files)
const link = document.createElement('a')
link.download = filename // TODO: filename isn't working. Keep getting 'get.tar.gz' - is it being overwritten?
link.href = url
link.click()
}

const onDownloadCar = async (files) => {
if (downloadProgress !== null) {
return downloadAbort()
}

const url = await doFilesDownloadCarLink(files)
const link = document.createElement('a')
link.href = url
Expand Down Expand Up @@ -167,7 +150,6 @@ const FilesPage = ({
pendingPins={pendingPins}
failedPins={failedPins}
upperDir={files.upper}
downloadProgress={downloadProgress}
onShare={(files) => showModal(SHARE, files)}
onRename={(files) => showModal(RENAME, files)}
onRemove={(files) => showModal(DELETE, files)}
Expand Down
44 changes: 0 additions & 44 deletions src/files/download-file.js

This file was deleted.

39 changes: 13 additions & 26 deletions src/lib/files.js
Expand Up @@ -38,28 +38,23 @@ export function normalizeFiles (files) {
* @typedef {Object} FileDownload
* @property {string} url
* @property {string} filename
* @property {string} method
*
* @param {FileStat} file
* @param {string} gatewayUrl
* @param {string} apiUrl
* @returns {Promise<FileDownload>}
*/
async function downloadSingle (file, gatewayUrl, apiUrl) {
let url, filename, method
async function downloadSingle (file, gatewayUrl) {
let url, filename

if (file.type === 'directory') {
const name = file.name || `download_${file.cid}` // Name is not always available.
url = `${apiUrl}/api/v0/get?arg=${file.cid}&archive=true&compress=true`
filename = `${name}.tar.gz`
method = 'POST' // API is POST-only
filename = `${file.name || `download_${file.cid}`}.tar.gz`
url = `${gatewayUrl}/api/v0/get?arg=${file.cid}&archive=true&compress=true`
} else {
url = `${gatewayUrl}/ipfs/${file.cid}?download=true&filename=${file.name}`
filename = file.name
method = 'GET'
url = `${gatewayUrl}/ipfs/${file.cid}?download=true&filename=${file.name}`
}

return { url, filename, method }
return { url, filename }
}

/**
Expand Down Expand Up @@ -90,39 +85,31 @@ export async function makeCIDFromFiles (files, ipfs) {
/**
*
* @param {FileStat[]} files
* @param {string} apiUrl
* @param {string} gatewayUrl
* @param {IPFSService} ipfs
* @returns {Promise<FileDownload>}
*/
async function downloadMultiple (files, apiUrl, ipfs) {
if (!apiUrl) {
const e = new Error('api url undefined')
return Promise.reject(e)
}

async function downloadMultiple (files, gatewayUrl, ipfs) {
const cid = await makeCIDFromFiles(files, ipfs)

return {
url: `${apiUrl}/api/v0/get?arg=${cid}&archive=true&compress=true`,
filename: `download_${cid}.tar.gz`,
method: 'POST' // API is POST-only
url: `${gatewayUrl}/api/v0/get?arg=${cid}&archive=true&compress=true`,
filename: `download_${cid}.tar.gz`
}
}

/**
*
* @param {FileStat[]} files
* @param {string} gatewayUrl
* @param {string} apiUrl
* @param {IPFSService} ipfs
* @returns {Promise<FileDownload>}
*/
export async function getDownloadLink (files, gatewayUrl, apiUrl, ipfs) {
export async function getDownloadLink (files, gatewayUrl, ipfs) {
if (files.length === 1) {
return downloadSingle(files[0], gatewayUrl, apiUrl)
return downloadSingle(files[0], gatewayUrl)
}

return downloadMultiple(files, apiUrl, ipfs)
return downloadMultiple(files, gatewayUrl, ipfs)
}

/**
Expand Down

0 comments on commit 480b955

Please sign in to comment.