| 79 | }; |
| 80 | |
| 81 | export const validateCSSFilename = (file: string, metadata: IDResponse) => { |
| 82 | const [filename, extension] = file.split('.'); |
| 83 | if (!extension || extension !== 'css') { |
| 84 | throw new StatusError(400, 'Bad Request. Invalid file extension.'); |
| 85 | } |
| 86 | |
| 87 | // Accept index.css |
| 88 | if (filename === 'index') { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | const { weights, styles, subsets } = metadata; |
| 93 | |
| 94 | // Accept weight.css |
| 95 | if (weights.includes(Number(filename))) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | // Accept weight-italic.css |
| 100 | let weight: string | undefined; |
| 101 | let style: string | undefined; |
| 102 | let subset: string | undefined; |
| 103 | [weight, style] = filename.split('-'); |
| 104 | if ( |
| 105 | weights.includes(Number(weight)) && |
| 106 | style === 'italic' && |
| 107 | styles.includes(style) |
| 108 | ) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | // Accept subset-weight.css |
| 113 | const subsetWeight = filename.split('-'); |
| 114 | weight = subsetWeight.pop(); |
| 115 | subset = subsetWeight.join('-'); |
| 116 | if (subsets.includes(subset) && weights.includes(Number(weight))) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // Accept subset-weight-style.css |
| 121 | const subsetWeightStyle = filename.split('-'); |
| 122 | style = subsetWeightStyle.pop(); |
| 123 | weight = subsetWeightStyle.pop(); |
| 124 | subset = subsetWeightStyle.join('-'); |
| 125 | if ( |
| 126 | style && |
| 127 | subsets.includes(subset) && |
| 128 | weights.includes(Number(weight)) && |
| 129 | styles.includes(style) |
| 130 | ) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | throw new StatusError(404, 'Not Found. Invalid filename.'); |
| 135 | }; |
| 136 | |
| 137 | export const validateVCSSFilename = ( |
| 138 | file: string, |