diff --git a/piecestorage/store_wrapper.go b/piecestorage/store_wrapper.go index 0133c702..5ee8ca43 100644 --- a/piecestorage/store_wrapper.go +++ b/piecestorage/store_wrapper.go @@ -57,9 +57,12 @@ func (sw *storeWrapper) GetReaderCloser(ctx context.Context, s string) (io.ReadC var rc io.ReadCloser var err error for _, name := range extendPiece(s) { - rc, err = sw.IPieceStorage.GetReaderCloser(ctx, name) - if err == nil { - return rc, nil + has, _ := sw.IPieceStorage.Has(ctx, name) + if has { + rc, err = sw.IPieceStorage.GetReaderCloser(ctx, name) + if err == nil { + return rc, nil + } } } @@ -70,9 +73,12 @@ func (sw *storeWrapper) GetMountReader(ctx context.Context, s string) (mount.Rea var reader mount.Reader var err error for _, name := range extendPiece(s) { - reader, err = sw.IPieceStorage.GetMountReader(ctx, name) - if err == nil { - return reader, nil + has, _ := sw.IPieceStorage.Has(ctx, name) + if has { + reader, err = sw.IPieceStorage.GetMountReader(ctx, name) + if err == nil { + return reader, nil + } } } diff --git a/retrievalprovider/httpretrieval/server.go b/retrievalprovider/httpretrieval/server.go index 82343b7a..e23df4a6 100644 --- a/retrievalprovider/httpretrieval/server.go +++ b/retrievalprovider/httpretrieval/server.go @@ -106,6 +106,10 @@ func (s *Server) retrievalByPieceCID(w http.ResponseWriter, r *http.Request) { } defer mountReader.Close() // nolint + var buf [32]byte + _, _ = mountReader.Read(buf[:]) + fmt.Println("xxxx", string(buf[:])) + contentReader, err := handleRangeHeader(r.Header.Get("Range"), mountReader, len) if err != nil { log.Warnf("handleRangeHeader failed, Range: %s, error: %v", r.Header.Get("Range"), err) @@ -179,7 +183,11 @@ func serveContent(w http.ResponseWriter, r *http.Request, content io.ReadSeeker, // Write a line to the log end := time.Now() total, count := writeErrWatcher.total, writeErrWatcher.count - avg := total / count + var avg uint64 + if count != 0 { + avg = total / count + } + completeMsg := fmt.Sprintf("GET %s\t%s - %s: %s / %s transferred", r.URL, end.Format(time.RFC3339), start.Format(time.RFC3339), time.Since(start), fmt.Sprintf("total %s (%d B), average write %s ", types.SizeStr(types.NewInt(total)), total, types.SizeStr(types.NewInt(avg)))) diff --git a/retrievalprovider/httpretrieval/server_test.go b/retrievalprovider/httpretrieval/server_test.go index 7455567c..d02eca63 100644 --- a/retrievalprovider/httpretrieval/server_test.go +++ b/retrievalprovider/httpretrieval/server_test.go @@ -80,8 +80,9 @@ func TestRetrievalByPiece(t *testing.T) { piece, err := cid.Decode(pieceStr) assert.NoError(t, err) buf := &bytes.Buffer{} - f, err := os.Create(filepath.Join(tmpDri, pieceStr)) + f, err := os.Create(filepath.Join(tmpDri, pieceStr+".car")) assert.NoError(t, err) + assert.NoError(t, os.MkdirAll(filepath.Join(tmpDri, pieceStr), os.ModePerm)) for i := 0; i < 100; i++ { buf.WriteString("TEST TEST\n") }