|
| 1 | +package tus |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/url" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/ARM-software/golang-utils/utils/collection" |
| 10 | + "github.com/ARM-software/golang-utils/utils/commonerrors" |
| 11 | + "github.com/ARM-software/golang-utils/utils/encoding/base64" |
| 12 | + "github.com/ARM-software/golang-utils/utils/field" |
| 13 | + "github.com/ARM-software/golang-utils/utils/hashing" |
| 14 | + "github.com/ARM-software/golang-utils/utils/reflection" |
| 15 | +) |
| 16 | + |
| 17 | +const KeyTUSMetadata = "filename" |
| 18 | + |
| 19 | +// ParseTUSHash parses the checksum header value and tries to determine the different elements it contains. |
| 20 | +// See https://tus.io/protocols/resumable-upload#upload-checksum |
| 21 | +func ParseTUSHash(checksum string) (hashAlgo, hash string, err error) { |
| 22 | + if reflection.IsEmpty(checksum) { |
| 23 | + err = commonerrors.UndefinedVariable("checksum") |
| 24 | + return |
| 25 | + } |
| 26 | + match := regexp.MustCompile(`^\s*([a-zA-Z0-9-_]+)\s+([-A-Za-z0-9+/]*={0,3})$`).FindStringSubmatch(checksum) |
| 27 | + if match == nil || len(match) != 3 { |
| 28 | + err = commonerrors.Newf(commonerrors.ErrInvalid, "invalid checksum format") |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + hashAlgo, err = hashing.DetermineHashingAlgorithmCanonicalReference(match[1]) |
| 33 | + if err != nil { |
| 34 | + err = commonerrors.WrapError(commonerrors.ErrUnsupported, err, "hashing algorithm is not supported") |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + h := strings.TrimSpace(match[2]) |
| 39 | + if !base64.IsEncoded(h) { |
| 40 | + err = commonerrors.Newf(commonerrors.ErrInvalid, "checksum is not base64 encoded") |
| 41 | + return |
| 42 | + } |
| 43 | + hash, err = base64.DecodeString(context.Background(), h) |
| 44 | + if err != nil { |
| 45 | + err = commonerrors.WrapError(commonerrors.ErrMarshalling, err, "failed decoding checksum") |
| 46 | + } |
| 47 | + return |
| 48 | +} |
| 49 | + |
| 50 | +// ParseTUSConcatHeader parses the `Concat` header value https://tus.io/protocols/resumable-upload#upload-concat |
| 51 | +func ParseTUSConcatHeader(concat string) (isPartial bool, partials []*url.URL, err error) { |
| 52 | + header := strings.TrimSpace(concat) |
| 53 | + if reflection.IsEmpty(header) { |
| 54 | + err = commonerrors.UndefinedVariable("concat header") |
| 55 | + return |
| 56 | + } |
| 57 | + if strings.EqualFold(header, "partial") { |
| 58 | + isPartial = true |
| 59 | + return |
| 60 | + } |
| 61 | + h := strings.TrimPrefix(header, "final;") |
| 62 | + if header == h { |
| 63 | + err = commonerrors.New(commonerrors.ErrInvalid, "invalid header value") |
| 64 | + return |
| 65 | + } |
| 66 | + partials, err = collection.MapWithError[string, *url.URL](collection.ParseListWithCleanup(h, " "), url.Parse) |
| 67 | + if err != nil { |
| 68 | + err = commonerrors.WrapError(commonerrors.ErrInvalid, commonerrors.New(commonerrors.ErrMarshalling, "invalid partial url value"), "invalid header value") |
| 69 | + } |
| 70 | + if len(partials) == 0 { |
| 71 | + err = commonerrors.New(commonerrors.ErrInvalid, "no partial url found") |
| 72 | + } |
| 73 | + return |
| 74 | +} |
| 75 | + |
| 76 | +// ParseTUSMetadataHeader parses the `metadata` header value https://tus.io/protocols/resumable-upload#upload-metadata |
| 77 | +func ParseTUSMetadataHeader(header string) (filename *string, elements map[string]any, err error) { |
| 78 | + h := strings.TrimSpace(header) |
| 79 | + if reflection.IsEmpty(h) { |
| 80 | + err = commonerrors.UndefinedVariable("metadata header") |
| 81 | + return |
| 82 | + } |
| 83 | + e := collection.ParseCommaSeparatedList(h) |
| 84 | + if len(e) == 0 { |
| 85 | + err = commonerrors.UndefinedVariable("metadata header") |
| 86 | + return |
| 87 | + } |
| 88 | + elements = make(map[string]any, len(e)/2) |
| 89 | + for i := range e { |
| 90 | + subElem := collection.ParseListWithCleanup(e[i], " ") |
| 91 | + switch len(subElem) { |
| 92 | + case 1: |
| 93 | + elements[subElem[0]] = true |
| 94 | + case 2: |
| 95 | + key := subElem[0] |
| 96 | + value := subElem[1] |
| 97 | + _, has := elements[key] |
| 98 | + if has { |
| 99 | + err = commonerrors.WrapError(commonerrors.ErrInvalid, commonerrors.Newf(commonerrors.ErrInvalid, "duplicated key [%v]", key), "invalid metadata element") |
| 100 | + return |
| 101 | + } |
| 102 | + if !base64.IsEncoded(value) { |
| 103 | + err = commonerrors.WrapError(commonerrors.ErrInvalid, commonerrors.New(commonerrors.ErrMarshalling, "value is not base64 encoded"), "invalid metadata element") |
| 104 | + return |
| 105 | + } |
| 106 | + v, subErr := base64.DecodeString(context.Background(), value) |
| 107 | + if subErr != nil { |
| 108 | + err = commonerrors.WrapError(commonerrors.ErrInvalid, commonerrors.New(commonerrors.ErrMarshalling, "value is not base64 encoded"), "invalid metadata element") |
| 109 | + return |
| 110 | + } |
| 111 | + elements[key] = v |
| 112 | + if strings.EqualFold(key, KeyTUSMetadata) { |
| 113 | + filename = field.ToOptionalString(v) |
| 114 | + } |
| 115 | + |
| 116 | + default: |
| 117 | + err = commonerrors.New(commonerrors.ErrInvalid, "invalid metadata header element") |
| 118 | + return |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return |
| 123 | +} |
0 commit comments