mirror of
https://github.com/kopia/kopia.git
synced 2026-10-21 21:54:41 -04:00
server: added /api/v1/objects/:id that retrieves contents of object given its ID
This commit is contained in:
1 parent
3f721aaf01
commit
5d55568767
2 files changed
+51
-1
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kopia/kopia/repo/object"
|
||||
)
|
||||
|
||||
func (s *Server) handleObjectGet(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
http.Error(w, "incompatible HTTP method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
oidstr := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
|
||||
|
||||
oid, err := object.ParseID(oidstr)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid object id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
obj, err := s.rep.Objects.Open(r.Context(), oid)
|
||||
if 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)
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func (s *Server) APIHandlers() http.Handler {
|
||||
mux.HandleFunc("/api/v1/sources/resume", s.handleAPI(s.handleResume, "POST"))
|
||||
mux.HandleFunc("/api/v1/sources/upload", s.handleAPI(s.handleUpload, "POST"))
|
||||
mux.HandleFunc("/api/v1/sources/cancel", s.handleAPI(s.handleCancel, "POST"))
|
||||
|
||||
mux.HandleFunc("/api/v1/objects/", s.handleObjectGet)
|
||||
return mux
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user