swarm/api: for loop for GetFile

This commit is contained in:
Anton Evangelatov 2018-04-22 14:11:52 +02:00
parent b775c55622
commit f88ac098cb
2 changed files with 16 additions and 2 deletions

View file

@ -308,7 +308,7 @@ func (self *Api) Put(content, contentType string, toEncrypt bool) (k storage.Key
// Get uses iterative manifest retrieval and prefix matching // Get uses iterative manifest retrieval and prefix matching
// to resolve basePath to content using dpa retrieve // to resolve basePath to content using dpa retrieve
// it returns a section reader, mimeType, status and an error // 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) log.Debug("api.get", "key", key, "path", path)
apiGetCount.Inc(1) apiGetCount.Inc(1)
trie, err := loadManifest(self.dpa, key, nil) trie, err := loadManifest(self.dpa, key, nil)

View file

@ -844,9 +844,23 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *Request) {
return 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) 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) { func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {