Skip to content

Commit

Permalink
Fix android downloads (#7633)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondjacobson authored Feb 16, 2024
1 parent 19ca463 commit dff9ae4
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions packages/mobile/src/services/track-download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { DownloadTrackArgs } from '@audius/common/services'
import { TrackDownload as TrackDownloadBase } from '@audius/common/services'
import { tracksSocialActions, downloadsActions } from '@audius/common/store'
import { Platform, Share } from 'react-native'
import { zip } from 'react-native-zip-archive'
import type {
FetchBlobResponse,
ReactNativeBlobUtilConfig,
StatefulPromise
} from 'react-native-blob-util'
import ReactNativeBlobUtil from 'react-native-blob-util'
import { zip } from 'react-native-zip-archive'
import { dedupFilenames } from '~/utils'

import { dispatch } from 'app/store'
Expand Down Expand Up @@ -57,10 +57,9 @@ const downloadOne = async ({
const filePath = directory + '/' + filename

try {
const fetchTask = ReactNativeBlobUtil.config(getFetchConfig(filePath)).fetch(
'GET',
fileUrl
)
const fetchTask = ReactNativeBlobUtil.config(
getFetchConfig(filePath)
).fetch('GET', fileUrl)
fetchTasks = [fetchTask]

// TODO: The ReactNativeBlobUtil library is currently broken for download progress events on both platforms.
Expand Down Expand Up @@ -103,10 +102,9 @@ const downloadMany = async ({
dedupFilenames(files)
try {
const responsePromises = files.map(({ url, filename }) =>
ReactNativeBlobUtil.config(getFetchConfig(directory + '/' + filename)).fetch(
'GET',
url
)
ReactNativeBlobUtil.config(
getFetchConfig(directory + '/' + filename)
).fetch('GET', url)
)
fetchTasks = responsePromises
const responses = await Promise.all(responsePromises)
Expand Down Expand Up @@ -216,6 +214,7 @@ const download = async ({
mediaScannable: true,
notification: true,
path: filePath,
storeInDownloads: true,
title: filename,
useDownloadManager: true
}
Expand All @@ -236,17 +235,41 @@ const download = async ({
* the initial downloads to avoid showing notifications, then manually add a
* notification for the zip file.
*/
directory: ReactNativeBlobUtil.fs.dirs.DownloadDir + '/' + rootDirectoryName,
directory:
ReactNativeBlobUtil.fs.dirs.DownloadDir + '/' + rootDirectoryName,
getFetchConfig: (filePath) => ({
fileCache: true,
path: filePath
}),
onFetchComplete: async (path: string) => {
let mediaStoragePath
// On android 13+, we need to manually copy to media storage
try {
mediaStoragePath =
await ReactNativeBlobUtil.MediaCollection.copyToMediaStore(
{
// The name of the file that should show up in Downloads as a .zip
name: rootDirectoryName,
// Can be left empty as we're putting the file into downloads
parentFolder: '',
mimeType: 'application/zip'
},
'Download',
path
)
} catch (e) {
console.error(e)
// Continue on because on android <13+ the media storage copy will
// not work, but we can deliver the file to the old download system
// by calling android.addCompleteDownload.
}
// We still need to add the complete download notification here anyway
// even if on android 13+
ReactNativeBlobUtil.android.addCompleteDownload({
title: rootDirectoryName,
description: rootDirectoryName,
mime: 'application/zip',
path,
path: mediaStoragePath ?? path,
showNotification: true
})
dispatch(downloadFinished())
Expand Down

0 comments on commit dff9ae4

Please sign in to comment.