|
| 1 | +/* |
| 2 | + * Copyright 2025 The Dragonfly Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package storage |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + |
| 24 | + "d7y.io/dragonfly/v2/pkg/idgen" |
| 25 | +) |
| 26 | + |
| 27 | +// defaultFilteredQueryParams is the default filtered query params to generate the task id. |
| 28 | +var defaultFilteredQueryParams []string |
| 29 | + |
| 30 | +var ( |
| 31 | + // s3FilteredQueryParams is the default filtered query params with s3 protocol to generate the task id. |
| 32 | + s3FilteredQueryParams = []string{ |
| 33 | + "X-Amz-Algorithm", |
| 34 | + "X-Amz-Credential", |
| 35 | + "X-Amz-Date", |
| 36 | + "X-Amz-Expires", |
| 37 | + "X-Amz-SignedHeaders", |
| 38 | + "X-Amz-Signature", |
| 39 | + "X-Amz-Security-Token", |
| 40 | + "X-Amz-User-Agent", |
| 41 | + } |
| 42 | + |
| 43 | + // gcsFilteredQueryParams is the filtered query params with gcs protocol to generate the task id. |
| 44 | + gcsFilteredQueryParams = []string{ |
| 45 | + "X-Goog-Algorithm", |
| 46 | + "X-Goog-Credential", |
| 47 | + "X-Goog-Date", |
| 48 | + "X-Goog-Expires", |
| 49 | + "X-Goog-SignedHeaders", |
| 50 | + "X-Goog-Signature", |
| 51 | + } |
| 52 | + |
| 53 | + // ossFilteredQueryParams is the default filtered query params with oss protocol to generate the task id. |
| 54 | + ossFilteredQueryParams = []string{ |
| 55 | + "OSSAccessKeyId", |
| 56 | + "Expires", |
| 57 | + "Signature", |
| 58 | + "SecurityToken", |
| 59 | + } |
| 60 | + |
| 61 | + // obsFilteredQueryParams is the default filtered query params with obs protocol to generate the task id. |
| 62 | + obsFilteredQueryParams = []string{ |
| 63 | + "AccessKeyId", |
| 64 | + "Signature", |
| 65 | + "Expires", |
| 66 | + "X-Obs-Date", |
| 67 | + "X-Obs-Security-Token", |
| 68 | + } |
| 69 | + |
| 70 | + // cosFilteredQueryParams is the default filtered query params with cos protocol to generate the task id. |
| 71 | + cosFilteredQueryParams = []string{ |
| 72 | + "q-sign-algorithm", |
| 73 | + "q-ak", |
| 74 | + "q-sign-time", |
| 75 | + "q-key-time", |
| 76 | + "q-header-list", |
| 77 | + "q-url-param-list", |
| 78 | + "q-signature", |
| 79 | + "x-cos-security-token", |
| 80 | + } |
| 81 | + |
| 82 | + // containerdFilteredQueryParams is the default filtered query params with containerd to generate the task id. |
| 83 | + containerdFilteredQueryParams = []string{ |
| 84 | + "ns", |
| 85 | + } |
| 86 | +) |
| 87 | + |
| 88 | +func init() { |
| 89 | + defaultFilteredQueryParams = append(defaultFilteredQueryParams, s3FilteredQueryParams...) |
| 90 | + defaultFilteredQueryParams = append(defaultFilteredQueryParams, gcsFilteredQueryParams...) |
| 91 | + defaultFilteredQueryParams = append(defaultFilteredQueryParams, ossFilteredQueryParams...) |
| 92 | + defaultFilteredQueryParams = append(defaultFilteredQueryParams, obsFilteredQueryParams...) |
| 93 | + defaultFilteredQueryParams = append(defaultFilteredQueryParams, cosFilteredQueryParams...) |
| 94 | + defaultFilteredQueryParams = append(defaultFilteredQueryParams, containerdFilteredQueryParams...) |
| 95 | +} |
| 96 | + |
| 97 | +// statTask is the options for stat task. |
| 98 | +type statTask struct { |
| 99 | + // path is the base path of the storage. |
| 100 | + path string |
| 101 | + // url is the url of the task. |
| 102 | + url string |
| 103 | + // contentLength is the content length of the task.(optional) |
| 104 | + contentLength *uint64 |
| 105 | + // pieceLength is the piece length of the task.(optional) |
| 106 | + pieceLength *uint64 |
| 107 | + // tag is the tag of the task.(optional) |
| 108 | + tag string |
| 109 | + // application is the application of the task.(optional) |
| 110 | + application string |
| 111 | + // filteredQueryParams is the filtered query params of the task.(optional) |
| 112 | + filteredQueryParams []string |
| 113 | +} |
| 114 | + |
| 115 | +type StatTaskOption func(*statTask) |
| 116 | + |
| 117 | +func WithStatTaskContentLength(contentLength *uint64) StatTaskOption { |
| 118 | + return func(task *statTask) { |
| 119 | + task.contentLength = contentLength |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func WithStatTaskPieceLength(pieceLength *uint64) StatTaskOption { |
| 124 | + return func(task *statTask) { |
| 125 | + task.pieceLength = pieceLength |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func WithStatTaskTag(tag string) StatTaskOption { |
| 130 | + return func(task *statTask) { |
| 131 | + task.tag = tag |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func WithStatTaskApplication(application string) StatTaskOption { |
| 136 | + return func(task *statTask) { |
| 137 | + task.application = application |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func WithStatTaskFilteredQueryParams(filteredQueryParams []string) StatTaskOption { |
| 142 | + return func(task *statTask) { |
| 143 | + task.filteredQueryParams = filteredQueryParams |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +// StatTask stats the task by the given parameters. |
| 148 | +func StatTask(path, url string, opts ...StatTaskOption) (os.FileInfo, error) { |
| 149 | + st := &statTask{ |
| 150 | + path: path, |
| 151 | + url: url, |
| 152 | + } |
| 153 | + for _, opt := range opts { |
| 154 | + opt(st) |
| 155 | + } |
| 156 | + |
| 157 | + // Validate and mutate the options for stat task. |
| 158 | + if st.path == "" || st.url == "" { |
| 159 | + return nil, errors.New("path and url are required") |
| 160 | + } |
| 161 | + |
| 162 | + if st.contentLength == nil && st.pieceLength == nil { |
| 163 | + return nil, errors.New("either contentLength or pieceLength must be specified") |
| 164 | + } |
| 165 | + |
| 166 | + if st.pieceLength == nil { |
| 167 | + // Calculate pieceLength from contentLength if not specified. |
| 168 | + pieceLength := CalculatePieceLength(*st.contentLength) |
| 169 | + st.pieceLength = &pieceLength |
| 170 | + } |
| 171 | + |
| 172 | + if len(st.filteredQueryParams) == 0 { |
| 173 | + st.filteredQueryParams = defaultFilteredQueryParams |
| 174 | + } |
| 175 | + |
| 176 | + taskID := idgen.TaskIDV2ByURLBased(st.url, st.pieceLength, st.tag, st.application, st.filteredQueryParams) |
| 177 | + |
| 178 | + // Construct the file path. |
| 179 | + filePath := filepath.Join(path, "content/tasks", taskID[0:3], taskID) |
| 180 | + return os.Stat(filePath) |
| 181 | +} |
0 commit comments