Skip to content

Commit 1d76b09

Browse files
committed
Fix APK's Content-Type header
1 parent 3656a2a commit 1d76b09

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

modules/base/tool.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ func IsAudioFile(data []byte) bool {
332332
return strings.Contains(DetectContentType(data), "audio/")
333333
}
334334

335+
// IsZipFile detects if data is a zip format
336+
func IsZipFile(data []byte) bool {
337+
return strings.Contains(DetectContentType(data), "application/zip")
338+
}
339+
335340
// EntryIcon returns the octicon class for displaying files/directories
336341
func EntryIcon(entry *git.TreeEntry) string {
337342
switch {

modules/base/tool_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,12 @@ func TestIsAudioFile(t *testing.T) {
329329
assert.False(t, IsAudioFile([]byte("plain text")))
330330
}
331331

332+
func TestIsZipFile(t *testing.T) {
333+
zip, _ := base64.StdEncoding.DecodeString("UEsDBAoAAAAAANG0d1IAAAAAAAAAAAAAAAAIABwAdGV4dC50eHRVVAkAA9pfWmDaX1pgdXgLAAEE9QEAAAQUAAAAUEsBAh4DCgAAAAAA0bR3UgAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAAKSBAAAAAHRleHQudHh0VVQFAAPaX1pgdXgLAAEE9QEAAAQUAAAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA==")
334+
assert.True(t, IsZipFile(zip))
335+
assert.False(t, IsZipFile([]byte("plain text")))
336+
}
337+
332338
// TODO: Test EntryIcon
333339

334340
func TestSetupGiteaRoot(t *testing.T) {

routers/repo/download.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"io"
1111
"path"
12+
"path/filepath"
1213
"strings"
1314

1415
"code.gitea.io/gitea/modules/base"
@@ -61,6 +62,9 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
6162
} else {
6263
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))
6364
ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
65+
if filepath.Ext(name) == ".apk" && base.IsZipFile(buf) {
66+
ctx.Resp.Header().Set("Content-Type", "application/vnd.android.package-archive")
67+
}
6468
}
6569

6670
_, err = ctx.Resp.Write(buf)

0 commit comments

Comments
 (0)