2021-02-22 17:27:13 +05:30
|
|
|
package upload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-07-23 23:45:48 +05:30
|
|
|
"io"
|
2021-02-22 17:27:13 +05:30
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/secret"
|
2022-05-07 20:08:51 +05:30
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/upload/destination"
|
2022-07-23 23:45:48 +05:30
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/upload/exif"
|
2021-02-22 17:27:13 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type SavedFileTracker struct {
|
|
|
|
Request *http.Request
|
|
|
|
rewrittenFields map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SavedFileTracker) Track(fieldName string, localPath string) {
|
|
|
|
if s.rewrittenFields == nil {
|
|
|
|
s.rewrittenFields = make(map[string]string)
|
|
|
|
}
|
|
|
|
s.rewrittenFields[fieldName] = localPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SavedFileTracker) Count() int {
|
|
|
|
return len(s.rewrittenFields)
|
|
|
|
}
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
func (s *SavedFileTracker) ProcessFile(_ context.Context, fieldName string, file *destination.FileHandler, _ *multipart.Writer) error {
|
2022-01-26 12:08:38 +05:30
|
|
|
if _, ok := s.rewrittenFields[fieldName]; ok {
|
|
|
|
return fmt.Errorf("the %v field has already been processed", fieldName)
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
s.Track(fieldName, file.LocalPath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SavedFileTracker) ProcessField(_ context.Context, _ string, _ *multipart.Writer) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SavedFileTracker) Finalize(_ context.Context) error {
|
|
|
|
if s.rewrittenFields == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
claims := MultipartClaims{RewrittenFields: s.rewrittenFields, RegisteredClaims: secret.DefaultClaims}
|
2021-02-22 17:27:13 +05:30
|
|
|
tokenString, err := secret.JWTTokenString(claims)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("savedFileTracker.Finalize: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Request.Header.Set(RewrittenFieldsHeader, tokenString)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
func (s *SavedFileTracker) Name() string { return "accelerate" }
|
|
|
|
|
|
|
|
func (*SavedFileTracker) TransformContents(ctx context.Context, filename string, r io.Reader) (io.ReadCloser, error) {
|
|
|
|
if imageType := exif.FileTypeFromSuffix(filename); imageType != exif.TypeUnknown {
|
|
|
|
return handleExifUpload(ctx, r, filename, imageType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return io.NopCloser(r), nil
|
2021-02-22 17:27:13 +05:30
|
|
|
}
|