return http response directly

This commit is contained in:
Chris Lu 2020-09-09 03:53:09 -07:00
parent 5e13bc878c
commit 4fc0bd1a81
5 changed files with 20 additions and 19 deletions

View file

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

View file

@ -3,6 +3,7 @@ package filersink
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/chrislusf/seaweedfs/weed/util"
"sync" "sync"
"google.golang.org/grpc" "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) { 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 { if err != nil {
return "", fmt.Errorf("read part %s: %v", sourceChunk.GetFileIdString(), err) return "", fmt.Errorf("read part %s: %v", sourceChunk.GetFileIdString(), err)
} }
defer readCloser.Close() defer util.CloseResponse(resp)
var host string var host string
var auth security.EncodedJwt 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) glog.V(4).Infof("replicating %s to %s header:%+v", filename, fileUrl, header)
// fetch data as is, regardless whether it is encrypted or not // 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 { 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) return "", fmt.Errorf("upload data: %v", err)
} }
if uploadResult.Error != "" { if uploadResult.Error != "" {

View file

@ -79,16 +79,16 @@ func (fs *FilerSource) LookupFileId(part string) (fileUrl string, err error) {
return 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) fileUrl, err := fs.LookupFileId(part)
if err != nil { if err != nil {
return "", nil, nil, err 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{}) var _ = filer_pb.FilerClient(&FilerSource{})

View file

@ -39,14 +39,14 @@ func (s3a *S3ApiServer) CopyObjectHandler(w http.ResponseWriter, r *http.Request
srcUrl := fmt.Sprintf("http://%s%s/%s%s", srcUrl := fmt.Sprintf("http://%s%s/%s%s",
s3a.option.Filer, s3a.option.BucketsPath, srcBucket, srcObject) s3a.option.Filer, s3a.option.BucketsPath, srcBucket, srcObject)
_, _, dataReader, err := util.DownloadFile(srcUrl) _, _, resp, err := util.DownloadFile(srcUrl)
if err != nil { if err != nil {
writeErrorResponse(w, ErrInvalidCopySource, r.URL) writeErrorResponse(w, ErrInvalidCopySource, r.URL)
return 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 { if errCode != ErrNone {
writeErrorResponse(w, errCode, r.URL) writeErrorResponse(w, errCode, r.URL)

View file

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