| 1133 | 1133 | return response.sendStatus(200); |
| 1134 | 1134 | } catch (err) { |
| 1135 | 1135 | console.error('An error occurred, character edit invalidated.', err); |
| 1136 | + return response.sendStatus(500); |
| 1136 | 1137 | } |
| 1137 | 1138 | }); |
| 1138 | 1139 | |
| 1140 | +router.post('/edit-avatar', validateAvatarUrlMiddleware, async function (request, response) { |
| 1141 | + try { |
| 1142 | + if (!request.file) { |
| 1143 | + return response.status(400).send('Error: no file uploaded'); |
| 1144 | + } |
| 1145 | + |
| 1146 | + if (!request.body || !request.body.avatar_url) { |
| 1147 | + return response.status(400).send('Error: no avatar_url in request body'); |
| 1148 | + } |
| 1149 | + |
| 1150 | + const uploadPath = path.join(request.file.destination, request.file.filename); |
| 1151 | + if (!fs.existsSync(uploadPath)) { |
| 1152 | + return response.status(400).send('Error: uploaded file does not exist'); |
| 1153 | + } |
| 1154 | + const characterPath = path.join(request.user.directories.characters, request.body.avatar_url); |
| 1155 | + if (!fs.existsSync(characterPath)) { |
| 1156 | + return response.status(400).send('Error: character file does not exist'); |
| 1157 | + } |
| 1158 | + const data = await readCharacterData(characterPath); |
| 1159 | + if (!data) { |
| 1160 | + return response.status(400).send('Error: failed to read character data'); |
| 1161 | + } |
| 1162 | + |
| 1163 | + const crop = tryParse(request.query.crop); |
| 1164 | + const fileName = request.body.avatar_url.replace('.png', ''); |
| 1165 | + await writeCharacterData(uploadPath, data, fileName, request, crop); |
| 1166 | + |
| 1167 | + // Remove uploaded temp file |
| 1168 | + fs.unlinkSync(uploadPath); |
| 1169 | + |
| 1170 | + // Reset images caches |
| 1171 | + cacheBuster.bust(request, response); |
| 1172 | + invalidateThumbnail(request.user.directories, 'avatar', request.body.avatar_url); |
| 1173 | + |
| 1174 | + return response.sendStatus(200); |
| 1175 | + } catch (err) { |
| 1176 | + console.error('An error occurred while editing avatar', err); |
| 1177 | + return response.sendStatus(500); |
| 1178 | + } |
| 1179 | +}); |
| 1139 | 1180 | |
| 1140 | 1181 | /** |
| 1141 | 1182 | * Handle a POST request to edit a character attribute. |