2020-03-11 07:22:21 +00:00
|
|
|
package stores
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-sectorbuilder"
|
|
|
|
"github.com/filecoin-project/lotus/lib/tarutil"
|
2020-03-23 11:40:02 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sectorstorage/sectorutil"
|
2020-03-11 07:22:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("stores")
|
|
|
|
|
|
|
|
type FetchHandler struct {
|
2020-03-22 04:27:28 +00:00
|
|
|
*Local
|
2020-03-11 07:22:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 17:11:45 +00:00
|
|
|
func (handler *FetchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // /remote/
|
2020-03-11 07:22:21 +00:00
|
|
|
mux := mux.NewRouter()
|
|
|
|
|
2020-03-19 17:11:45 +00:00
|
|
|
mux.HandleFunc("/remote/{type}/{id}", handler.remoteGetSector).Methods("GET")
|
2020-03-22 04:27:28 +00:00
|
|
|
mux.HandleFunc("/remote/{type}/{id}", handler.remoteDeleteSector).Methods("DELETE")
|
2020-03-11 07:22:21 +00:00
|
|
|
|
|
|
|
mux.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *FetchHandler) remoteGetSector(w http.ResponseWriter, r *http.Request) {
|
2020-03-22 04:27:28 +00:00
|
|
|
log.Infof("SERVE GET %s", r.URL)
|
2020-03-11 07:22:21 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
id, err := sectorutil.ParseSectorID(vars["id"])
|
|
|
|
if err != nil {
|
2020-03-22 04:27:28 +00:00
|
|
|
log.Error("%+v", err)
|
2020-03-11 07:22:21 +00:00
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ft, err := ftFromString(vars["type"])
|
|
|
|
if err != nil {
|
2020-03-22 04:27:28 +00:00
|
|
|
log.Error("%+v", err)
|
2020-03-11 07:22:21 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-22 04:27:28 +00:00
|
|
|
paths, _, done, err := handler.Local.AcquireSector(r.Context(), id, ft, 0, false)
|
2020-03-11 07:22:21 +00:00
|
|
|
if err != nil {
|
2020-03-22 04:27:28 +00:00
|
|
|
log.Error("%+v", err)
|
2020-03-11 07:22:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
2020-03-13 00:23:05 +00:00
|
|
|
path := sectorutil.PathByType(paths, ft)
|
2020-03-11 07:22:21 +00:00
|
|
|
if path == "" {
|
|
|
|
log.Error("acquired path was empty")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2020-03-22 04:27:28 +00:00
|
|
|
log.Error("%+v", err)
|
2020-03-11 07:22:21 +00:00
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rd io.Reader
|
|
|
|
if stat.IsDir() {
|
|
|
|
rd, err = tarutil.TarDirectory(path)
|
|
|
|
w.Header().Set("Content-Type", "application/x-tar")
|
|
|
|
} else {
|
|
|
|
rd, err = os.OpenFile(path, os.O_RDONLY, 0644)
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-03-22 04:27:28 +00:00
|
|
|
log.Error("%+v", err)
|
2020-03-11 07:22:21 +00:00
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
if _, err := io.Copy(w, rd); err != nil { // TODO: default 32k buf may be too small
|
2020-03-22 04:27:28 +00:00
|
|
|
log.Error("%+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *FetchHandler) remoteDeleteSector(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Infof("SERVE DELETE %s", r.URL)
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
id, err := sectorutil.ParseSectorID(vars["id"])
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%+v", err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ft, err := ftFromString(vars["type"])
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := handler.delete(r.Context(), id, ft); err != nil {
|
|
|
|
log.Error("%+v", err)
|
|
|
|
w.WriteHeader(500)
|
2020-03-11 07:22:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ftFromString(t string) (sectorbuilder.SectorFileType, error) {
|
|
|
|
switch t {
|
|
|
|
case sectorbuilder.FTUnsealed.String():
|
|
|
|
return sectorbuilder.FTUnsealed, nil
|
|
|
|
case sectorbuilder.FTSealed.String():
|
|
|
|
return sectorbuilder.FTSealed, nil
|
|
|
|
case sectorbuilder.FTCache.String():
|
|
|
|
return sectorbuilder.FTCache, nil
|
|
|
|
default:
|
|
|
|
return 0, xerrors.Errorf("unknown sector file type: '%s'", t)
|
|
|
|
}
|
|
|
|
}
|