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