Fix content_type detection for extensions GitHub rejects (#32) - #33
Merged
Conversation
detectContentType relied on mime.TypeByExtension, whose output GitHub's
upload policy endpoint rejects (422 "content_type is not included in the
list") for many extensions:
- On Windows, mime reads the registry, which returns legacy types for
JPEG (image/pjpeg / image/jpg) — the failure reported in #32.
- On every OS, Go has no mapping for several code/data extensions
(.md, .yaml, .py, .ts, .tsx, ...), so detectContentType fell back to
application/octet-stream, which GitHub rejects for text types. This
broke uploads of those file types even on macOS/Linux.
- A few extensions map to a type GitHub rejects (.wav -> audio/x-wav,
.cpp -> text/x-c, .ts -> video/mp2t, .pdb -> application/vnd.palm,
.sql -> application/x-sql).
Expand the githubContentType override map with 21 entries, each verified
accepted (HTTP 201) against GitHub's policy endpoint, covering only the
extensions Go would otherwise get wrong. Extensions whose mime value is
already accepted continue to use the mime fallback.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #32.
Problem
detectContentTyperelied onmime.TypeByExtension, whose output GitHub's upload policy endpoint (POST /upload/policies/assets) rejects with422 content_type is not included in the listfor many extensions:.jpg(the reported bug): on Windows, Go'smimereads the registry, which commonly returns legacyimage/pjpeg/image/jpg. GitHub only acceptsimage/jpeg. Renaming to.jpegworked because that path yieldedimage/jpeg..md,.yaml,.yml,.jsonc,.cs,.php,.py,.patch,.cpuprofile,.ipynb,.tsx,.tgz) Go's mime table returns nothing, sodetectContentTypefell back toapplication/octet-stream, which GitHub rejects for text types. Uploading these advertised file types failed on every platform..wav→audio/x-wav,.cpp→text/x-c,.ts→video/mp2t(the well-known MPEG-TS collision),.pdb→application/vnd.palm,.sql→application/x-sql— all rejected.Fix
Expand the existing
githubContentTypeoverride map (which previously held only.log) with 21 entries. Each accepted value was verified against GitHub's live policy endpoint (HTTP 201), and the type Go would otherwise send was verified rejected (422). Only extensions Go gets wrong are pinned; extensions whose mime value GitHub already accepts continue to use themime.TypeByExtensionfallback.Notable verified mappings:
.ts→text/typescript,.tsx→text/tsx,.js→text/javascript(GitHub rejectsapplication/javascript),.yaml/.yml→application/x-yaml(GitHub rejectstext/yaml).Testing
TestDetectContentTypeininternal/upload/upload_test.go.go build ./...,go vet ./..., andgo test ./...all pass.