diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 065d6bf8b2..46dce95929 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -257,7 +257,9 @@ func GetArchive(ctx *context.APIContext) { // --- // summary: Get an archive of a repository // produces: - // - application/json + // - application/octet-stream + // - application/zip + // - application/gzip // parameters: // - name: owner // in: path @@ -337,7 +339,17 @@ func download(ctx *context.APIContext, archiveName string, archiver *repo_model. } defer fr.Close() + contentType := "" + switch archiver.Type { + case git.ZIP: + contentType = "application/zip" + case git.TARGZ: + // Per RFC6713. + contentType = "application/gzip" + } + ctx.ServeContent(fr, &context.ServeHeaderOptions{ + ContentType: contentType, Filename: downloadName, LastModified: archiver.CreatedUnix.AsLocalTime(), }) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 453d8b0809..a09f580e97 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -3533,7 +3533,9 @@ "/repos/{owner}/{repo}/archive/{archive}": { "get": { "produces": [ - "application/json" + "application/octet-stream", + "application/zip", + "application/gzip" ], "tags": [ "repository" diff --git a/tests/integration/api_repo_archive_test.go b/tests/integration/api_repo_archive_test.go index 57d3abfe84..c574d49450 100644 --- a/tests/integration/api_repo_archive_test.go +++ b/tests/integration/api_repo_archive_test.go @@ -32,18 +32,21 @@ func TestAPIDownloadArchive(t *testing.T) { bs, err := io.ReadAll(resp.Body) assert.NoError(t, err) assert.Len(t, bs, 320) + assert.EqualValues(t, "application/zip", resp.Header().Get("Content-Type")) link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master.tar.gz", user2.Name, repo.Name)) resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK) bs, err = io.ReadAll(resp.Body) assert.NoError(t, err) assert.Len(t, bs, 266) + assert.EqualValues(t, "application/gzip", resp.Header().Get("Content-Type")) link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master.bundle", user2.Name, repo.Name)) resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK) bs, err = io.ReadAll(resp.Body) assert.NoError(t, err) assert.Len(t, bs, 382) + assert.EqualValues(t, "application/octet-stream", resp.Header().Get("Content-Type")) link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master", user2.Name, repo.Name)) MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusBadRequest)