tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[bug] audio tag using a asset resource file as the source, the asset server returns wrong content-type of text/html

Open nathanfaucett opened this issue 2 years ago • 5 comments

Describe the bug

audio tag using a asset resource file as the source, the asset server returns wrong content-type of text/html

Reproduction

create project with audio files in bundle.resources create a filepath using something like

const filePath = convertFileSrc(
	await normalize(
		await resolveResource('name_of_audio_file.mp3')
	)
)

use in audio tag <audio controls><source src="{filePath}" type="audio/mpeg" /></audio>

Screen Shot 2022-09-15 at 16 53 31

Expected behavior

the content type to be audio/mpeg

Platform and versions

Environment › OS: Mac OS 12.2.1 X64 › Node.js: 16.15.0 › npm: 8.8.0 › pnpm: 7.0.0 › yarn: Not installed! › rustup: 1.25.1 › rustc: 1.63.0 › cargo: 1.63.0 › Rust toolchain: stable-aarch64-apple-darwin

Packages › @tauri-apps/cli [NPM]: 1.1.0 › @tauri-apps/api [NPM]: 1.1.0 › tauri [RUST]: 1.1.0, › tauri-build [RUST]: 1.1.0, › tao [RUST]: 0.14.0, › wry [RUST]: 0.21.0,

App › build-type: bundle › CSP: default-src 'self'; img-src 'self' asset: https://asset.localhost; media-src 'self' asset: https://asset.localhost; › distDir: ../dist › devPath: http://localhost:3000/ › framework: Svelte

App directory structure ├─ node_modules ├─ public ├─ src-tauri ├─ .git ├─ .vscode └─ src

Stack trace

No response

Additional context

No response

nathanfaucett avatar Sep 15 '22 20:09 nathanfaucett

Maybe the good news is that it doesn't affect playback :) (yes! also can work in production mode)

图片

The code is not complicated in Vue3. 图片

Moon1102 avatar Sep 16 '22 01:09 Moon1102

This only affects requests with range header. Initial request seems fine, only subsequent requests are affected.

This is probably because in the initial request we are reading the first bytes of the file where it usually contains the magic number to identify the file while subsequent requests might not have the magic number inside it and so infer crate will fail to parse the correct mimetype.

@lucasfernog should we use https://docs.rs/infer/0.9.0/infer/fn.get_from_path.html instead?

amrbashir avatar Sep 16 '22 12:09 amrbashir

We shouldn't infer the mime type from the path, that's a security vulnerability. We could instead cache the mime type on range requests or maybe always read the first bytes.

lucasfernog avatar Sep 16 '22 12:09 lucasfernog

get_from_path does read the first bytes, it doesn't rely on file extension. https://docs.rs/infer/0.9.0/src/infer/lib.rs.html#240-251

amrbashir avatar Sep 16 '22 12:09 amrbashir

although I think caching might be better for performance?

amrbashir avatar Sep 16 '22 12:09 amrbashir