From f88ac098cbc5c2b4d6931cdd443888c7ee738f23 Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Sun, 22 Apr 2018 14:11:52 +0200 Subject: [PATCH] swarm/api: for loop for GetFile --- swarm/api/api.go | 2 +- swarm/api/http/server.go | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/swarm/api/api.go b/swarm/api/api.go index 1e08b71d35..84ac95769b 100644 --- a/swarm/api/api.go +++ b/swarm/api/api.go @@ -308,7 +308,7 @@ func (self *Api) Put(content, contentType string, toEncrypt bool) (k storage.Key // Get uses iterative manifest retrieval and prefix matching // to resolve basePath to content using dpa retrieve // it returns a section reader, mimeType, status and an error -func (self *Api) Get(key storage.Key, path string) (reader storage.LazySectionReader, mimeType string, status int, err error) { +func (self *Api) Get(key storage.Key, path string) (reader *storage.LazyChunkReader, mimeType string, status int, err error) { log.Debug("api.get", "key", key, "path", path) apiGetCount.Inc(1) trie, err := loadManifest(self.dpa, key, nil) diff --git a/swarm/api/http/server.go b/swarm/api/http/server.go index 4ce832863f..aab253e12b 100644 --- a/swarm/api/http/server.go +++ b/swarm/api/http/server.go @@ -844,9 +844,23 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *Request) { return } + var res []byte + for { + res, err = ioutil.ReadAll(reader) + if err != nil { + log.Error("handle.get.file", "ruid", r.ruid, "error", err) + time.Sleep(200 * time.Millisecond) + continue + } + log.Debug("handle.get.file.readall success", "ruid", r.ruid) + break + } + + rdr := bytes.NewReader(res) + w.Header().Set("Content-Type", contentType) - http.ServeContent(w, &r.Request, "", time.Now(), reader) + http.ServeContent(w, &r.Request, "", time.Now(), rdr) } func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {