For path rules, see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html
( root string, threshold int, force bool, paths ...string, )
| 157 | |
| 158 | // For path rules, see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html |
| 159 | func (c *cloudFrontClient) normalizeInvalidationPaths( |
| 160 | root string, |
| 161 | threshold int, |
| 162 | force bool, |
| 163 | paths ...string, |
| 164 | ) []string { |
| 165 | if !strings.HasPrefix(root, "/") { |
| 166 | root = "/" + root |
| 167 | } |
| 168 | |
| 169 | matchAll := path.Join(root, "*") |
| 170 | clearAll := []string{matchAll} |
| 171 | |
| 172 | if force { |
| 173 | return clearAll |
| 174 | } |
| 175 | |
| 176 | var normalized []string |
| 177 | var maxlevels int |
| 178 | |
| 179 | for _, p := range paths { |
| 180 | p = pathClean(p) |
| 181 | if !strings.HasPrefix(p, "/") { |
| 182 | p = "/" + p |
| 183 | } |
| 184 | levels := strings.Count(p, "/") |
| 185 | if levels > maxlevels { |
| 186 | maxlevels = levels |
| 187 | } |
| 188 | |
| 189 | if strings.HasSuffix(p, "index.html") { |
| 190 | dir := path.Dir(p) |
| 191 | if !strings.HasSuffix(dir, "/") { |
| 192 | dir += "/" |
| 193 | } |
| 194 | normalized = append(normalized, dir) |
| 195 | } else { |
| 196 | normalized = append(normalized, p) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | normalized = uniqueStrings(normalized) |
| 201 | sort.Strings(normalized) |
| 202 | |
| 203 | if len(normalized) > threshold { |
| 204 | if len(normalized) > threshold { |
| 205 | for k := maxlevels; k > 0; k-- { |
| 206 | for i, p := range normalized { |
| 207 | if strings.Count(p, "/") > k { |
| 208 | parts := strings.Split(strings.TrimPrefix(path.Dir(p), "/"), "/") |
| 209 | if len(parts) > 1 { |
| 210 | parts = parts[:len(parts)-1] |
| 211 | } |
| 212 | normalized[i] = "/" + path.Join(parts...) + "/*" |
| 213 | } |
| 214 | } |
| 215 | normalized = uniqueStrings(normalized) |
| 216 | if len(normalized) <= threshold { |