Files
kopia/internal/server/api_object_get.go
Jarek Kowalski 9a6dea898b Linter upgrade to v1.30.0 (#526)
* fixed godot linter errors
* reformatted source with gofumpt
* disabled some linters
* fixed nolintlint warnings
* fixed gci warnings
* lint: fixed 'nestif' warnings
* lint: fixed 'exhaustive' warnings
* lint: fixed 'gocritic' warnings
* lint: fixed 'noctx' warnings
* lint: fixed 'wsl' warnings
* lint: fixed 'goerr113' warnings
* lint: fixed 'gosec' warnings
* lint: upgraded linter to 1.30.0
* lint: more 'exhaustive' warnings

Co-authored-by: Nick <nick@kasten.io>
2020-08-12 19:28:53 -07:00

48 lines
1003 B
Go

package server
import (
"errors"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/kopia/kopia/repo/object"
)
func (s *Server) handleObjectGet(w http.ResponseWriter, r *http.Request) {
oidstr := mux.Vars(r)["objectID"]
oid, err := object.ParseID(oidstr)
if err != nil {
http.Error(w, "invalid object id", http.StatusBadRequest)
return
}
obj, err := s.rep.OpenObject(r.Context(), oid)
if errors.Is(err, object.ErrObjectNotFound) {
http.Error(w, "object not found", http.StatusNotFound)
return
}
if cid, _, ok := oid.ContentID(); ok && cid.Prefix() == "k" {
w.Header().Set("Content-Type", "application/json")
}
fname := oid.String()
if p := r.URL.Query().Get("fname"); p != "" {
fname = p
w.Header().Set("Content-Disposition", "attachment; filename=\""+p+"\"")
}
mtime := time.Now()
if p := r.URL.Query().Get("mtime"); p != "" {
if m, err := time.Parse(time.RFC3339Nano, p); err == nil {
mtime = m
}
}
http.ServeContent(w, r, fname, mtime, obj)
}