Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 149 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@
"jimp": "0.6.8",
"js-sha3": "0.8.0",
"js-yaml": "3.13.1",
"jsmediatags": "3.8.1",
"linkify-react": "4.1.0",
"linkifyjs": "4.1.0",
"localforage": "1.10.0",
"lodash": "4.17.21",
"lottie-react": "2.4.0",
"markdown-to-jsx": "7.4.3",
"music-metadata": "13.0.2",
"numeral": "2.0.6",
"persona": "5.5.0",
"prop-types": "15.7.2",
Expand Down Expand Up @@ -191,7 +191,6 @@
"@types/eth-sig-util": "2.1.1",
"@types/history": "^4.7.11",
"@types/js-yaml": "3.12.4",
"@types/jsmediatags": "3.9.1",
"@types/linkifyjs": "2.1.3",
"@types/lodash": "4.14.149",
"@types/node": "12.12.35",
Expand Down
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { readFileSync } from 'fs'

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { resizeImage } from 'utils/imageProcessingUtil'

import { processFiles } from './processFiles'

vi.mock('@audius/common/schemas', () => ({
newTrackMetadata: vi.fn((metadata) => metadata)
}))
vi.mock(
'@audius/common/utils',
() => import('@audius/common/src/utils/uploadConstants')
)
vi.mock('utils/imageProcessingUtil', () => ({ resizeImage: vi.fn() }))

const fixtureDirectory = 'src/pages/upload-page/store/utils/fixtures'
const fixture = (name: string, type: string) =>
new File([readFileSync(`${fixtureDirectory}/${name}`)], name, {
type
})

const upload = async (file: File) => {
const handleInvalid = vi.fn()
const [track] = await Promise.all(processFiles([file], handleInvalid))
expect(handleInvalid).not.toHaveBeenCalled()
return track!
}

describe('processFiles artwork', () => {
beforeEach(() => {
vi.mocked(resizeImage).mockReset()
vi.mocked(resizeImage).mockResolvedValue(
new File(['resized artwork'], 'resized.png', { type: 'image/png' })
)
vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:artwork')
vi.spyOn(console, 'warn').mockImplementation(() => {})
})

afterEach(() => {
vi.restoreAllMocks()
})

it.each([
['cover.opus', 'audio/ogg'],
['cover.mp3', 'audio/mpeg']
])(
'extracts embedded artwork from %s through the resize pipeline',
async (name, type) => {
const file = fixture(name, type)
const track = await upload(file)

expect(resizeImage).toHaveBeenCalledOnce()
const image = vi.mocked(resizeImage).mock.calls[0][0]
expect(image.type).toBe('image/png')
expect(new Uint8Array(await image.arrayBuffer())).toEqual(
new Uint8Array(readFileSync(`${fixtureDirectory}/cover.png`))
)
expect(track.file).toBe(file)
expect(track.metadata.title).toBe(name.replace(/\.[^.]+$/, ''))
expect(track.metadata.orig_filename).toBe(name)
expect(track.metadata.artwork.url).toBe('blob:artwork')
expect(await track.metadata.artwork.file!.text()).toBe('resized artwork')
expect(console.warn).not.toHaveBeenCalled()
}
)

it('continues without artwork when the audio has no cover', async () => {
const track = await upload(fixture('no-cover.wav', 'audio/wav'))

expect(track.metadata.artwork).toEqual({ file: null, url: '' })
expect(resizeImage).not.toHaveBeenCalled()
expect(console.warn).not.toHaveBeenCalled()
})

it('continues uploading when metadata parsing fails', async () => {
const file = new File(['not audio'], 'broken.tsv', {
type: 'audio/unknown'
})
const track = await upload(file)

expect(track.file).toBe(file)
expect(track.metadata.artwork).toEqual({ file: null, url: '' })
expect(resizeImage).not.toHaveBeenCalled()
expect(console.warn).toHaveBeenCalledOnce()
})

it('continues uploading when resizing the embedded cover fails', async () => {
vi.mocked(resizeImage).mockRejectedValue(
new Error('Unable to resize image')
)
const track = await upload(fixture('cover.opus', 'audio/ogg'))

expect(track.metadata.artwork).toEqual({ file: null, url: '' })
expect(console.warn).toHaveBeenCalledOnce()
})
})
Loading