UNPKG

dimensions-ai

Version:

A generalized AI Competition framework that allows you to create any competition you want in any language you want with no hassle.

114 lines 5.13 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import * as error from '../error'; import formidable from 'formidable'; import extract from 'extract-zip'; import { existsSync } from 'fs'; import path from 'path'; import { BOT_DIR } from '..'; import { genID } from '../../utils'; import { removeDirectory } from '../../utils/System'; /** * Returns path to unzipped bot contents and the main file. If user provided, will only handle upload if user * matches playerID given */ export const handleBotUpload = (req, user) => { return new Promise((resolve, reject) => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - this function is callable const form = formidable({ multiples: true }); try { form.parse(req, (err, fields, files) => __awaiter(void 0, void 0, void 0, function* () { if (err) { throw err; } if (files.files === undefined) throw new error.BadRequest('No file(s) provided'); if (fields.paths === undefined) throw new error.BadRequest('No file path(s) provided'); if (!files.files.length) { files.files = [files.files]; } fields.paths = JSON.parse(fields.paths); fields.names = JSON.parse(fields.names); fields.playerIDs = JSON.parse(fields.playerIDs); if (!fields.paths.length) throw new error.BadRequest('No file path(s) provided'); if (fields.paths.length != files.files.length) throw new error.BadRequest('Paths and File arrays mismatch'); const uploads = files.files; const paths = fields.paths; let names = fields.names; const playerIDs = fields.playerIDs; if (!names) names = []; const uploadProcessPromises = []; for (let i = 0; i < uploads.length; i++) { const upload = uploads[i]; const pathToFile = paths[i]; if (pathToFile.indexOf('/') !== -1 || pathToFile.indexOf('\\') !== -1) { reject(new error.BadRequest('Path for file/directory cannot have / or \\ in them. File/directory must be in root directory after unzipping file')); return; } const botName = names[i]; const playerID = playerIDs[i]; if (user) { // if differrent playerID and isn't admin, throw insufficient permissions if (user.playerID !== playerID && !req.data.dimension.databasePlugin.isAdmin(user)) { reject(new error.Unauthorized('Insufficient permissions to upload bot for this player ID')); return; } } uploadProcessPromises.push(processUpload(upload, pathToFile, botName, playerID)); } Promise.all(uploadProcessPromises).then(resolve).catch(reject); })); } catch (err) { reject(err); } }); }; const processUpload = (file, pathToFile, botName, playerID) => __awaiter(void 0, void 0, void 0, function* () { // generate a 18 char length nano ID to store this bot const id = genID(18); const botdir = BOT_DIR + '/bot-' + playerID + '-' + id; // extract first try { yield extract(file.path, { dir: botdir, }); } catch (err) { // clean up removeDirectory(botdir); throw new error.InternalServerError(err); } const pathToBotFile = path.join(botdir, pathToFile); const name = botName; // check if file exists if (!existsSync(pathToBotFile)) { // remove folder if doesn't exist removeDirectory(botdir); throw new error.BadRequest(`Extracted zip file to bot-${playerID}-${id} but path to file ${pathToFile} does not exist in the extracted directory`); } else { return { name: name, file: pathToBotFile, playerID: playerID, botdir: botdir, originalFile: file.path, }; } }); //# sourceMappingURL=index.js.map