// Extract form fields and file data from a HTTP POST Policy
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) {
	fileName = ""
	formValues = make(http.Header)
	for k, v := range form.Value {
		formValues[http.CanonicalHeaderKey(k)] = v
	}
	if err = validateFormFieldSize(formValues); err != nil {
		return nil, "", 0, nil, err
	}
	if len(form.File) == 0 {
		var b = &bytes.Buffer{}
		for _, v := range formValues["File"] {
			b.WriteString(v)
		}
		fileSize = int64(b.Len())
		filePart = io.NopCloser(b)
		return filePart, fileName, fileSize, formValues, nil
	}
	for k, v := range form.File {
		canonicalFormName := http.CanonicalHeaderKey(k)
		if canonicalFormName == "File" {
			if len(v) == 0 {
				return nil, "", 0, nil, errors.New("Invalid arguments specified")
			}
			fileHeader := v[0]
			fileName = fileHeader.Filename
			filePart, err = fileHeader.Open()
			if err != nil {
				return nil, "", 0, nil, err
			}
			fileSize, err = filePart.(io.Seeker).Seek(0, 2)
			if err != nil {
				return nil, "", 0, nil, err
			}
			_, err = filePart.(io.Seeker).Seek(0, 0)
			if err != nil {
				return nil, "", 0, nil, err
			}
			break
		}
	}
	return filePart, fileName, fileSize, formValues, nil
}
func extractPostPolicyFormValues ( form * multipart . Form ) ( filePart io . ReadCloser , fileName string , fileSize int64 , formValues http . Header , err error ) { fileName = " " formValues = make ( http . Header ) for k , v := range form . Value { formValues [ http . CanonicalHeaderKey ( k ) ] = v } if err = validateFormFieldSize ( formValues ) ; err != nil { return nil , " " , 0 , nil , err } if len ( form . File ) == 0 { var b = & bytes . Buffer { } for _ , v := range formValues [ " " ] { b . WriteString ( v ) } fileSize = int64 ( b . Len ( ) ) filePart = io . NopCloser ( b ) return filePart , fileName , fileSize , formValues , nil } for k , v := range form . File { canonicalFormName := http . CanonicalHeaderKey ( k ) if canonicalFormName == " " { if len ( v ) == 0 { return nil , " " , 0 , nil , errors . New ( " " ) } fileHeader := v [ 0 ] fileName = fileHeader . Filename filePart , err = fileHeader . Open ( ) if err != nil { return nil , " " , 0 , nil , err } fileSize , err = filePart . ( io . Seeker ) . Seek ( 0 , 2 ) if err != nil { return nil , " " , 0 , nil , err } _ , err = filePart . ( io . Seeker ) . Seek ( 0 , 0 ) if err != nil { return nil , " " , 0 , nil , err } break } } return filePart , fileName , fileSize , formValues , nil }
Extract form fields and file data from a HTTP POST Policy
Extract form fields and file data from a HTTP POST Policy
// / HTML Form values // Canonicalize the form values into http.Header. // Validate form values. // this means that filename="" was not specified for file key and Go has // an ugly way of handling this situation. Refer here // https://golang.org/src/mime/multipart/formdata.go#L61 // Iterator until we find a valid File field and break // Fetch fileHeader which has the uploaded file information // Set filename // Open the uploaded part // Compute file size // Reset Seek to the beginning // File found and ready for reading