diff --git a/weed/s3api/filer_multipart.go b/weed/s3api/filer_multipart.go index fca1a0aa4..a7d4b95a7 100644 --- a/weed/s3api/filer_multipart.go +++ b/weed/s3api/filer_multipart.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 } }) diff --git a/weed/s3api/s3api_object_handlers.go b/weed/s3api/s3api_object_handlers.go index 0b7e8043f..16f5f34f3 100644 --- a/weed/s3api/s3api_object_handlers.go +++ b/weed/s3api/s3api_object_handlers.go @@ -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 { diff --git a/weed/s3api/s3api_object_multipart_handlers.go b/weed/s3api/s3api_object_multipart_handlers.go index ab72b8437..486161dfb 100644 --- a/weed/s3api/s3api_object_multipart_handlers.go +++ b/weed/s3api/s3api_object_multipart_handlers.go @@ -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