Repository: rclone/rclone
File: cmd/serve/restic/restic.go (Go)

Code before:
// newObject returns an object with the remote given either from the
// cache or directly
func (s *server) newObject(ctx context.Context, remote string) (fs.Object, error) {
	o := s.cache.find(remote)
	if o != nil {
		return o, nil
	}
	o, err := s.f.NewObject(ctx, remote)
	if err != nil {
		return o, err
	}
	s.cache.add(remote, o)
	return o, nil
}

// get the remote
func (s *server) serveObject(w http.ResponseWriter, r *http.Request) {
	remote, ok := r.Context().Value(ContextRemoteKey).(string)
	if !ok {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	o, err := s.newObject(r.Context(), remote)
	if err != nil {
		fs.Debugf(remote, "%s request error: %v", r.Method, err)
		if err == fs.ErrorObjectNotFound {
			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
		} else {
			http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		}
		return
	}
	serve.Object(w, r, o)
}

// postObject posts an object to the repository
func (s *server) postObject(w http.ResponseWriter, r *http.Request) {
	remote, ok := r.Context().Value(ContextRemoteKey).(string)
	if !ok {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	if s.opt.AppendOnly {
		// make sure the file does not exist yet
		_, err := s.newObject(r.Context(), remote)
		if err == nil {
			fs.Errorf(remote, "Post request: file already exists, refusing to overwrite in append-only mode")
			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)

			return
		}

Review comment (suggestion):
This should probably use `errors.Is` as we do elsewhere in this code.

Code after:
// newObject returns an object with the remote given either from the
// cache or directly
func (s *server) newObject(ctx context.Context, remote string) (fs.Object, error) {
	o := s.cache.find(remote)
	if o != nil {
		return o, nil
	}
	o, err := s.f.NewObject(ctx, remote)
	if err != nil {
		return o, err
	}
	s.cache.add(remote, o)
	return o, nil
}

// get the remote
func (s *server) serveObject(w http.ResponseWriter, r *http.Request) {
	remote, ok := r.Context().Value(ContextRemoteKey).(string)
	if !ok {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	o, err := s.newObject(r.Context(), remote)
	if err != nil {
		fs.Debugf(remote, "%s request error: %v", r.Method, err)
		if errors.Is(err, fs.ErrorObjectNotFound) {
			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
		} else {
			http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		}
		return
	}
	serve.Object(w, r, o)
}

// postObject posts an object to the repository
func (s *server) postObject(w http.ResponseWriter, r *http.Request) {
	remote, ok := r.Context().Value(ContextRemoteKey).(string)
	if !ok {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	if s.opt.AppendOnly {
		// make sure the file does not exist yet
		_, err := s.newObject(r.Context(), remote)
		if err == nil {
			fs.Errorf(remote, "Post request: file already exists, refusing to overwrite in append-only mode")
			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)

			return
		}