mirror of
https://github.com/kopia/kopia.git
synced 2026-01-26 07:18:02 -05:00
* linter: upgraded to 1.33, disabled some linters * lint: fixed 'errorlint' errors This ensures that all error comparisons use errors.Is() or errors.As(). We will be wrapping more errors going forward so it's important that error checks are not strict everywhere. Verified that there are no exceptions for errorlint linter which guarantees that. * lint: fixed or suppressed wrapcheck errors * lint: nolintlint and misc cleanups Co-authored-by: Julio López <julio+gh@kasten.io>
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"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, body []byte) (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 errors.Is(err, content.ErrContentNotFound) {
|
|
return nil, notFoundError("content not found")
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|
|
func (s *Server) handleContentInfo(ctx context.Context, r *http.Request, body []byte) (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 {
|
|
case err == nil:
|
|
return ci, nil
|
|
|
|
case errors.Is(err, content.ErrContentNotFound):
|
|
return nil, notFoundError("content not found")
|
|
|
|
default:
|
|
return nil, internalServerError(err)
|
|
}
|
|
}
|
|
|
|
func (s *Server) handleContentPut(ctx context.Context, r *http.Request, data []byte) (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()
|
|
|
|
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
|
|
}
|