Browse Source
Merge pull request #2383 from kmlebedev/s3MimeDetect
s3 mime detect
pull/2388/head
Chris Lu
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with
22 additions and
1 deletions
-
weed/s3api/filer_multipart.go
-
weed/s3api/s3api_object_handlers.go
-
weed/s3api/s3api_object_multipart_handlers.go
|
|
@ -82,9 +82,13 @@ func (s3a *S3ApiServer) completeMultipartUpload(input *s3.CompleteMultipartUploa |
|
|
|
|
|
|
|
var finalParts []*filer_pb.FileChunk |
|
|
|
var offset int64 |
|
|
|
var mime string |
|
|
|
|
|
|
|
for _, entry := range entries { |
|
|
|
if strings.HasSuffix(entry.Name, ".part") && !entry.IsDirectory { |
|
|
|
if entry.Name == "0001.part" && entry.Attributes.Mime != "" { |
|
|
|
mime = entry.Attributes.Mime |
|
|
|
} |
|
|
|
for _, chunk := range entry.Chunks { |
|
|
|
p := &filer_pb.FileChunk{ |
|
|
|
FileId: chunk.GetFileIdString(), |
|
|
@ -126,6 +130,8 @@ func (s3a *S3ApiServer) completeMultipartUpload(input *s3.CompleteMultipartUploa |
|
|
|
} |
|
|
|
if pentry.Attributes.Mime != "" { |
|
|
|
entry.Attributes.Mime = pentry.Attributes.Mime |
|
|
|
} else if mime != "" { |
|
|
|
entry.Attributes.Mime = mime |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
@ -1,6 +1,7 @@ |
|
|
|
package s3api |
|
|
|
|
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"crypto/md5" |
|
|
|
"encoding/json" |
|
|
|
"encoding/xml" |
|
|
@ -36,6 +37,13 @@ func init() { |
|
|
|
}} |
|
|
|
} |
|
|
|
|
|
|
|
func mimeDetect(r *http.Request, dataReader io.Reader) io.ReadCloser { |
|
|
|
mimeBuffer := make([]byte, 512) |
|
|
|
dataReader.Read(mimeBuffer) |
|
|
|
r.Header.Set("Content-Type", http.DetectContentType(mimeBuffer)) |
|
|
|
return io.NopCloser(io.MultiReader(bytes.NewReader(mimeBuffer), dataReader)) |
|
|
|
} |
|
|
|
|
|
|
|
func (s3a *S3ApiServer) PutObjectHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
// http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadingObjects.html
|
|
|
@ -95,6 +103,10 @@ func (s3a *S3ApiServer) PutObjectHandler(w http.ResponseWriter, r *http.Request) |
|
|
|
} else { |
|
|
|
uploadUrl := fmt.Sprintf("http://%s%s/%s%s", s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, bucket, urlPathEscape(object)) |
|
|
|
|
|
|
|
if r.Header.Get("Content-Type") == "" { |
|
|
|
dataReader = mimeDetect(r, dataReader) |
|
|
|
} |
|
|
|
|
|
|
|
etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader) |
|
|
|
|
|
|
|
if errCode != s3err.ErrNone { |
|
|
|
|
|
@ -217,8 +217,11 @@ func (s3a *S3ApiServer) PutObjectPartHandler(w http.ResponseWriter, r *http.Requ |
|
|
|
uploadUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s", |
|
|
|
s3a.option.Filer.ToHttpAddress(), s3a.genUploadsFolder(bucket), uploadID, partID, bucket) |
|
|
|
|
|
|
|
etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader) |
|
|
|
if partID == 1 && r.Header.Get("Content-Type") == "" { |
|
|
|
dataReader = mimeDetect(r, dataReader) |
|
|
|
} |
|
|
|
|
|
|
|
etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader) |
|
|
|
if errCode != s3err.ErrNone { |
|
|
|
s3err.WriteErrorResponse(w, errCode, r) |
|
|
|
return |
|
|
|