Fix path to sprites construction (#4860) * Fix path to sprites construction * Clean-up destination sprite paths
Signed| @@ -71,9 +71,16 @@ export function importRisuSprites(directories, data) { | ||
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | // Create sprites folder if it doesn't exist |
| 74 | 74 | const spritesPath = path.joingetSpritesPath(directories.characters, name, false); |
| 75 | + | |
| 76 | + // Invalid sprites path | |
| 77 | + if (!spritesPath) { | |
| 78 | + return; | |
| 79 | + } | |
| 80 | + | |
| 81 | + // Create sprites folder if it doesn't exist | |
| 75 | 82 | if (!fs.existsSync(spritesPath)) { |
| 76 | 83 | fs.mkdirSync(spritesPath, { recursive: true }); |
| 77 | 84 | } |
| 78 | 85 | |
| 79 | 86 | // Path to sprites is not a directory. This should never happen. |
| @@ -94,7 +101,7 @@ export function importRisuSprites(directories, data) { | ||
| 94 | 101 | } |
| 95 | 102 | |
| 96 | 103 | const filename = label + '.png'; |
| 97 | 104 | const pathToFile = path.join(spritesPath, sanitize(filename)); |
| 98 | 105 | writeFileAtomicSync(pathToFile, fileBase64, { encoding: 'base64' }); |
| 99 | 106 | } |
| 100 | 107 | |
| @@ -145,7 +152,8 @@ router.get('/get', function (request, response) { | ||
| 145 | 152 | |
| 146 | 153 | router.post('/delete', async (request, response) => { |
| 147 | 154 | const label = request.body.label; |
| 148 | 155 | const name = String(request.body.name); |
| 156 | + const isSubfolder = name.includes('/'); | |
| 149 | 157 | const spriteName = request.body.spriteName || label; |
| 150 | 158 | |
| 151 | 159 | if (!spriteName || !name) { |
| @@ -153,10 +161,10 @@ router.post('/delete', async (request, response) => { | ||
| 153 | 161 | } |
| 154 | 162 | |
| 155 | 163 | try { |
| 156 | 164 | const spritesPath = path.joingetSpritesPath(request.user.directories.characters, name, isSubfolder); |
| 157 | 165 | |
| 158 | 166 | // No sprites folder exists, or not a directory |
| 159 | 167 | if (!spritesPath || !fs.existsSync(spritesPath) || !fs.statSync(spritesPath).isDirectory()) { |
| 160 | 168 | return response.sendStatus(404); |
| 161 | 169 | } |
| 162 | 170 | |
| @@ -178,18 +186,24 @@ router.post('/delete', async (request, response) => { | ||
| 178 | 186 | |
| 179 | 187 | router.post('/upload-zip', async (request, response) => { |
| 180 | 188 | const file = request.file; |
| 181 | 189 | const name = String(request.body.name); |
| 190 | + const isSubfolder = name.includes('/'); | |
| 182 | 191 | |
| 183 | 192 | if (!file || !name) { |
| 184 | 193 | return response.sendStatus(400); |
| 185 | 194 | } |
| 186 | 195 | |
| 187 | 196 | try { |
| 188 | 197 | const spritesPath = path.joingetSpritesPath(request.user.directories.characters, name, isSubfolder); |
| 198 | + | |
| 199 | + // Invalid sprites path | |
| 200 | + if (!spritesPath) { | |
| 201 | + return response.sendStatus(400); | |
| 202 | + } | |
| 189 | 203 | |
| 190 | 204 | // Create sprites folder if it doesn't exist |
| 191 | 205 | if (!fs.existsSync(spritesPath)) { |
| 192 | 206 | fs.mkdirSync(spritesPath, { recursive: true }); |
| 193 | 207 | } |
| 194 | 208 | |
| 195 | 209 | // Path to sprites is not a directory. This should never happen. |
| @@ -210,7 +224,7 @@ router.post('/upload-zip', async (request, response) => { | ||
| 210 | 224 | } |
| 211 | 225 | |
| 212 | 226 | // Write sprite buffer to disk |
| 213 | 227 | const pathToSprite = path.join(spritesPath, sanitize(filename)); |
| 214 | 228 | writeFileAtomicSync(pathToSprite, buffer); |
| 215 | 229 | } |
| 216 | 230 | |
| @@ -226,7 +240,8 @@ router.post('/upload-zip', async (request, response) => { | ||
| 226 | 240 | router.post('/upload', async (request, response) => { |
| 227 | 241 | const file = request.file; |
| 228 | 242 | const label = request.body.label; |
| 229 | 243 | const name = String(request.body.name); |
| 244 | + const isSubfolder = name.includes('/'); | |
| 230 | 245 | const spriteName = request.body.spriteName || label; |
| 231 | 246 | |
| 232 | 247 | if (!file || !label || !name) { |
| @@ -234,11 +249,16 @@ router.post('/upload', async (request, response) => { | ||
| 234 | 249 | } |
| 235 | 250 | |
| 236 | 251 | try { |
| 237 | 252 | const spritesPath = path.joingetSpritesPath(request.user.directories.characters, name, isSubfolder); |
| 253 | + | |
| 254 | + // Invalid sprites path | |
| 255 | + if (!spritesPath) { | |
| 256 | + return response.sendStatus(400); | |
| 257 | + } | |
| 238 | 258 | |
| 239 | 259 | // Create sprites folder if it doesn't exist |
| 240 | 260 | if (!fs.existsSync(spritesPath)) { |
| 241 | 261 | fs.mkdirSync(spritesPath, { recursive: true }); |
| 242 | 262 | } |
| 243 | 263 | |
| 244 | 264 | // Path to sprites is not a directory. This should never happen. |
| @@ -257,7 +277,7 @@ router.post('/upload', async (request, response) => { | ||
| 257 | 277 | |
| 258 | 278 | const filename = spriteName + path.parse(file.originalname).ext; |
| 259 | 279 | const spritePath = path.join(file.destination, file.filename); |
| 260 | 280 | const pathToFile = path.join(spritesPath, sanitize(filename)); |
| 261 | 281 | // Copy uploaded file to sprites folder |
| 262 | 282 | fs.cpSync(spritePath, pathToFile); |
| 263 | 283 | // Remove uploaded file |