-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat(audio): set audio content type #8416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| package audio | ||
|
|
||
| import ( | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/dhowden/tag" | ||
| "github.com/mudler/xlog" | ||
| ) | ||
|
|
||
| // extensionFromFileType returns the file extension for tag.FileType. | ||
| func extensionFromFileType(ft tag.FileType) string { | ||
| switch ft { | ||
| case tag.FLAC: | ||
| return "flac" | ||
| case tag.MP3: | ||
| return "mp3" | ||
| case tag.OGG: | ||
| return "ogg" | ||
| case tag.M4A: | ||
| return "m4a" | ||
| case tag.M4B: | ||
| return "m4b" | ||
| case tag.M4P: | ||
| return "m4p" | ||
| case tag.ALAC: | ||
| return "m4a" | ||
| case tag.DSF: | ||
| return "dsf" | ||
| default: | ||
| return "" | ||
| } | ||
| } | ||
|
|
||
| // contentTypeFromFileType returns the MIME type for tag.FileType. | ||
| func contentTypeFromFileType(ft tag.FileType) string { | ||
| switch ft { | ||
| case tag.FLAC: | ||
| return "audio/flac" | ||
| case tag.MP3: | ||
| return "audio/mpeg" | ||
| case tag.OGG: | ||
| return "audio/ogg" | ||
| case tag.M4A, tag.M4B, tag.M4P, tag.ALAC: | ||
| return "audio/mp4" | ||
| case tag.DSF: | ||
| return "audio/dsd" | ||
| default: | ||
| return "" | ||
| } | ||
| } | ||
|
|
||
| // Identify reads from r and returns the detected audio extension and Content-Type. | ||
| // It uses github.com/dhowden/tag to identify the format from the stream. | ||
| // Returns ("", "", err) if the format could not be identified. | ||
| func Identify(r io.ReadSeeker) (ext string, contentType string, err error) { | ||
| _, fileType, err := tag.Identify(r) | ||
| if err != nil || fileType == tag.UnknownFileType { | ||
| return "", "", err | ||
| } | ||
| ext = extensionFromFileType(fileType) | ||
| contentType = contentTypeFromFileType(fileType) | ||
| if ext == "" || contentType == "" { | ||
| return "", "", nil | ||
| } | ||
| return ext, contentType, nil | ||
| } | ||
|
|
||
| // ContentTypeFromExtension returns the MIME type for common audio file extensions. | ||
| // Use as a fallback when Identify fails or when the file is not openable. | ||
| func ContentTypeFromExtension(path string) string { | ||
| ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(path), ".")) | ||
| switch ext { | ||
| case "flac": | ||
| return "audio/flac" | ||
| case "mp3": | ||
| return "audio/mpeg" | ||
| case "wav": | ||
| return "audio/wav" | ||
| case "ogg": | ||
| return "audio/ogg" | ||
| case "m4a", "m4b", "m4p": | ||
| return "audio/mp4" | ||
| case "webm": | ||
| return "audio/webm" | ||
| default: | ||
| return "" | ||
| } | ||
| } | ||
|
|
||
| // NormalizeAudioFile opens the file at path, identifies its format with tag.Identify, | ||
| // and renames the file to have the correct extension if the current one does not match. | ||
| // It returns the path to use (possibly the renamed file) and the Content-Type to set. | ||
| // If identification fails, returns (path, ContentTypeFromExtension(path)). | ||
| func NormalizeAudioFile(path string) (finalPath string, contentType string) { | ||
| finalPath = path | ||
| f, err := os.Open(path) | ||
| if err != nil { | ||
| contentType = ContentTypeFromExtension(path) | ||
| return finalPath, contentType | ||
| } | ||
| defer f.Close() | ||
|
|
||
| ext, ct, identifyErr := Identify(f) | ||
| if identifyErr != nil || ext == "" || ct == "" { | ||
| contentType = ContentTypeFromExtension(path) | ||
| return finalPath, contentType | ||
| } | ||
| contentType = ct | ||
|
|
||
| currentExt := strings.ToLower(strings.TrimPrefix(filepath.Ext(path), ".")) | ||
| if currentExt == ext { | ||
| return finalPath, contentType | ||
| } | ||
|
|
||
| dir := filepath.Dir(path) | ||
| base := filepath.Base(path) | ||
| baseNoExt := strings.TrimSuffix(base, filepath.Ext(base)) | ||
| if baseNoExt == "" { | ||
| baseNoExt = base | ||
| } | ||
| newPath := filepath.Join(dir, baseNoExt+"."+ext) | ||
| if renameErr := os.Rename(path, newPath); renameErr != nil { | ||
| xlog.Debug("Could not rename audio file to match type", "from", path, "to", newPath, "error", renameErr) | ||
| return finalPath, contentType | ||
| } | ||
| return newPath, contentType | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.