Browse Source

return http response directly

pull/1455/head
Chris Lu 4 years ago
parent
commit
4fc0bd1a81
  1. 14
      weed/command/download.go
  2. 9
      weed/replication/sink/filersink/fetch_write.go
  3. 6
      weed/replication/source/filer_source.go
  4. 6
      weed/s3api/s3api_object_copy_handlers.go
  5. 4
      weed/util/http_util.go

14
weed/command/download.go

@ -4,6 +4,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
@ -59,7 +60,7 @@ func downloadToFile(server, fileId, saveDir string) error {
if err != nil {
return err
}
defer rc.Close()
defer util.CloseResponse(rc)
if filename == "" {
filename = fileId
}
@ -71,12 +72,11 @@ func downloadToFile(server, fileId, saveDir string) error {
}
f, err := os.OpenFile(path.Join(saveDir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
io.Copy(ioutil.Discard, rc)
return err
}
defer f.Close()
if isFileList {
content, err := ioutil.ReadAll(rc)
content, err := ioutil.ReadAll(rc.Body)
if err != nil {
return err
}
@ -95,7 +95,7 @@ func downloadToFile(server, fileId, saveDir string) error {
}
}
} else {
if _, err = io.Copy(f, rc); err != nil {
if _, err = io.Copy(f, rc.Body); err != nil {
return err
}
@ -108,12 +108,12 @@ func fetchContent(server string, fileId string) (filename string, content []byte
if lookupError != nil {
return "", nil, lookupError
}
var rc io.ReadCloser
var rc *http.Response
if filename, _, rc, e = util.DownloadFile(fileUrl); e != nil {
return "", nil, e
}
content, e = ioutil.ReadAll(rc)
rc.Close()
defer util.CloseResponse(rc)
content, e = ioutil.ReadAll(rc.Body)
return
}

9
weed/replication/sink/filersink/fetch_write.go

@ -3,6 +3,7 @@ package filersink
import (
"context"
"fmt"
"github.com/chrislusf/seaweedfs/weed/util"
"sync"
"google.golang.org/grpc"
@ -59,11 +60,11 @@ func (fs *FilerSink) replicateOneChunk(sourceChunk *filer_pb.FileChunk, dir stri
func (fs *FilerSink) fetchAndWrite(sourceChunk *filer_pb.FileChunk, dir string) (fileId string, err error) {
filename, header, readCloser, err := fs.filerSource.ReadPart(sourceChunk.GetFileIdString())
filename, header, resp, err := fs.filerSource.ReadPart(sourceChunk.GetFileIdString())
if err != nil {
return "", fmt.Errorf("read part %s: %v", sourceChunk.GetFileIdString(), err)
}
defer readCloser.Close()
defer util.CloseResponse(resp)
var host string
var auth security.EncodedJwt
@ -100,9 +101,9 @@ func (fs *FilerSink) fetchAndWrite(sourceChunk *filer_pb.FileChunk, dir string)
glog.V(4).Infof("replicating %s to %s header:%+v", filename, fileUrl, header)
// fetch data as is, regardless whether it is encrypted or not
uploadResult, err, _ := operation.Upload(fileUrl, filename, false, readCloser, "gzip" == header.Get("Content-Encoding"), header.Get("Content-Type"), nil, auth)
uploadResult, err, _ := operation.Upload(fileUrl, filename, false, resp.Body, "gzip" == header.Get("Content-Encoding"), header.Get("Content-Type"), nil, auth)
if err != nil {
glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
glog.V(0).Infof("upload source data %v to %s: %v", sourceChunk.GetFileIdString(), fileUrl, err)
return "", fmt.Errorf("upload data: %v", err)
}
if uploadResult.Error != "" {

6
weed/replication/source/filer_source.go

@ -79,16 +79,16 @@ func (fs *FilerSource) LookupFileId(part string) (fileUrl string, err error) {
return
}
func (fs *FilerSource) ReadPart(part string) (filename string, header http.Header, readCloser io.ReadCloser, err error) {
func (fs *FilerSource) ReadPart(part string) (filename string, header http.Header, resp *http.Response, err error) {
fileUrl, err := fs.LookupFileId(part)
if err != nil {
return "", nil, nil, err
}
filename, header, readCloser, err = util.DownloadFile(fileUrl)
filename, header, resp, err = util.DownloadFile(fileUrl)
return filename, header, readCloser, err
return filename, header, resp, err
}
var _ = filer_pb.FilerClient(&FilerSource{})

6
weed/s3api/s3api_object_copy_handlers.go

@ -39,14 +39,14 @@ func (s3a *S3ApiServer) CopyObjectHandler(w http.ResponseWriter, r *http.Request
srcUrl := fmt.Sprintf("http://%s%s/%s%s",
s3a.option.Filer, s3a.option.BucketsPath, srcBucket, srcObject)
_, _, dataReader, err := util.DownloadFile(srcUrl)
_, _, resp, err := util.DownloadFile(srcUrl)
if err != nil {
writeErrorResponse(w, ErrInvalidCopySource, r.URL)
return
}
defer dataReader.Close()
defer util.CloseResponse(resp)
etag, errCode := s3a.putToFiler(r, dstUrl, dataReader)
etag, errCode := s3a.putToFiler(r, dstUrl, resp.Body)
if errCode != ErrNone {
writeErrorResponse(w, errCode, r.URL)

4
weed/util/http_util.go

@ -174,7 +174,7 @@ func GetUrlStream(url string, values url.Values, readFn func(io.Reader) error) e
return readFn(r.Body)
}
func DownloadFile(fileUrl string) (filename string, header http.Header, rc io.ReadCloser, e error) {
func DownloadFile(fileUrl string) (filename string, header http.Header, resp *http.Response, e error) {
response, err := client.Get(fileUrl)
if err != nil {
return "", nil, nil, err
@ -188,7 +188,7 @@ func DownloadFile(fileUrl string) (filename string, header http.Header, rc io.Re
filename = strings.Trim(filename, "\"")
}
}
rc = response.Body
resp = response
return
}

Loading…
Cancel
Save