You need to sign in or sign up before continuing.
pin_bytes.go 2.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package api

import (
	"errors"
	"net/http"

	"github.com/ethersphere/bee/pkg/jsonhttp"
12
	"github.com/ethersphere/bee/pkg/storage"
13 14 15 16 17 18 19 20 21
	"github.com/ethersphere/bee/pkg/swarm"
	"github.com/ethersphere/bee/pkg/traversal"
	"github.com/gorilla/mux"
)

// pinBytes is used to pin an already uploaded content.
func (s *server) pinBytes(w http.ResponseWriter, r *http.Request) {
	addr, err := swarm.ParseHexAddress(mux.Vars(r)["address"])
	if err != nil {
22 23
		s.logger.Debugf("pin bytes: parse address: %v", err)
		s.logger.Error("pin bytes: parse address")
24 25 26 27
		jsonhttp.BadRequest(w, "bad address")
		return
	}

28
	has, err := s.storer.Has(r.Context(), addr)
29
	if err != nil {
30 31
		s.logger.Debugf("pin bytes: localstore has: %v", err)
		s.logger.Error("pin bytes: store")
32 33 34 35 36
		jsonhttp.InternalServerError(w, err)
		return
	}

	if !has {
37
		_, err := s.storer.Get(r.Context(), storage.ModeGetRequest, addr)
38
		if err != nil {
39 40
			s.logger.Debugf("pin chunk: netstore get: %v", err)
			s.logger.Error("pin chunk: netstore")
41 42 43 44

			jsonhttp.NotFound(w, nil)
			return
		}
45 46 47 48 49 50
	}

	ctx := r.Context()

	chunkAddressFn := s.pinChunkAddressFn(ctx, addr)

51
	err = s.traversal.TraverseBytesAddresses(ctx, addr, chunkAddressFn)
52
	if err != nil {
53
		s.logger.Debugf("pin bytes: traverse chunks: %v, addr %s", err, addr)
54 55

		if errors.Is(err, traversal.ErrInvalidType) {
56
			s.logger.Error("pin bytes: invalid type")
57 58 59 60
			jsonhttp.BadRequest(w, "invalid type")
			return
		}

61
		s.logger.Error("pin bytes: cannot pin")
62 63 64 65 66 67 68 69 70 71 72
		jsonhttp.InternalServerError(w, "cannot pin")
		return
	}

	jsonhttp.OK(w, nil)
}

// unpinBytes removes pinning from content.
func (s *server) unpinBytes(w http.ResponseWriter, r *http.Request) {
	addr, err := swarm.ParseHexAddress(mux.Vars(r)["address"])
	if err != nil {
73 74
		s.logger.Debugf("pin bytes: parse address: %v", err)
		s.logger.Error("pin bytes: parse address")
75 76 77 78
		jsonhttp.BadRequest(w, "bad address")
		return
	}

79
	has, err := s.storer.Has(r.Context(), addr)
80
	if err != nil {
81 82
		s.logger.Debugf("pin bytes: localstore has: %v", err)
		s.logger.Error("pin bytes: store")
83 84 85 86 87 88 89 90 91 92 93 94 95
		jsonhttp.InternalServerError(w, err)
		return
	}

	if !has {
		jsonhttp.NotFound(w, nil)
		return
	}

	ctx := r.Context()

	chunkAddressFn := s.unpinChunkAddressFn(ctx, addr)

96
	err = s.traversal.TraverseBytesAddresses(ctx, addr, chunkAddressFn)
97
	if err != nil {
98
		s.logger.Debugf("pin bytes: traverse chunks: %v, addr %s", err, addr)
99 100

		if errors.Is(err, traversal.ErrInvalidType) {
101
			s.logger.Error("pin bytes: invalid type")
102 103 104 105
			jsonhttp.BadRequest(w, "invalid type")
			return
		}

106
		s.logger.Error("pin bytes: cannot unpin")
107 108 109 110 111 112
		jsonhttp.InternalServerError(w, "cannot unpin")
		return
	}

	jsonhttp.OK(w, nil)
}