mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
refactoring: go fmt, reorg
This commit is contained in:
parent
480a073f1f
commit
8db9319a06
|
@ -82,10 +82,10 @@ func ReadFromChunks(chunks []*filer_pb.FileChunk, offset int64, size int) (views
|
||||||
|
|
||||||
func logPrintf(name string, visibles []*visibleInterval) {
|
func logPrintf(name string, visibles []*visibleInterval) {
|
||||||
/*
|
/*
|
||||||
log.Printf("%s len %d", name, len(visibles))
|
log.Printf("%s len %d", name, len(visibles))
|
||||||
for _, v := range visibles {
|
for _, v := range visibles {
|
||||||
log.Printf("%s: => %+v", name, v)
|
log.Printf("%s: => %+v", name, v)
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,13 +8,13 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/chrislusf/seaweedfs/weed/filer2"
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
||||||
"github.com/chrislusf/seaweedfs/weed/glog"
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
||||||
"github.com/chrislusf/seaweedfs/weed/operation"
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
||||||
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
||||||
"github.com/chrislusf/seaweedfs/weed/util"
|
"github.com/chrislusf/seaweedfs/weed/util"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type FilerPostResult struct {
|
type FilerPostResult struct {
|
||||||
|
|
|
@ -3,15 +3,16 @@ package weed_server
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/chrislusf/seaweedfs/weed/filer2"
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
||||||
"github.com/chrislusf/seaweedfs/weed/glog"
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
||||||
"github.com/chrislusf/seaweedfs/weed/operation"
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
||||||
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
||||||
"time"
|
|
||||||
"io/ioutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (fs *FilerServer) autoChunk(w http.ResponseWriter, r *http.Request, replication string, collection string) bool {
|
func (fs *FilerServer) autoChunk(w http.ResponseWriter, r *http.Request, replication string, collection string) bool {
|
||||||
|
|
|
@ -5,13 +5,56 @@ import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/textproto"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/chrislusf/seaweedfs/weed/glog"
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
|
||||||
|
|
||||||
|
func escapeQuotes(s string) string {
|
||||||
|
return quoteEscaper.Replace(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createFormFile(writer *multipart.Writer, fieldname, filename, mime string) (io.Writer, error) {
|
||||||
|
h := make(textproto.MIMEHeader)
|
||||||
|
h.Set("Content-Disposition",
|
||||||
|
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
|
||||||
|
escapeQuotes(fieldname), escapeQuotes(filename)))
|
||||||
|
if len(mime) == 0 {
|
||||||
|
mime = "application/octet-stream"
|
||||||
|
}
|
||||||
|
h.Set("Content-Type", mime)
|
||||||
|
return writer.CreatePart(h)
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeFormData(filename, mimeType string, content io.Reader) (formData io.Reader, contentType string, err error) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
writer := multipart.NewWriter(buf)
|
||||||
|
defer writer.Close()
|
||||||
|
|
||||||
|
part, err := createFormFile(writer, "file", filename, mimeType)
|
||||||
|
if err != nil {
|
||||||
|
glog.V(0).Infoln(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = io.Copy(part, content)
|
||||||
|
if err != nil {
|
||||||
|
glog.V(0).Infoln(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
formData = buf
|
||||||
|
contentType = writer.FormDataContentType()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func checkContentMD5(w http.ResponseWriter, r *http.Request) (err error) {
|
func checkContentMD5(w http.ResponseWriter, r *http.Request) (err error) {
|
||||||
if contentMD5 := r.Header.Get("Content-MD5"); contentMD5 != "" {
|
if contentMD5 := r.Header.Get("Content-MD5"); contentMD5 != "" {
|
||||||
buf, _ := ioutil.ReadAll(r.Body)
|
buf, _ := ioutil.ReadAll(r.Body)
|
||||||
|
@ -65,3 +108,32 @@ func (fs *FilerServer) monolithicUploadAnalyzer(w http.ResponseWriter, r *http.R
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func multipartHttpBodyBuilder(w http.ResponseWriter, r *http.Request, fileName string) (err error) {
|
||||||
|
body, contentType, te := makeFormData(fileName, r.Header.Get("Content-Type"), r.Body)
|
||||||
|
if te != nil {
|
||||||
|
glog.V(0).Infoln("S3 protocol to raw seaweed protocol failed", te.Error())
|
||||||
|
writeJsonError(w, r, http.StatusInternalServerError, te)
|
||||||
|
err = te
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if body != nil {
|
||||||
|
switch v := body.(type) {
|
||||||
|
case *bytes.Buffer:
|
||||||
|
r.ContentLength = int64(v.Len())
|
||||||
|
case *bytes.Reader:
|
||||||
|
r.ContentLength = int64(v.Len())
|
||||||
|
case *strings.Reader:
|
||||||
|
r.ContentLength = int64(v.Len())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Header.Set("Content-Type", contentType)
|
||||||
|
rc, ok := body.(io.ReadCloser)
|
||||||
|
if !ok && body != nil {
|
||||||
|
rc = ioutil.NopCloser(body)
|
||||||
|
}
|
||||||
|
r.Body = rc
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -2,58 +2,14 @@ package weed_server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"mime/multipart"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/textproto"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/chrislusf/seaweedfs/weed/glog"
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
||||||
"github.com/chrislusf/seaweedfs/weed/storage"
|
"github.com/chrislusf/seaweedfs/weed/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
|
|
||||||
|
|
||||||
func escapeQuotes(s string) string {
|
|
||||||
return quoteEscaper.Replace(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func createFormFile(writer *multipart.Writer, fieldname, filename, mime string) (io.Writer, error) {
|
|
||||||
h := make(textproto.MIMEHeader)
|
|
||||||
h.Set("Content-Disposition",
|
|
||||||
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
|
|
||||||
escapeQuotes(fieldname), escapeQuotes(filename)))
|
|
||||||
if len(mime) == 0 {
|
|
||||||
mime = "application/octet-stream"
|
|
||||||
}
|
|
||||||
h.Set("Content-Type", mime)
|
|
||||||
return writer.CreatePart(h)
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeFormData(filename, mimeType string, content io.Reader) (formData io.Reader, contentType string, err error) {
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
writer := multipart.NewWriter(buf)
|
|
||||||
defer writer.Close()
|
|
||||||
|
|
||||||
part, err := createFormFile(writer, "file", filename, mimeType)
|
|
||||||
if err != nil {
|
|
||||||
glog.V(0).Infoln(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_, err = io.Copy(part, content)
|
|
||||||
if err != nil {
|
|
||||||
glog.V(0).Infoln(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
formData = buf
|
|
||||||
contentType = writer.FormDataContentType()
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fs *FilerServer) multipartUploadAnalyzer(w http.ResponseWriter, r *http.Request, replication, collection string) (fileId, urlLocation string, err error) {
|
func (fs *FilerServer) multipartUploadAnalyzer(w http.ResponseWriter, r *http.Request, replication, collection string) (fileId, urlLocation string, err error) {
|
||||||
//Default handle way for http multipart
|
//Default handle way for http multipart
|
||||||
if r.Method == "PUT" {
|
if r.Method == "PUT" {
|
||||||
|
@ -81,32 +37,3 @@ func (fs *FilerServer) multipartUploadAnalyzer(w http.ResponseWriter, r *http.Re
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func multipartHttpBodyBuilder(w http.ResponseWriter, r *http.Request, fileName string) (err error) {
|
|
||||||
body, contentType, te := makeFormData(fileName, r.Header.Get("Content-Type"), r.Body)
|
|
||||||
if te != nil {
|
|
||||||
glog.V(0).Infoln("S3 protocol to raw seaweed protocol failed", te.Error())
|
|
||||||
writeJsonError(w, r, http.StatusInternalServerError, te)
|
|
||||||
err = te
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if body != nil {
|
|
||||||
switch v := body.(type) {
|
|
||||||
case *bytes.Buffer:
|
|
||||||
r.ContentLength = int64(v.Len())
|
|
||||||
case *bytes.Reader:
|
|
||||||
r.ContentLength = int64(v.Len())
|
|
||||||
case *strings.Reader:
|
|
||||||
r.ContentLength = int64(v.Len())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
r.Header.Set("Content-Type", contentType)
|
|
||||||
rc, ok := body.(io.ReadCloser)
|
|
||||||
if !ok && body != nil {
|
|
||||||
rc = ioutil.NopCloser(body)
|
|
||||||
}
|
|
||||||
r.Body = rc
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue