Skip to content

Commit

Permalink
[PAY-2468] Dedup filenames during download (#7563)
Browse files Browse the repository at this point in the history
  • Loading branch information
dharit-tan authored Feb 13, 2024
1 parent ba2263b commit ce72ac5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/common/src/utils/fileUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Buffer } from 'buffer'

import { Nullable } from './typeUtils'
import { DownloadFile } from '~/services'

/** Convert a base64 string to a file object */
export const dataURLtoFile = async (
Expand Down Expand Up @@ -35,3 +36,15 @@ export const getDownloadFilename = ({
return `${split.join('.')}.mp3`
}
}

export const dedupFilenames = (files: DownloadFile[]) => {
const filenameCounts = new Map<string, number>()
for (const file of files) {
const count = filenameCounts.get(file.filename) ?? 0
filenameCounts.set(file.filename, count + 1)
const split = file.filename.split('.')
const extension = split.pop()
file.filename =
count === 0 ? file.filename : split.join('.') + `-${count}.${extension}`
}
}
2 changes: 2 additions & 0 deletions packages/mobile/src/services/track-download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { setFetchCancel, setFileInfo } from 'app/store/download/slice'
import { setVisibility } from 'app/store/drawers/slice'

import { audiusBackendInstance } from './audius-backend-instance'
import { dedupFilenames } from '~/utils'

const { downloadFinished } = tracksSocialActions

Expand Down Expand Up @@ -93,6 +94,7 @@ const downloadMany = async ({
getFetchConfig: (filePath: string) => RNFetchBlobConfig
onFetchComplete?: (path: string) => Promise<void>
}) => {
dedupFilenames(files)
try {
const responsePromises = files.map(({ url, filename }) =>
RNFetchBlob.config(getFetchConfig(directory + '/' + filename)).fetch(
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/services/track-download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { tracksSocialActions } from '@audius/common/store'
import { downloadZip } from 'client-zip'

import { audiusBackendInstance } from './audius-backend/audius-backend-instance'
import { dedupFilenames } from '@audius/common/utils'

const { downloadFinished } = tracksSocialActions

Expand Down Expand Up @@ -41,6 +42,7 @@ class TrackDownload extends TrackDownloadBase {
rootDirectoryName,
abortSignal
}: DownloadTrackArgs) {
dedupFilenames(files)
const responsePromises = files.map(
async ({ url }) => await window.fetch(url, { signal: abortSignal })
)
Expand Down

0 comments on commit ce72ac5

Please sign in to comment.