Browse Source

Adapt S3 POST ContentType (#5275)

pull/5278/head
sxlehua 11 months ago
committed by GitHub
parent
commit
0b49c16361
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 29
      weed/s3api/s3api_object_handlers_postpolicy.go

29
weed/s3api/s3api_object_handlers_postpolicy.go

@ -39,7 +39,7 @@ func (s3a *S3ApiServer) PostPolicyBucketHandler(w http.ResponseWriter, r *http.R
} }
defer form.RemoveAll() defer form.RemoveAll()
fileBody, fileName, fileSize, formValues, err := extractPostPolicyFormValues(form)
fileBody, fileName, fileContentType, fileSize, formValues, err := extractPostPolicyFormValues(form)
if err != nil { if err != nil {
s3err.WriteErrorResponse(w, r, s3err.ErrMalformedPOSTRequest) s3err.WriteErrorResponse(w, r, s3err.ErrMalformedPOSTRequest)
return return
@ -115,6 +115,14 @@ func (s3a *S3ApiServer) PostPolicyBucketHandler(w http.ResponseWriter, r *http.R
uploadUrl := fmt.Sprintf("http://%s%s/%s%s", s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, bucket, urlEscapeObject(object)) uploadUrl := fmt.Sprintf("http://%s%s/%s%s", s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, bucket, urlEscapeObject(object))
// Get ContentType from post formData
// Otherwise from formFile ContentType
contentType := formValues.Get("Content-Type")
if contentType == "" {
contentType = fileContentType
}
r.Header.Set("Content-Type", contentType)
etag, errCode := s3a.putToFiler(r, uploadUrl, fileBody, "", bucket) etag, errCode := s3a.putToFiler(r, uploadUrl, fileBody, "", bucket)
if errCode != s3err.ErrNone { if errCode != s3err.ErrNone {
@ -152,9 +160,10 @@ func (s3a *S3ApiServer) PostPolicyBucketHandler(w http.ResponseWriter, r *http.R
} }
// Extract form fields and file data from a HTTP POST Policy // Extract form fields and file data from a HTTP POST Policy
func extractPostPolicyFormValues(form *multipart.Form) (filePart io.ReadCloser, fileName string, fileSize int64, formValues http.Header, err error) {
func extractPostPolicyFormValues(form *multipart.Form) (filePart io.ReadCloser, fileName, fileContentType string, fileSize int64, formValues http.Header, err error) {
// / HTML Form values // / HTML Form values
fileName = "" fileName = ""
fileContentType = ""
// Canonicalize the form values into http.Header. // Canonicalize the form values into http.Header.
formValues = make(http.Header) formValues = make(http.Header)
@ -164,7 +173,7 @@ func extractPostPolicyFormValues(form *multipart.Form) (filePart io.ReadCloser,
// Validate form values. // Validate form values.
if err = validateFormFieldSize(formValues); err != nil { if err = validateFormFieldSize(formValues); err != nil {
return nil, "", 0, nil, err
return nil, "", "", 0, nil, err
} }
// this means that filename="" was not specified for file key and Go has // this means that filename="" was not specified for file key and Go has
@ -177,7 +186,7 @@ func extractPostPolicyFormValues(form *multipart.Form) (filePart io.ReadCloser,
} }
fileSize = int64(b.Len()) fileSize = int64(b.Len())
filePart = io.NopCloser(b) filePart = io.NopCloser(b)
return filePart, fileName, fileSize, formValues, nil
return filePart, fileName, fileContentType, fileSize, formValues, nil
} }
// Iterator until we find a valid File field and break // Iterator until we find a valid File field and break
@ -185,32 +194,34 @@ func extractPostPolicyFormValues(form *multipart.Form) (filePart io.ReadCloser,
canonicalFormName := http.CanonicalHeaderKey(k) canonicalFormName := http.CanonicalHeaderKey(k)
if canonicalFormName == "File" { if canonicalFormName == "File" {
if len(v) == 0 { if len(v) == 0 {
return nil, "", 0, nil, errors.New("Invalid arguments specified")
return nil, "", "", 0, nil, errors.New("Invalid arguments specified")
} }
// Fetch fileHeader which has the uploaded file information // Fetch fileHeader which has the uploaded file information
fileHeader := v[0] fileHeader := v[0]
// Set filename // Set filename
fileName = fileHeader.Filename fileName = fileHeader.Filename
// Set contentType
fileContentType = fileHeader.Header.Get("Content-Type")
// Open the uploaded part // Open the uploaded part
filePart, err = fileHeader.Open() filePart, err = fileHeader.Open()
if err != nil { if err != nil {
return nil, "", 0, nil, err
return nil, "", "", 0, nil, err
} }
// Compute file size // Compute file size
fileSize, err = filePart.(io.Seeker).Seek(0, 2) fileSize, err = filePart.(io.Seeker).Seek(0, 2)
if err != nil { if err != nil {
return nil, "", 0, nil, err
return nil, "", "", 0, nil, err
} }
// Reset Seek to the beginning // Reset Seek to the beginning
_, err = filePart.(io.Seeker).Seek(0, 0) _, err = filePart.(io.Seeker).Seek(0, 0)
if err != nil { if err != nil {
return nil, "", 0, nil, err
return nil, "", "", 0, nil, err
} }
// File found and ready for reading // File found and ready for reading
break break
} }
} }
return filePart, fileName, fileSize, formValues, nil
return filePart, fileName, fileContentType, fileSize, formValues, nil
} }
// Validate form field size for s3 specification requirement. // Validate form field size for s3 specification requirement.

Loading…
Cancel
Save