mirror of
https://github.com/kopia/kopia.git
synced 2026-01-27 07:48:06 -05:00
Support for remote content repository where all contents and manifests are fetched over HTTP(S) instead of locally manipulating blob storage * server: implement content and manifest access APIs * apiclient: moved Kopia API client to separate package * content: exposed content.ValidatePrefix() * manifest: added JSON serialization attributes to EntryMetadata * repo: changed repo.Open() to return Repository instead of *DirectRepository * repo: added apiServerRepository * cli: added 'kopia repository connect server' This sets up repository connection via the API server instead of directly-manipulated storage. * server: add support for specifying a list of usernames/password via --htpasswd-file * tests: added API server repository E2E test * server: only return manifests (policies and snapshots) belonging to authenticated user
77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/kopia/kopia/internal/serverapi"
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/repo/content"
|
|
)
|
|
|
|
func (s *Server) handleContentGet(ctx context.Context, r *http.Request) (interface{}, *apiError) {
|
|
dr, ok := s.rep.(*repo.DirectRepository)
|
|
if !ok {
|
|
return nil, notFoundError("content not found")
|
|
}
|
|
|
|
cid := content.ID(mux.Vars(r)["contentID"])
|
|
|
|
data, err := dr.Content.GetContent(ctx, cid)
|
|
if err == content.ErrContentNotFound {
|
|
return nil, notFoundError("content not found")
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|
|
func (s *Server) handleContentInfo(ctx context.Context, r *http.Request) (interface{}, *apiError) {
|
|
dr, ok := s.rep.(*repo.DirectRepository)
|
|
if !ok {
|
|
return nil, notFoundError("content not found")
|
|
}
|
|
|
|
cid := content.ID(mux.Vars(r)["contentID"])
|
|
|
|
ci, err := dr.Content.ContentInfo(ctx, cid)
|
|
switch err {
|
|
case nil:
|
|
return ci, nil
|
|
|
|
case content.ErrContentNotFound:
|
|
return nil, notFoundError("content not found")
|
|
|
|
default:
|
|
return nil, internalServerError(err)
|
|
}
|
|
}
|
|
|
|
func (s *Server) handleContentPut(ctx context.Context, r *http.Request) (interface{}, *apiError) {
|
|
dr, ok := s.rep.(*repo.DirectRepository)
|
|
if !ok {
|
|
return nil, notFoundError("content not found")
|
|
}
|
|
|
|
cid := content.ID(mux.Vars(r)["contentID"])
|
|
prefix := cid.Prefix()
|
|
|
|
data, err := ioutil.ReadAll(r.Body)
|
|
if err != nil {
|
|
return nil, requestError(serverapi.ErrorMalformedRequest, "malformed request body")
|
|
}
|
|
|
|
actualCID, err := dr.Content.WriteContent(ctx, data, prefix)
|
|
if err != nil {
|
|
return nil, internalServerError(err)
|
|
}
|
|
|
|
if actualCID != cid {
|
|
return nil, requestError(serverapi.ErrorMalformedRequest, "mismatched content ID")
|
|
}
|
|
|
|
return &serverapi.Empty{}, nil
|
|
}
|