mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
* feat: add golangci-lint linting * chore: fix linter issues * feat: add linting into the workflow * docs: update lint docs * fix: cr suggestions * fix: remove old formatting and vetting scripts * fix: add docker make target * fix: action go caching * fix: depreciated actions checkout version * fix: cr suggestion * fix: cr suggestions --------- Co-authored-by: Manu Gupta <manugupt1@gmail.com>
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package download
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gomods/athens/pkg/download/mode"
|
|
"github.com/gomods/athens/pkg/errors"
|
|
"github.com/gomods/athens/pkg/log"
|
|
)
|
|
|
|
// PathVersionZip URL.
|
|
const PathVersionZip = "/{module:.+}/@v/{version}.zip"
|
|
|
|
// ZipHandler implements GET baseURL/module/@v/version.zip.
|
|
func ZipHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handler {
|
|
const op errors.Op = "download.ZipHandler"
|
|
f := func(w http.ResponseWriter, r *http.Request) {
|
|
mod, ver, err := getModuleParams(r, op)
|
|
if err != nil {
|
|
lggr.SystemErr(err)
|
|
w.WriteHeader(errors.Kind(err))
|
|
return
|
|
}
|
|
zip, err := dp.Zip(r.Context(), mod, ver)
|
|
if err != nil {
|
|
severityLevel := errors.Expect(err, errors.KindNotFound, errors.KindRedirect)
|
|
err = errors.E(op, err, severityLevel)
|
|
lggr.SystemErr(err)
|
|
if errors.Kind(err) == errors.KindRedirect {
|
|
url, err := getRedirectURL(df.URL(mod), r.URL.Path)
|
|
if err != nil {
|
|
lggr.SystemErr(err)
|
|
w.WriteHeader(errors.Kind(err))
|
|
return
|
|
}
|
|
http.Redirect(w, r, url, errors.KindRedirect)
|
|
return
|
|
}
|
|
w.WriteHeader(errors.Kind(err))
|
|
return
|
|
}
|
|
defer func() { _ = zip.Close() }()
|
|
|
|
w.Header().Set("Content-Type", "application/zip")
|
|
size := zip.Size()
|
|
if size > 0 {
|
|
w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
|
|
}
|
|
if r.Method == http.MethodHead {
|
|
return
|
|
}
|
|
_, err = io.Copy(w, zip)
|
|
if err != nil {
|
|
lggr.SystemErr(errors.E(op, errors.M(mod), errors.V(ver), err))
|
|
}
|
|
}
|
|
return http.HandlerFunc(f)
|
|
}
|