converter-toless-plugin
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -20,4 +20,6 @@ | ||
CompilerPath: string; | ||
CompilerLink?: string; | ||
command: string; | ||
outputT: string; | ||
buildOutputDirectory: boolean; | ||
whitelistInputs: string[]; | ||
@@ -79,3 +81,3 @@ } | ||
/** check this user with google recaptcha */ | ||
reCaptchaCheck: (UserToken: string, ip: string) => [stated: boolean, msg: string]; | ||
reCaptchaCheck: (UserToken: string, ip: string) => any; | ||
@@ -105,2 +107,4 @@ /** Send message on progress page */ | ||
paths: any; | ||
} | ||
} | ||
@@ -1,63 +0,69 @@ | ||
// String.prototype.replaceAll = function ra(string, string2) { | ||
// return this.toLowerCase().split(string.toLowerCase()).join(string2); | ||
// } | ||
"use strict"; | ||
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()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DefaultFilter = void 0; | ||
function replaceAll(strmain, str1, str2) { | ||
return strmain.toLowerCase().split(str1.toLowerCase()).join(str2); | ||
} | ||
export const DefaultFilter = { | ||
exports.DefaultFilter = { | ||
enabled: true, | ||
words: [ | ||
"fuck", "shit", "Swear", "www", "https", "http" | ||
"fuck", "shit", "Swear", "www", "https", "http", "*", ">", '"' | ||
], | ||
replaceBy: "u", | ||
filterWords: true, | ||
filterComas: true, | ||
filterSlashes: true, | ||
filterUsingDecoder: true | ||
filterUsingDecoder: true, | ||
replaceBy: "u", | ||
}; | ||
export default async (string, filter) => { | ||
let _filter = filter || DefaultFilter; | ||
exports.default = (string, filter) => __awaiter(void 0, void 0, void 0, function* () { | ||
let _filter = filter || exports.DefaultFilter; | ||
let _string = string; | ||
_string = (_filter.filterWords) ? await step1(_string, _filter) : _string; | ||
_string = (_filter.filterWords) ? await step2(_string, _filter) : _string; | ||
_string = (_filter.filterWords) ? await step3(_string) : _string; | ||
_string = (_filter.filterWords) ? await step4(_string, _filter) : _string; | ||
_string = (_filter.filterWords) ? yield layer1(_string, _filter) : _string; | ||
_string = (_filter.filterWords) ? yield layer2(_string, _filter) : _string; | ||
_string = (_filter.filterWords) ? yield layer3(_string) : _string; | ||
_string = (_filter.filterWords) ? yield layer4(_string, _filter) : _string; | ||
return _string; | ||
}; | ||
// filter words | ||
function step1(string, filter) { | ||
}); | ||
function layer1(str, filter) { | ||
return new Promise((solve) => { | ||
let _string = string; | ||
let _str = str; | ||
filter.words.forEach((word) => { | ||
_string = replaceAll(_string, word, filter.replaceBy); | ||
_str = replaceAll(_str, word, filter.replaceBy); | ||
}); | ||
solve(_string); | ||
return _string; | ||
solve(_str); | ||
return _str; | ||
}); | ||
} | ||
// filter commas | ||
function step2(string, filter) { | ||
function layer2(str, filter) { | ||
return new Promise((solve) => { | ||
let _string = string.replace(/"/gi, filter.replaceBy); | ||
_string = _string.replace(/'/gi, filter.replaceBy); | ||
solve(_string); | ||
return _string; | ||
let _str = str.replace(/"/gi, filter.replaceBy); | ||
_str = _str.replace(/'/gi, filter.replaceBy); | ||
solve(_str); | ||
return _str; | ||
}); | ||
} | ||
// filter with decoding | ||
function step3(string, filter) { | ||
function layer3(str) { | ||
return new Promise((solve) => { | ||
let _string = encodeURI(string); | ||
solve(_string); | ||
return _string; | ||
let _str = encodeURI(str); | ||
solve(_str); | ||
return _str; | ||
}); | ||
} | ||
// filter slashes | ||
function step4(string, filter) { | ||
function layer4(str, filter) { | ||
return new Promise((solve) => { | ||
let _string = string.replace(/\\/gi, filter.replaceBy); | ||
_string = _string.replace(/\//gi, filter.replaceBy); | ||
solve(_string); | ||
return _string; | ||
let _str = str.replace(/\\/gi, filter.replaceBy); | ||
_str = _str.replace(/\//gi, filter.replaceBy); | ||
solve(_str); | ||
return _str; | ||
}); | ||
} | ||
//# sourceMappingURL=filter.js.map |
@@ -1,43 +0,91 @@ | ||
import * as path from "path"; | ||
import * as fs from "fs"; | ||
export function getProps(name) { | ||
const n = name.split("."); | ||
const n2 = name.split(""); | ||
const type = name.split(".")[n.length - 1]; | ||
const other = n2.slice(0, n2.length - type.length - 1).join(''); | ||
return { type, name: other }; | ||
} | ||
// delete functions | ||
export function deleteFile(filePath) { | ||
fs.unlink(filePath, (err) => { | ||
if (err) { | ||
console.log(err); | ||
"use strict"; | ||
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()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createDir = exports.deleteAllFilesInDirectory = exports.deleteDirectory = exports.deleteFile = exports.deleteFileOrDirectory = exports.NamePro = exports.getNameOf = void 0; | ||
const path_1 = require("path"); | ||
const fs = require("fs"); | ||
const del = require("del"); | ||
const filter_1 = require("./filter"); | ||
const randomstring_1 = require("randomstring"); | ||
function getNameOf(arry, name) { | ||
let indexofar = -1; | ||
arry.forEach((ar, i) => { | ||
if (ar.name === name) { | ||
indexofar = i; | ||
} | ||
}); | ||
return indexofar; | ||
} | ||
export function deleteDirectory(dirPath) { | ||
return new Promise((solve) => { | ||
fs.rmdir(dirPath, { recursive: true }, solve); | ||
exports.getNameOf = getNameOf; | ||
class NamePro { | ||
constructor(string) { | ||
this.all = string; | ||
const n = this.all.split("."); | ||
const n2 = this.all.split(""); | ||
this.type = this.all.split(".")[n.length - 1]; | ||
this.name = n2.slice(0, n2.length - this.type.length - 1).join(''); | ||
} | ||
withType(_type = this.type) { | ||
return `${this.name}.${_type}`; | ||
} | ||
filter(filterOptions) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (filterOptions.enabled) | ||
this.name = yield filter_1.default(this.name, filterOptions); | ||
}); | ||
} | ||
randomize(options) { | ||
this.name = `${this.name}_${randomstring_1.generate(options)}`; | ||
} | ||
} | ||
exports.NamePro = NamePro; | ||
function deleteFileOrDirectory(filePath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield del(filePath, { force: true }); | ||
}); | ||
} | ||
export function deleteAllFilesInDirectory(dir) { | ||
fs.readdir(dir, (err, files) => { | ||
if (err) | ||
throw err; | ||
for (const file of files) { | ||
fs.unlink(path.join(dir, file), err => { | ||
if (err) | ||
throw err; | ||
}); | ||
} | ||
exports.deleteFileOrDirectory = deleteFileOrDirectory; | ||
function deleteFile(filePath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield del(filePath, { force: true }); | ||
}); | ||
} | ||
export function getNameOf(arry, name) { | ||
let indexofar = -1; | ||
arry.forEach((ar, i) => { | ||
if (ar.name === name) { | ||
indexofar = i; | ||
exports.deleteFile = deleteFile; | ||
function deleteDirectory(dirPath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield del(dirPath, { force: true }); | ||
}); | ||
} | ||
exports.deleteDirectory = deleteDirectory; | ||
function deleteAllFilesInDirectory(dir) { | ||
return new Promise((s, r) => { | ||
fs.readdir(dir, (err, files) => __awaiter(this, void 0, void 0, function* () { | ||
if (err) | ||
r(err); | ||
for (const file of files) { | ||
const pathi = path_1.join(dir, file); | ||
deleteFileOrDirectory(pathi); | ||
} | ||
s(); | ||
})); | ||
}); | ||
} | ||
exports.deleteAllFilesInDirectory = deleteAllFilesInDirectory; | ||
function createDir(path) { | ||
return new Promise((s, r) => { | ||
if (!fs.existsSync(path)) { | ||
fs.mkdirSync(path); | ||
} | ||
s(); | ||
}); | ||
return indexofar; | ||
} | ||
exports.createDir = createDir; | ||
//# sourceMappingURL=utils.js.map |
@@ -1,8 +0,8 @@ | ||
import multer from "multer"; | ||
import { getProps } from "../components/utils.js"; | ||
import * as mime from "mime-types"; | ||
// for generating random strings | ||
import { generate as generateRandomString } from "randomstring"; | ||
let MB = 2048; | ||
export default function upload_file(props) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const multer = require("multer"); | ||
const mime_types_1 = require("mime-types"); | ||
const utils_1 = require("../components/utils"); | ||
let MB = 1024 * 1024; | ||
function upload_file(props) { | ||
var storage = multer.diskStorage({ | ||
@@ -13,13 +13,13 @@ destination: (req, file, cb) => { | ||
filename: (req, file, cb) => { | ||
const fileprops = getProps(file.originalname); | ||
cb(null, `${fileprops.name}_F_${generateRandomString(props.randomStringSize)}.${mime.extension(file.mimetype)}`); | ||
const fileprops = new utils_1.NamePro(file.originalname); | ||
fileprops.randomize(props.randomStringSize); | ||
fileprops.name = fileprops.name.toLowerCase(); | ||
cb(null, `${fileprops.name}.${mime_types_1.extension(file.mimetype)}`); | ||
} | ||
}); | ||
return multer({ | ||
storage: storage, | ||
dest: props.dest, | ||
limits: { | ||
fileSize: props.fileSize * MB + 1 * MB | ||
}, ...props.additions | ||
}); | ||
return multer(Object.assign({ storage: storage, dest: props.dest, limits: { | ||
fileSize: props.fileSize * MB + 3 * MB | ||
} }, props.additions)); | ||
} | ||
exports.default = upload_file; | ||
//# sourceMappingURL=multer.config.js.map |
@@ -25,3 +25,2 @@ import { Compiler, converterOptions, Dirs, Router } from "../d/types"; | ||
mainPage(req: Request, res: Response): Promise<void>; | ||
checkFile(req: Request, resOrToken: any): void; | ||
/**upload file */ | ||
@@ -59,4 +58,2 @@ uploadFile(req: Request, res: Response): Promise<void>; | ||
zipTheOutputDirectory(name: string): Promise<unknown>; | ||
/** this function download the file to the server **DEPRICATED** */ | ||
uploadTheFile(file: any, uploadpath: string): Promise<unknown>; | ||
/** this function exicute a programmer with params */ | ||
@@ -63,0 +60,0 @@ execShellCommand(cmd: string, stdcb: Function): Promise<unknown>; |
507
lib/index.js
@@ -1,19 +0,24 @@ | ||
// for running commands | ||
import { exec } from "child_process"; | ||
// for file controling | ||
import * as Path from "path"; | ||
import * as fs from "fs"; | ||
import arp from "app-root-path"; | ||
const _dirname = arp.path; | ||
// for utils | ||
import { getNameOf, getProps, deleteFile, deleteAllFilesInDirectory, deleteDirectory } from "./components/utils.js"; | ||
// for filtering names | ||
import filter, { DefaultFilter } from "./components/filter.js"; | ||
import { join } from 'path'; | ||
import multerConfig from "./config/multer.config.js"; | ||
// for archiving | ||
import archiver from "archiver"; | ||
import { generate } from "randomstring"; | ||
"use strict"; | ||
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()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const child_process_1 = require("child_process"); | ||
const node_fetch_1 = require("node-fetch"); | ||
const fs = require("fs"); | ||
const app_root_path_1 = require("app-root-path"); | ||
const randomstring_1 = require("randomstring"); | ||
const utils_1 = require("./components/utils"); | ||
const filter_1 = require("./components/filter"); | ||
const path_1 = require("path"); | ||
const multer_config_1 = require("./config/multer.config"); | ||
const archiver = require("archiver"); | ||
const aforwait_1 = require("aforwait"); | ||
const defaultProps = { | ||
PORT: 3000, | ||
compilers: [ | ||
@@ -25,2 +30,3 @@ { | ||
command: `-o "#{CompeleteOutputFilePath}" -i "#{CompeleteInputFilePath}"`, | ||
buildOutputDirectory: true, | ||
whitelistInputs: ["Doc", "Docx"] | ||
@@ -31,3 +37,3 @@ }, | ||
logInFile: true, | ||
logfile: Path.join(_dirname, 'logs', 'converterlog.txt'), | ||
logfile: path_1.join(app_root_path_1.path, 'logs', 'converterlog.txt'), | ||
timetoGarbageCleaner: 240, | ||
@@ -37,25 +43,22 @@ deleteallTempFilesOnStart: true, | ||
randomStringSize: 8, | ||
filter: DefaultFilter | ||
filter: filter_1.DefaultFilter | ||
}; | ||
const MB = 2048; | ||
const MB = 1024 * 1024; | ||
const Minute = 60000; | ||
export default class CompilersHandler { | ||
class CompilersHandler { | ||
constructor(props = {}) { | ||
this.getParams({ ...defaultProps, ...props }); | ||
this.getParams(Object.assign(Object.assign({}, defaultProps), props)); | ||
} | ||
/** this function starts the server */ | ||
start() { | ||
this.setInputOutputDir(); | ||
// deleting garbage before start | ||
if (this.deleteallTempFilesOnStart) { | ||
this.deleteGarbage(); | ||
} | ||
//this.setTextConverter() | ||
this.GarbageCleaner(); | ||
this.setup(); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.setInputOutputDir(); | ||
if (this.deleteallTempFilesOnStart) { | ||
yield this.deleteGarbage(); | ||
} | ||
this.GarbageCleaner(); | ||
this.setup(); | ||
}); | ||
} | ||
/** this function waits for a file from the client */ | ||
setup() { | ||
// setup the uploader | ||
let upload = multerConfig({ | ||
let upload = multer_config_1.default({ | ||
randomStringSize: this.randomStringSize, | ||
@@ -66,126 +69,102 @@ fileSize: this.filesizeLimitsMB, | ||
if (this.debug) | ||
this.log(this.alldir.maindir); | ||
this.log(`launching ${this.alldir.maindir} plugin`); | ||
this.app.set('trust proxy', true); | ||
// Main Page | ||
this.app.get(`/${this.alldir.maindir}`, this.mainPage.bind(this)); | ||
// Uploading Page | ||
this.app.post(`/${this.alldir.maindir}/upload`, upload, this.uploadFile.bind(this)); | ||
this.app.get(`/${this.alldir.maindir}`, (req, res) => this.mainPage(req, res)); | ||
this.app.post(`/${this.alldir.maindir}/upload`, upload, (req, res) => this.uploadFile(req, res)); | ||
} | ||
/**main page */ | ||
async mainPage(req, res) { | ||
// log a user visit | ||
this.log(`visited`, req); | ||
this.router.setPage(res, "main", { | ||
compilers: this.compilers | ||
mainPage(req, res) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.log(`visited`, req); | ||
this.router.setPage(res, "main", { | ||
compilers: this.compilers | ||
}); | ||
}); | ||
} | ||
checkFile(req, resOrToken) { | ||
const file = req.file; | ||
const cutConnection = () => { | ||
if (typeof resOrToken == typeof Response) | ||
resOrToken.end(); | ||
}; | ||
// Check if the file exists | ||
if (!file) { | ||
this.log("No file been selected!", req, resOrToken); | ||
cutConnection(); | ||
return; | ||
} | ||
; | ||
// check the file size | ||
if (file.size > this.filesizeLimitsMB * MB) { | ||
this.log(`The file is larger than ${this.filesizeLimitsMB}MB`, req, resOrToken); | ||
cutConnection(); | ||
return; | ||
} | ||
uploadFile(req, res) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const userCaptchakey = req.body['g-recaptcha']; | ||
if (userCaptchakey === undefined || userCaptchakey === '' || userCaptchakey === null) { | ||
this.error("rickrolled, you tried to abuse the system huh?", req, res); | ||
} | ||
const [stated, msg] = yield this.router.reCaptchaCheck(userCaptchakey, req.ip) | ||
.catch((err) => { | ||
this.log(err, req, res); | ||
}); | ||
if (!stated) { | ||
this.log(msg, req, res); | ||
return; | ||
} | ||
this.log(msg); | ||
const file = req.file; | ||
if (!file) { | ||
this.error("No file been selected!", req, res); | ||
res.end(); | ||
return; | ||
} | ||
; | ||
if (file.size > this.filesizeLimitsMB * MB) { | ||
this.error(`The file is larger than ${this.filesizeLimitsMB}MB`, req, res); | ||
res.end(); | ||
return; | ||
} | ||
const token = randomstring_1.generate(); | ||
this.router.newSocketUser(token); | ||
res.status(200).send(token); | ||
res.end(); | ||
yield aforwait_1.aforSec(1); | ||
this.convert(token, req); | ||
} | ||
catch (err) { | ||
console.error(err); | ||
} | ||
}); | ||
} | ||
/**upload file */ | ||
async uploadFile(req, res) { | ||
try { | ||
const userCaptchakey = req.body['g-recaptcha']; | ||
// check for captcha abuse | ||
if (userCaptchakey === undefined || userCaptchakey === '' || userCaptchakey === null) { | ||
this.log("rickrolled, you tried to abuse the system huh?", req, res); | ||
convert(token, req) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const compileType = req.body.type; | ||
const file = req.file; | ||
if (compileType == undefined) { | ||
this.error("No compiler has been selected!", req, token); | ||
return; | ||
} | ||
// check for user verification | ||
const [stated, msg] = await this.router.reCaptchaCheck(userCaptchakey, req.ip); | ||
if (!stated) { | ||
this.log(msg, req, res); | ||
const nameprops = new utils_1.NamePro(file.filename); | ||
const originname = nameprops.name; | ||
yield nameprops.filter(this.filter); | ||
const nameWT = nameprops.withType(); | ||
const compiler = this.compilers[compileType]; | ||
if (!compiler) { | ||
this.error(`the compiler ${compileType} doesn't exists`, req, token); | ||
return; | ||
} | ||
this.log(msg); | ||
// generate user token for the socket | ||
const token = generate(); | ||
// make available token for a user to access | ||
this.router.newSocketUser(token); | ||
// send the token to the user | ||
res.send(token); | ||
// start converting | ||
this.convert(token, req); | ||
} | ||
catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
async convert(token, req) { | ||
const compileType = req.body.type; | ||
const file = req.file; | ||
this.checkFile(req, token); | ||
// check if a compile is been selected | ||
if (compileType == undefined) { | ||
this.log("No compiler has been selected!", req, token); | ||
return; | ||
} | ||
// setting properties | ||
const props = getProps(file.filename); | ||
let name = props.name; | ||
const nameWT = name + "." + props.type; | ||
// filter words in the name | ||
if (this.filter.enabled) | ||
name = await filter(name, this.filter); | ||
// get the right compiler | ||
const compiler = this.compilers[compileType]; | ||
// Check if the compiler actually there | ||
if (!compiler) { | ||
this.log(`the compiler ${compileType} doesn't exists`, req, token); | ||
return; | ||
} | ||
// check if the compiler can work with the file | ||
if (compiler.whitelistInputs[0]) { | ||
if (compiler.whitelistInputs.length > 0 && compiler.whitelistInputs.map(a => a.toUpperCase()).indexOf(props.type.toUpperCase()) == -1) { | ||
this.log(`Not an acceptable file type by the compiler | ||
if (compiler.whitelistInputs[0]) { | ||
if (compiler.whitelistInputs.length > 0 && compiler.whitelistInputs.map(a => a.toUpperCase()).indexOf(nameprops.type.toUpperCase()) == -1) { | ||
this.error(`Not an acceptable file type by the compiler | ||
${compiler.name}\n it only accepts | ||
[${compiler.whitelistInputs.join(`,`)}]`, req, token); | ||
return; | ||
return; | ||
} | ||
} | ||
} | ||
// definig the output name | ||
const newnameWT = `${name}.zip`; | ||
// defining paths | ||
const uploadpath = Path.join(this.inputdir, nameWT); | ||
const outputDirPath = Path.join(this.outputdir, name); | ||
const downloadpath = Path.join(this.outputdir, newnameWT); | ||
const URLFILE = `/files/${newnameWT}`; | ||
/// doing the work using Await | ||
// compiling | ||
await this.compileFile(token, nameWT, compileType); | ||
// delete the input file | ||
await deleteFile(uploadpath); | ||
// zip the output folder | ||
await this.zipTheOutputDirectory(name); | ||
// delete the the output folder | ||
await deleteDirectory(outputDirPath); | ||
// making url for the file | ||
await this.makeGetReqForTheFile(URLFILE, downloadpath); | ||
// redirecting | ||
//res.redirect(URLFILE); | ||
// socket way | ||
this.router.newSocketMessage(token, "url", URLFILE); | ||
this.router.endSocketUser(token); | ||
const inputFilePath = path_1.join(this.inputdir, originname); | ||
const outputDirPath = path_1.join(this.outputdir, nameprops.name); | ||
const zipfilename = nameprops.withType("zip"); | ||
const zipFilePath = path_1.join(this.outputdir, zipfilename); | ||
const urlLink = `files/${randomstring_1.generate()}/${zipfilename}`; | ||
const errlog = (err) => this.log(err, req, token); | ||
this.router.newSocketMessage(token, "log", "Compiling"); | ||
yield this.compileFile(token, nameprops, compileType).catch(errlog); | ||
yield utils_1.deleteFile(inputFilePath).catch(errlog); | ||
this.router.newSocketMessage(token, "log", "zipping the folder"); | ||
yield this.zipTheOutputDirectory(outputDirPath).catch(errlog); | ||
yield utils_1.deleteDirectory(outputDirPath).catch(errlog); | ||
yield this.makeGetReqForTheFile(urlLink, zipFilePath).catch(errlog); | ||
this.router.newSocketMessage(token, "url", urlLink); | ||
this.router.endSocketUser(token); | ||
}); | ||
} | ||
/**depricated!! */ | ||
setTextConverter() { | ||
this.app.get('/textconverter', (req, res) => { | ||
this.app.get('/textconverter', (_req, res) => { | ||
res.send(this.router.page("textconverter", { error: "", result: "" })); | ||
}); | ||
this.app.post('/convert', async (req, res) => { | ||
this.app.post('/convert', (req, res) => __awaiter(this, void 0, void 0, function* () { | ||
let mathmlHtml = req.body.input; | ||
@@ -195,3 +174,2 @@ let result = ""; | ||
try { | ||
//@ts-ignore | ||
result = MathML2LaTeX.convert(mathmlHtml); | ||
@@ -204,110 +182,85 @@ } | ||
res.redirect(`./textconverter?portable=${encodeURI(req.body.ts)}&error=${encodeURI(error)}&result=${encodeURI(result)}&input=${encodeURI(mathmlHtml)}`); | ||
}); | ||
})); | ||
} | ||
// discribers | ||
/** Construct Commands from string | ||
* commands are: | ||
* @const {compilerCommand} | ||
* | ||
* @example | ||
* //Main | ||
* | ||
* #{iPath} | ||
* ->is the path for the input file | ||
* | ||
* #{oPath} | ||
* ->is the path for the output folder | ||
* | ||
* //Costume | ||
* | ||
* #{name.type} | ||
* ->is the file name with type | ||
* | ||
* #{name} | ||
* ->is the file name without type | ||
* | ||
* | ||
*/ | ||
Command(FileNameWT, compilerIndex) { | ||
const compiler = this.compilers[compilerIndex]; | ||
const path = this.inputdir; | ||
const name = getProps(FileNameWT).name; | ||
const pathtoOutput = Path.join(this.outputdir, name); | ||
const pathToInput = Path.join(this.inputdir, FileNameWT); | ||
const pathToInputWithType = `${Path.join(path, FileNameWT)}`; | ||
// Main | ||
let compilerCommand = compiler.command.replace(/#{iPath}/gi, pathToInput); | ||
// directory path | ||
compilerCommand = compilerCommand.replace(/#{oPath}/gi, pathtoOutput); | ||
// costume | ||
compilerCommand = compilerCommand.replace(/#{iPath.type}/gi, pathToInputWithType); | ||
compilerCommand = compilerCommand.replace(/#{name}/gi, name); | ||
compilerCommand = compilerCommand.replace(/#{name.type}/gi, FileNameWT); | ||
return compilerCommand; | ||
} | ||
/** this function compiles a file*/ | ||
compileFile(token, FileNameWT, compileIndex) { | ||
const command = this.Command(FileNameWT, compileIndex); | ||
const compiler = this.compilers[compileIndex]; | ||
let compilerPath = join(this.router.path("main"), compiler.CompilerPath); | ||
return this.execShellCommand(`${compiler.commander} "${compilerPath}" ${command}`, (stdout) => { | ||
// socket.io sending logs to the user on the proccess | ||
this.router.newSocketMessage(token, "log", stdout); | ||
Command(nameProps, compilerIndex) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const compiler = this.compilers[compilerIndex]; | ||
const path = this.inputdir; | ||
const name = nameProps.name; | ||
const FileNameWT = nameProps.withType(); | ||
const pathtoOutput = path_1.join(this.outputdir, name); | ||
const pathToInput = path_1.join(this.inputdir, FileNameWT); | ||
const pathToInputWithType = `${path_1.join(path, FileNameWT)}`; | ||
if (compiler.buildOutputDirectory) | ||
yield utils_1.createDir(pathtoOutput); | ||
let compilerCommand = compiler.command.replace(/#{iPath}/gi, pathToInput); | ||
compilerCommand = compilerCommand.replace(/#{oPath}/gi, pathtoOutput); | ||
compilerCommand = compilerCommand.replace(/#{iPath.type}/gi, pathToInputWithType); | ||
compilerCommand = compilerCommand.replace(/#{name}/gi, name); | ||
compilerCommand = compilerCommand.replace(/#{name.type}/gi, FileNameWT); | ||
return compilerCommand; | ||
}); | ||
} | ||
async zipTheOutputDirectory(name) { | ||
return new Promise((solve, reject) => { | ||
// input directory | ||
const output_dir = join(this.outputdir, name); | ||
// output file | ||
const output = fs.createWriteStream(`${output_dir}.zip`); | ||
// ziping technic | ||
const archive = archiver('zip'); | ||
archive.pipe(output); | ||
archive.directory(output_dir, false); | ||
output.on('close', solve); | ||
archive.on('error', reject); | ||
archive.finalize(); | ||
compileFile(token, nameprop, compileIndex) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const command = yield this.Command(nameprop, compileIndex); | ||
const compiler = this.compilers[compileIndex]; | ||
let compilerPath = path_1.join(this.router.path("main"), compiler.CompilerPath); | ||
if (compiler.CompilerLink) { | ||
return this.requestCompiler(compiler.CompilerLink, `${compiler.commander} "${compilerPath}" ${command}`, (stdout) => { | ||
this.router.newSocketMessage(token, "log", stdout); | ||
}); | ||
} | ||
return this.execShellCommand(`${compiler.commander} "${compilerPath}" ${command}`, (stdout) => { | ||
this.router.newSocketMessage(token, "log", stdout); | ||
}); | ||
}); | ||
} | ||
/** this function download the file to the server **DEPRICATED** */ | ||
uploadTheFile(file, uploadpath) { | ||
console.warn("the function uploadTheFile from converterPlugin is **DEPRICATED**"); | ||
// upload the file | ||
return new Promise((resolve, reject) => { | ||
file.mv(uploadpath, (err) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
else { | ||
setTimeout(resolve, 2000); | ||
} | ||
zipTheOutputDirectory(path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((solve, reject) => { | ||
const output = fs.createWriteStream(`${path}.zip`); | ||
const archive = archiver('zip'); | ||
archive.pipe(output); | ||
archive.directory(path, false); | ||
output.on('close', solve); | ||
archive.on('error', reject); | ||
archive.finalize(); | ||
}); | ||
}); | ||
} | ||
/** this function exicute a programmer with params */ | ||
execShellCommand(cmd, stdcb) { | ||
return new Promise((resolve, reject) => { | ||
let execi = exec(cmd, (error, stdout, stderr) => { | ||
if (error) | ||
console.warn(error); | ||
const execi = child_process_1.exec(cmd, (error, stdout, stderr) => { | ||
if (error) | ||
stdcb(error); | ||
if (stdout) | ||
stdcb(stdout); | ||
}); | ||
if (stderr) | ||
stdcb(stderr); | ||
}); | ||
return new Promise((resolve) => { | ||
execi.on('exit', resolve); | ||
}); | ||
} | ||
// downloader | ||
async makeGetReqForTheFile(urlfile, filepath) { | ||
this.app.get(urlfile, (req, res) => { | ||
if (!fs.existsSync(filepath)) { | ||
res.send(this.log("sorry the file is no longer avaliable", req, res)); | ||
} | ||
res.download(filepath); | ||
requestCompiler(cCompilerLink, cmd, stdcb) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let data = yield node_fetch_1.default(cCompilerLink, { method: 'GET', body: JSON.stringify({ cmd }) }); | ||
stdcb(data); | ||
return data; | ||
}); | ||
Promise.resolve(); | ||
} | ||
/** just a debugger and a messenger to the client if error*/ | ||
log(errorMes, req, resOrToken) { | ||
makeGetReqForTheFile(urlLink, filepath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.app.get(`/${this.alldir.maindir}/${urlLink}`, (req, res) => { | ||
if (!fs.existsSync(filepath)) { | ||
res.send(this.error("sorry the file is no longer avaliable", req, res)); | ||
} | ||
res.download(filepath); | ||
}); | ||
Promise.resolve(); | ||
}); | ||
} | ||
logger(type, errorMes, req, resOrToken) { | ||
if (resOrToken) | ||
if (typeof resOrToken == typeof Response) { | ||
if (typeof resOrToken != typeof "") { | ||
resOrToken.status(406).send({ message: errorMes }); | ||
@@ -317,34 +270,44 @@ resOrToken.end(); | ||
else { | ||
this.router.newSocketMessage(resOrToken, "log", errorMes); | ||
this.router.newSocketMessage(resOrToken, type, errorMes); | ||
this.router.endSocketUser(resOrToken); | ||
} | ||
let ip = req ? req.ip ? req.ip : "" : ""; | ||
let msg = `[${Date.now()}] ${ip} ${errorMes}\n`; | ||
let msg = `[${Date.now()}] ${ip} ${errorMes}`; | ||
if (this.debug) | ||
console.log(msg); | ||
if (this.logInFile) { | ||
let logfile = join(this.router.logdir, "log.txt"); | ||
fs.appendFile(logfile, msg, (err) => { | ||
let logfile = path_1.join(this.router.logdir, type + ".txt"); | ||
fs.appendFile(logfile, msg + "\n", (err) => { | ||
if (err) | ||
console.warn(`[${Date.now()}] ${ip} Not Able to log into file because the file is not accesible ${logfile}`); | ||
}); // => | ||
}); | ||
} | ||
} | ||
/** function to delete temp files off input and output folders*/ | ||
log(errorMes, req, resOrToken) { | ||
return this.logger("log", errorMes, req, resOrToken); | ||
} | ||
error(errorMes, req, resOrToken) { | ||
return this.logger("err", errorMes, req, resOrToken); | ||
} | ||
deleteGarbage() { | ||
const dirin = this.inputdir; | ||
const dirout = this.outputdir; | ||
deleteAllFilesInDirectory(dirin); | ||
deleteAllFilesInDirectory(dirout); | ||
if (!fs.existsSync(dirin)) { | ||
fs.mkdirSync(dirin); | ||
} | ||
if (!fs.existsSync(dirout)) { | ||
fs.mkdirSync(dirout); | ||
} | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const dirin = this.inputdir; | ||
if (!fs.existsSync(dirin)) { | ||
fs.mkdirSync(dirin); | ||
} | ||
else { | ||
yield utils_1.deleteAllFilesInDirectory(dirin); | ||
} | ||
const dirout = this.outputdir; | ||
if (!fs.existsSync(dirout)) { | ||
fs.mkdirSync(dirout); | ||
} | ||
else { | ||
yield utils_1.deleteAllFilesInDirectory(dirout); | ||
} | ||
}); | ||
} | ||
/** get compiler by its name*/ | ||
getIndexByName(name) { | ||
return getNameOf(this.compilers, name); | ||
return utils_1.getNameOf(this.compilers, name); | ||
} | ||
/** the garbage cleaner cicle starter*/ | ||
GarbageCleaner() { | ||
@@ -357,10 +320,15 @@ const _dir = this.outputdir; | ||
for (const file of files) { | ||
var stats = fs.statSync(Path.join(_dir, file)); | ||
var mtime = stats.mtime; | ||
if (Number(new Date()) - Number(new Date(mtime)) >= this.timetoGarbageCleaner * Minute) { | ||
fs.unlink(Path.join(_dir, file), (err) => { | ||
if (err) | ||
console.error(err); | ||
}); | ||
const _path = path_1.join(_dir, file); | ||
let stats = fs.statSync(_path); | ||
if (stats.isDirectory()) { | ||
stats = null; | ||
} | ||
else { | ||
const mtime = stats.mtime; | ||
if (Number(new Date()) - Number(new Date(mtime)) >= this.timetoGarbageCleaner * Minute) { | ||
(() => __awaiter(this, void 0, void 0, function* () { | ||
yield utils_1.deleteFile(_path); | ||
}))(); | ||
} | ||
} | ||
} | ||
@@ -370,15 +338,8 @@ }); | ||
} | ||
/** function to correct input and output directory if was relativly to the root | ||
* directory | ||
*/ | ||
setInputOutputDir() { | ||
let inputdir = this.router.path('inputdir'); | ||
let outputdir = this.router.path('outputdir'); | ||
this.inputdir = inputdir; | ||
this.outputdir = outputdir; | ||
this.inputdir = this.router.path('inputdir'); | ||
this.outputdir = this.router.path('outputdir'); | ||
} | ||
/** for not specifying all object parameters under constructor and safe lines of code*/ | ||
getParams(obj) { | ||
for (const [key, value] of Object.entries(obj)) { | ||
//@ts-ignore | ||
this[key] = value; | ||
@@ -388,1 +349,3 @@ } | ||
} | ||
exports.default = CompilersHandler; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "converter-toless-plugin", | ||
"version": "0.1.02", | ||
"version": "0.1.3", | ||
"description": "this is a plugin", | ||
@@ -10,3 +10,3 @@ "main": "./lib/index.js", | ||
"watch": "tsc -watch", | ||
"pub": "npm run build && npm publish" | ||
"pub": "tsc && npm publish" | ||
}, | ||
@@ -23,15 +23,18 @@ "keywords": [], | ||
"@types/multer": "^1.4.5", | ||
"@types/node": "^14.14.22", | ||
"@types/node": "^14.14.28", | ||
"@types/node-fetch": "^2.5.8", | ||
"@types/randomstring": "^1.1.6", | ||
"typescript": "^4.1.3" | ||
"typescript": "^4.1.5" | ||
}, | ||
"dependencies": { | ||
"aforwait": "^1.1.11", | ||
"app-root-path": "^3.0.0", | ||
"archiver": "5.2.0", | ||
"child_process": "^1.0.2", | ||
"del": "^6.0.0", | ||
"express": "^4.17.1", | ||
"mime-types": "^2.1.28", | ||
"multer": "^1.4.2", | ||
"randomstring": "^1.1.5", | ||
"rimraf": "^3.0.2" | ||
"node-fetch": "^2.6.1", | ||
"randomstring": "^1.1.5" | ||
}, | ||
@@ -41,2 +44,2 @@ "publishConfig": { | ||
} | ||
} | ||
} |
@@ -19,6 +19,4 @@ // String.prototype.replaceAll = function ra(string, string2) { | ||
words: [ | ||
"fuck", "shit", "Swear", "www", "https", "http" | ||
"fuck", "shit", "Swear", "www", "https", "http", "*", ">", '"' | ||
], | ||
replaceBy: "u", | ||
filterWords: true, | ||
@@ -30,3 +28,5 @@ | ||
filterUsingDecoder: true | ||
filterUsingDecoder: true, | ||
replaceBy: "u", | ||
} | ||
@@ -38,6 +38,6 @@ | ||
_string = (_filter.filterWords) ? await step1(_string, _filter) : _string; | ||
_string = (_filter.filterWords) ? await step2(_string, _filter) : _string; | ||
_string = (_filter.filterWords) ? await step3(_string) : _string; | ||
_string = (_filter.filterWords) ? await step4(_string, _filter) : _string; | ||
_string = (_filter.filterWords) ? await layer1(_string, _filter) : _string; | ||
_string = (_filter.filterWords) ? await layer2(_string, _filter) : _string; | ||
_string = (_filter.filterWords) ? await layer3(_string) : _string; | ||
_string = (_filter.filterWords) ? await layer4(_string, _filter) : _string; | ||
@@ -47,41 +47,41 @@ return _string | ||
// filter words | ||
function step1(string: string, filter: FilterOps): Promise<string> { | ||
/** Filtering words out */ | ||
function layer1(str: string, filter: FilterOps): Promise<string> { | ||
return new Promise((solve) => { | ||
let _string = string | ||
let _str = str | ||
filter.words.forEach((word: string) => { | ||
_string = replaceAll(_string, word, filter.replaceBy) | ||
_str = replaceAll(_str, word, filter.replaceBy) | ||
}) | ||
solve(_string) | ||
return _string; | ||
solve(_str) | ||
return _str; | ||
}) | ||
} | ||
// filter commas | ||
function step2(string: string, filter: FilterOps): Promise<string> { | ||
/** Filtering commas out */ | ||
function layer2(str: string, filter: FilterOps): Promise<string> { | ||
return new Promise((solve) => { | ||
let _string = string.replace(/"/gi, filter.replaceBy) | ||
_string = _string.replace(/'/gi, filter.replaceBy) | ||
solve(_string); | ||
return _string | ||
let _str = str.replace(/"/gi, filter.replaceBy) | ||
_str = _str.replace(/'/gi, filter.replaceBy) | ||
solve(_str); | ||
return _str | ||
}) | ||
} | ||
// filter with decoding | ||
function step3(string: string, filter?: FilterOps): Promise<string> { | ||
/** Filtering with encoding */ | ||
function layer3(str: string): Promise<string> { | ||
return new Promise((solve) => { | ||
let _string = encodeURI(string) | ||
solve(_string); | ||
return _string | ||
let _str = encodeURI(str) | ||
solve(_str); | ||
return _str | ||
}) | ||
} | ||
// filter slashes | ||
function step4(string: string, filter: FilterOps): Promise<string> { | ||
/** Filtering slashes out */ | ||
function layer4(str: string, filter: FilterOps): Promise<string> { | ||
return new Promise((solve) => { | ||
let _string = string.replace(/\\/gi, filter.replaceBy) | ||
_string = _string.replace(/\//gi, filter.replaceBy) | ||
solve(_string); | ||
return _string | ||
let _str = str.replace(/\\/gi, filter.replaceBy) | ||
_str = _str.replace(/\//gi, filter.replaceBy) | ||
solve(_str); | ||
return _str | ||
}) | ||
} |
@@ -1,57 +0,91 @@ | ||
import * as path from "path" | ||
import { join } from "path" | ||
import * as fs from "fs" | ||
import type { Compiler } from "../../d/types" | ||
import type { Compiler, FilterOps } from "../../d/types" | ||
import del = require('del'); | ||
import filter from "./filter"; | ||
import { generate as generateRandomString, GenerateOptions } from "randomstring"; | ||
export function getNameOf(arry: Compiler[], name: string) { | ||
let indexofar = -1; | ||
arry.forEach((ar, i) => { | ||
if (ar.name === name) { | ||
indexofar = i; | ||
} | ||
}); | ||
return indexofar; | ||
} | ||
export function getProps(name: string) { | ||
const n = name.split("."); | ||
const n2 = name.split(""); | ||
const type: string = name.split(".")[n.length - 1] | ||
const other: string = n2.slice(0, n2.length - type.length - 1).join('') | ||
return { type, name: other } | ||
export class NamePro { | ||
all: string; | ||
type: string; | ||
name: string; | ||
constructor(string: string) { | ||
this.all = string; | ||
const n = this.all.split("."); | ||
const n2 = this.all.split(""); | ||
this.type = this.all.split(".")[n.length - 1]; | ||
this.name = n2.slice(0, n2.length - this.type.length - 1).join(''); | ||
} | ||
withType(_type: string = this.type) { | ||
return `${this.name}.${_type}` | ||
} | ||
async filter(filterOptions: FilterOps) { | ||
if (filterOptions.enabled) | ||
this.name = await filter(this.name, filterOptions) | ||
} | ||
randomize(options?: number | GenerateOptions) { | ||
this.name = `${this.name}_${generateRandomString(options)}` | ||
} | ||
} | ||
// delete functions | ||
export function deleteFile(filePath: string) { | ||
fs.unlink(filePath, (err) => { | ||
if (err) { | ||
console.log(err) | ||
} | ||
}) | ||
export async function deleteFileOrDirectory(filePath: string): Promise<void> { | ||
await del(filePath, { force: true }) | ||
} | ||
export async function deleteFile(filePath: string): Promise<void> { | ||
await del(filePath, { force: true }) | ||
} | ||
export function deleteDirectory(dirPath: string) { | ||
return new Promise((solve) => { | ||
fs.rmdir(dirPath, { recursive: true }, solve) | ||
}) | ||
export async function deleteDirectory(dirPath: string): Promise<any> { | ||
await del(dirPath, { force: true }) | ||
} | ||
export function deleteAllFilesInDirectory(dir: string) { | ||
fs.readdir(dir, (err, files) => { | ||
if (err) throw err; | ||
for (const file of files) { | ||
fs.unlink(path.join(dir, file), err => { | ||
if (err) throw err; | ||
}); | ||
} | ||
}); | ||
export function deleteAllFilesInDirectory(dir: string): Promise<void> { | ||
return new Promise((s, r) => { | ||
fs.readdir(dir, async (err, files) => { | ||
if (err) r(err); | ||
for (const file of files) { | ||
const pathi = join(dir, file); | ||
deleteFileOrDirectory(pathi); | ||
} | ||
s() | ||
}); | ||
}) | ||
} | ||
export function createDir(path: string): Promise<void> { | ||
return new Promise((s, r) => { | ||
if (!fs.existsSync(path)) { | ||
fs.mkdirSync(path); | ||
} | ||
s() | ||
export function getNameOf(arry: Compiler[], name: string) { | ||
let indexofar = -1; | ||
arry.forEach((ar, i) => { | ||
if (ar.name === name) { | ||
indexofar = i; | ||
} | ||
}); | ||
return indexofar; | ||
}) | ||
} | ||
@@ -62,1 +96,2 @@ | ||
@@ -1,10 +0,10 @@ | ||
import multer from "multer" | ||
import { getProps } from "../components/utils.js"; | ||
import * as multer from "multer" | ||
import * as mime from "mime-types" | ||
import { extension } from "mime-types" | ||
// for generating random strings | ||
import { generate as generateRandomString } from "randomstring"; | ||
import { Request } from "express"; | ||
import { NamePro } from "../components/utils"; | ||
let MB = 2048; | ||
let MB = 1024 * 1024; | ||
@@ -24,4 +24,6 @@ interface upload_file_propies { | ||
filename: (req: Request<any, any, any, any, Record<string, any>>, file: any, cb: (error: Error | null, filename: string) => void) => { | ||
const fileprops = getProps(file.originalname) | ||
cb(null, `${fileprops.name}_F_${generateRandomString(props.randomStringSize)}.${mime.extension(file.mimetype)}`) | ||
const fileprops = new NamePro(file.originalname) | ||
fileprops.randomize(props.randomStringSize); | ||
fileprops.name = fileprops.name.toLowerCase(); | ||
cb(null, `${fileprops.name}.${extension(file.mimetype)}`) | ||
} | ||
@@ -34,5 +36,5 @@ }); | ||
limits: { | ||
fileSize: props.fileSize * MB + 1 * MB | ||
fileSize: props.fileSize * MB + 3 * MB | ||
}, ...props.additions | ||
}); | ||
} |
309
src/index.ts
// for running commands | ||
import { exec } from "child_process"; | ||
import fetch from "node-fetch" | ||
// for file controling | ||
import * as Path from "path"; | ||
import * as fs from "fs"; | ||
import arp from "app-root-path"; | ||
const _dirname = arp.path | ||
import { path as _dirname } from "app-root-path"; | ||
import { generate as generateRandomString } from "randomstring"; | ||
// for types | ||
@@ -18,11 +17,10 @@ import { Compiler, converterOptions, Dirs, Router } from "../d/types"; | ||
// for utils | ||
import { getNameOf, getProps, deleteFile, deleteAllFilesInDirectory, deleteDirectory } from "./components/utils.js"; | ||
import { getNameOf, deleteFile, deleteAllFilesInDirectory, deleteDirectory, NamePro, createDir } from "./components/utils"; | ||
// for filtering names | ||
import filter, { DefaultFilter } from "./components/filter.js"; | ||
import { DefaultFilter } from "./components/filter"; | ||
import { join } from 'path'; | ||
import { Request, response, Response } from "express"; | ||
import multerConfig from "./config/multer.config.js"; | ||
import { Request, Response } from "express"; | ||
import multerConfig from "./config/multer.config"; | ||
@@ -33,11 +31,9 @@ | ||
// for archiving | ||
import archiver from "archiver" | ||
import { generate } from "randomstring"; | ||
import { type } from "os"; | ||
import * as archiver from "archiver" | ||
//@ts-ignore | ||
import { aforSec } from "aforwait"; | ||
const defaultProps = { | ||
PORT: 3000, | ||
compilers: [ | ||
@@ -49,2 +45,3 @@ { | ||
command: `-o "#{CompeleteOutputFilePath}" -i "#{CompeleteInputFilePath}"`, | ||
buildOutputDirectory: true, | ||
whitelistInputs: ["Doc", "Docx"] | ||
@@ -58,3 +55,3 @@ }, | ||
logfile: Path.join(_dirname, 'logs', 'converterlog.txt'), | ||
logfile: join(_dirname, 'logs', 'converterlog.txt'), | ||
@@ -72,3 +69,3 @@ timetoGarbageCleaner: 240, | ||
const MB = 2048; | ||
const MB = 1024 * 1024; | ||
const Minute = 60000; | ||
@@ -102,3 +99,3 @@ | ||
/** this function starts the server */ | ||
start() { | ||
async start() { | ||
this.setInputOutputDir() | ||
@@ -108,3 +105,3 @@ | ||
if (this.deleteallTempFilesOnStart) { | ||
this.deleteGarbage() | ||
await this.deleteGarbage() | ||
} | ||
@@ -128,3 +125,3 @@ | ||
if (this.debug) this.log(this.alldir.maindir) | ||
if (this.debug) this.log(`launching ${this.alldir.maindir} plugin`) | ||
@@ -134,5 +131,5 @@ this.app.set('trust proxy', true) | ||
// Main Page | ||
this.app.get(`/${this.alldir.maindir}`, this.mainPage.bind(this)); | ||
this.app.get(`/${this.alldir.maindir}`, (req: Request, res: Response) => this.mainPage(req, res)); | ||
// Uploading Page | ||
this.app.post(`/${this.alldir.maindir}/upload`, upload, this.uploadFile.bind(this)); | ||
this.app.post(`/${this.alldir.maindir}/upload`, upload, (req: Request, res: Response) => this.uploadFile(req, res)); | ||
} | ||
@@ -153,26 +150,2 @@ | ||
checkFile(req: Request, resOrToken: any) { | ||
const file = req.file | ||
const cutConnection = () => { | ||
if (typeof resOrToken == typeof Response) | ||
resOrToken.end(); | ||
} | ||
// Check if the file exists | ||
if (!file) { | ||
this.log("No file been selected!", req, resOrToken); | ||
cutConnection(); | ||
return; | ||
}; | ||
// check the file size | ||
if (file.size > this.filesizeLimitsMB * MB) { | ||
this.log(`The file is larger than ${this.filesizeLimitsMB}MB`, req, resOrToken) | ||
cutConnection(); | ||
return; | ||
} | ||
} | ||
/**upload file */ | ||
@@ -185,3 +158,3 @@ async uploadFile(req: Request, res: Response) { | ||
this.log("rickrolled, you tried to abuse the system huh?", req, res); | ||
this.error("rickrolled, you tried to abuse the system huh?", req, res); | ||
} | ||
@@ -191,5 +164,9 @@ | ||
const [stated, msg] = await this.router.reCaptchaCheck(userCaptchakey, req.ip) | ||
.catch((err: string) => { | ||
this.log(err, req, res); | ||
}) | ||
if (!stated) { | ||
this.log(msg, req, res); | ||
return | ||
} | ||
@@ -199,9 +176,33 @@ | ||
const file = req.file | ||
// Check if the file exists | ||
if (!file) { | ||
this.error("No file been selected!", req, res); | ||
res.end(); | ||
return; | ||
}; | ||
// check the file size | ||
if (file.size > this.filesizeLimitsMB * MB) { | ||
this.error(`The file is larger than ${this.filesizeLimitsMB}MB`, req, res) | ||
res.end(); | ||
return; | ||
} | ||
// generate user token for the socket | ||
const token = generate(); | ||
const token = generateRandomString(); | ||
// make available token for a user to access | ||
this.router.newSocketUser(token) | ||
// send the token to the user | ||
res.send({ token }) | ||
res.status(200).send(token) | ||
res.end(); | ||
await aforSec(1) | ||
// start converting | ||
@@ -221,7 +222,5 @@ this.convert(token, req) | ||
this.checkFile(req, token) | ||
// check if a compile is been selected | ||
if (compileType == undefined) { | ||
this.log("No compiler has been selected!", req, token); | ||
this.error("No compiler has been selected!", req, token); | ||
return; | ||
@@ -232,11 +231,11 @@ } | ||
// setting properties | ||
const props = getProps(file.filename) | ||
let name = props.name; | ||
const nameWT = name + "." + props.type; | ||
const nameprops = new NamePro(file.filename) | ||
const originname = nameprops.name; | ||
// filter words in the name | ||
if (this.filter.enabled) | ||
name = await filter(name, this.filter) | ||
await nameprops.filter(this.filter) | ||
const nameWT = nameprops.withType(); | ||
@@ -248,3 +247,3 @@ // get the right compiler | ||
if (!compiler) { | ||
this.log(`the compiler ${compileType} doesn't exists`, req, token); | ||
this.error(`the compiler ${compileType} doesn't exists`, req, token); | ||
return; | ||
@@ -255,5 +254,4 @@ } | ||
if (compiler.whitelistInputs[0]) { | ||
if (compiler.whitelistInputs.length > 0 && compiler.whitelistInputs.map(a => a.toUpperCase()).indexOf(props.type.toUpperCase()) == -1) { | ||
this.log( | ||
if (compiler.whitelistInputs.length > 0 && compiler.whitelistInputs.map(a => a.toUpperCase()).indexOf(nameprops.type.toUpperCase()) == -1) { | ||
this.error( | ||
`Not an acceptable file type by the compiler | ||
@@ -268,28 +266,41 @@ ${compiler.name}\n it only accepts | ||
// definig the output name | ||
const newnameWT = `${name}.zip`; | ||
// defining paths | ||
const uploadpath: string = Path.join(this.inputdir, nameWT); | ||
const outputDirPath: string = Path.join(this.outputdir, name); | ||
const downloadpath: string = Path.join(this.outputdir, newnameWT); | ||
const URLFILE = `/files/${newnameWT}`; | ||
const inputFilePath: string = join(this.inputdir, originname); | ||
const outputDirPath: string = join(this.outputdir, nameprops.name); | ||
// definig the zip file output | ||
const zipfilename = nameprops.withType("zip"); | ||
const zipFilePath: string = join(this.outputdir, zipfilename); | ||
const urlLink = `files/${generateRandomString()}/${zipfilename}`; | ||
/// doing the work using Await | ||
// handling errors | ||
const errlog = (err: string) => | ||
this.log(err, req, token); | ||
// update the user | ||
this.router.newSocketMessage(token, "log", "Compiling"); | ||
// compiling | ||
await this.compileFile(token, nameWT, compileType); | ||
await this.compileFile(token, nameprops, compileType).catch(errlog); | ||
// delete the input file | ||
await deleteFile(uploadpath); | ||
await deleteFile(inputFilePath).catch(errlog); | ||
// update the user | ||
this.router.newSocketMessage(token, "log", "zipping the folder"); | ||
// zip the output folder | ||
await this.zipTheOutputDirectory(name) | ||
await this.zipTheOutputDirectory(outputDirPath).catch(errlog); | ||
// delete the the output folder | ||
await deleteDirectory(outputDirPath); | ||
await deleteDirectory(outputDirPath).catch(errlog); | ||
// making url for the file | ||
await this.makeGetReqForTheFile(URLFILE, downloadpath); | ||
await this.makeGetReqForTheFile(urlLink, zipFilePath).catch(errlog); | ||
@@ -299,4 +310,3 @@ // redirecting | ||
// socket way | ||
this.router.newSocketMessage(token, "url", URLFILE); | ||
this.router.newSocketMessage(token, "url", urlLink); | ||
this.router.endSocketUser(token); | ||
@@ -308,3 +318,3 @@ } | ||
setTextConverter() { | ||
this.app.get('/textconverter', (req: Request, res: Response) => { | ||
this.app.get('/textconverter', (_req: Request, res: Response) => { | ||
res.send(this.router.page("textconverter", { error: "", result: "" })); | ||
@@ -342,3 +352,3 @@ }) | ||
* | ||
* //Costume | ||
* //Costum | ||
* | ||
@@ -353,3 +363,3 @@ * #{name.type} | ||
*/ | ||
Command(FileNameWT: string, compilerIndex: number): string { | ||
async Command(nameProps: NamePro, compilerIndex: number): Promise<string> { | ||
@@ -361,14 +371,15 @@ const compiler = this.compilers[compilerIndex]; | ||
const name = nameProps.name | ||
const FileNameWT = nameProps.withType(); | ||
const name = getProps(FileNameWT).name; | ||
const pathtoOutput = join(this.outputdir, name); | ||
const pathToInput = join(this.inputdir, FileNameWT) | ||
const pathtoOutput = Path.join(this.outputdir, name); | ||
const pathToInput = Path.join(this.inputdir, FileNameWT) | ||
const pathToInputWithType = `${join(path, FileNameWT)}` | ||
const pathToInputWithType = `${Path.join(path, FileNameWT)}` | ||
if (compiler.buildOutputDirectory) | ||
await createDir(pathtoOutput) | ||
// Main | ||
@@ -395,8 +406,15 @@ let compilerCommand = compiler.command.replace(/#{iPath}/gi, pathToInput) | ||
/** this function compiles a file*/ | ||
compileFile(token: string, FileNameWT: string, compileIndex: number) { | ||
async compileFile(token: string, nameprop: NamePro, compileIndex: number) { | ||
const command = this.Command(FileNameWT, compileIndex); | ||
const command = await this.Command(nameprop, compileIndex); | ||
const compiler = this.compilers[compileIndex] | ||
let compilerPath = join(this.router.path("main"), compiler.CompilerPath) | ||
if (compiler.CompilerLink) { | ||
return this.requestCompiler(compiler.CompilerLink, `${compiler.commander} "${compilerPath}" ${command}`, (stdout: string) => { | ||
// socket.io sending logs to the user on the proccess | ||
this.router.newSocketMessage(token, "log", stdout) | ||
}) | ||
} | ||
return this.execShellCommand(`${compiler.commander} "${compilerPath}" ${command}`, (stdout: string) => { | ||
@@ -409,9 +427,6 @@ // socket.io sending logs to the user on the proccess | ||
async zipTheOutputDirectory(name: string) { | ||
async zipTheOutputDirectory(path: string) { | ||
return new Promise((solve, reject) => { | ||
// input directory | ||
const output_dir = join(this.outputdir, name) | ||
// output file | ||
const output = fs.createWriteStream(`${output_dir}.zip`); | ||
const output = fs.createWriteStream(`${path}.zip`); | ||
@@ -423,6 +438,4 @@ // ziping technic | ||
archive.directory(output_dir, false); | ||
archive.directory(path, false); | ||
output.on('close', solve); | ||
@@ -435,35 +448,30 @@ archive.on('error', reject); | ||
/** this function download the file to the server **DEPRICATED** */ | ||
uploadTheFile(file: any, uploadpath: string) { | ||
console.warn("the function uploadTheFile from converterPlugin is **DEPRICATED**") | ||
// upload the file | ||
return new Promise((resolve, reject) => { | ||
file.mv(uploadpath, (err: string) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
setTimeout(resolve, 2000) | ||
} | ||
}) | ||
}) | ||
} | ||
/** this function exicute a programmer with params */ | ||
execShellCommand(cmd: string, stdcb: Function) { | ||
return new Promise((resolve, reject) => { | ||
let execi = exec(cmd, (error: any, stdout: any, stderr: any) => { | ||
if (error) | ||
console.warn(error); | ||
const execi = exec(cmd, (error, stdout, stderr) => { | ||
if (error) | ||
stdcb(error); | ||
if (stdout) | ||
stdcb(stdout) | ||
}); | ||
execi.on('exit', resolve) | ||
if (stderr) | ||
stdcb(stderr) | ||
}); | ||
return new Promise((resolve) => { | ||
execi.on('exit', resolve); | ||
}); | ||
} | ||
/** make a request to one of the compilers */ | ||
async requestCompiler(cCompilerLink: string, cmd: string, stdcb: Function) { | ||
let data = await fetch(cCompilerLink, { method: 'GET', body: JSON.stringify({ cmd }) }) | ||
stdcb(data) | ||
return data | ||
} | ||
// downloader | ||
async makeGetReqForTheFile(urlfile: string, filepath: string) { | ||
this.app.get(urlfile, (req: Request, res: Response) => { | ||
async makeGetReqForTheFile(urlLink: string, filepath: string) { | ||
this.app.get(`/${this.alldir.maindir}/${urlLink}`, (req: Request, res: Response) => { | ||
if (!fs.existsSync(filepath)) { | ||
res.send(this.log("sorry the file is no longer avaliable", req, res)) | ||
res.send(this.error("sorry the file is no longer avaliable", req, res)) | ||
} | ||
@@ -475,10 +483,13 @@ res.download(filepath); | ||
/** just a debugger and a messenger to the client if error*/ | ||
log(errorMes: string, req?: Request, resOrToken?: any) { | ||
// make logs of the errors and other stuff for debuging. | ||
private logger(type: string, errorMes: string, req?: Request, resOrToken?: any) { | ||
if (resOrToken) | ||
if (typeof resOrToken == typeof Response) { | ||
if (typeof resOrToken != typeof "") { | ||
resOrToken.status(406).send({ message: errorMes }) | ||
resOrToken.end(); | ||
} else { | ||
this.router.newSocketMessage(resOrToken, "log", errorMes) | ||
this.router.newSocketMessage(resOrToken, type, errorMes) | ||
this.router.endSocketUser(resOrToken); | ||
} | ||
@@ -488,3 +499,3 @@ | ||
let ip = req ? req.ip ? req.ip : "" : "" | ||
let msg = `[${Date.now()}] ${ip} ${errorMes}\n` | ||
let msg = `[${Date.now()}] ${ip} ${errorMes}` | ||
@@ -494,4 +505,4 @@ if (this.debug) | ||
if (this.logInFile) { | ||
let logfile = join(this.router.logdir, "log.txt") | ||
fs.appendFile(logfile, msg, | ||
let logfile = join(this.router.logdir, type + ".txt") | ||
fs.appendFile(logfile, msg + "\n", | ||
(err: any) => { | ||
@@ -504,15 +515,29 @@ if (err) console.warn(`[${Date.now()}] ${ip} Not Able to log into file because the file is not accesible ${logfile}`) | ||
/** just a debugger and a messenger to the client if error*/ | ||
log(errorMes: string, req?: Request, resOrToken?: any) { | ||
return this.logger("log", errorMes, req, resOrToken) | ||
} | ||
/** just a debugger and a messenger to the client if error*/ | ||
error(errorMes: string, req?: Request, resOrToken?: any) { | ||
return this.logger("err", errorMes, req, resOrToken) | ||
} | ||
/** function to delete temp files off input and output folders*/ | ||
deleteGarbage() { | ||
async deleteGarbage() { | ||
const dirin = this.inputdir | ||
const dirout = this.outputdir | ||
deleteAllFilesInDirectory(dirin); | ||
deleteAllFilesInDirectory(dirout); | ||
if (!fs.existsSync(dirin)) { | ||
fs.mkdirSync(dirin); | ||
} else { | ||
await deleteAllFilesInDirectory(dirin); | ||
} | ||
const dirout = this.outputdir | ||
if (!fs.existsSync(dirout)) { | ||
fs.mkdirSync(dirout); | ||
} else { | ||
await deleteAllFilesInDirectory(dirout); | ||
} | ||
} | ||
@@ -533,8 +558,13 @@ | ||
for (const file of files) { | ||
var stats = fs.statSync(Path.join(_dir, file)); | ||
var mtime = stats.mtime; | ||
if (Number(new Date()) - Number(new Date(mtime)) >= this.timetoGarbageCleaner * Minute) { | ||
fs.unlink(Path.join(_dir, file), (err: any) => { | ||
if (err) console.error(err) | ||
}); | ||
const _path = join(_dir, file) | ||
let stats: any = fs.statSync(_path); | ||
if (stats.isDirectory()) { | ||
stats = null; | ||
} else { | ||
const mtime = stats.mtime; | ||
if (Number(new Date()) - Number(new Date(mtime)) >= this.timetoGarbageCleaner * Minute) { | ||
(async () => { | ||
await deleteFile(_path) | ||
})() | ||
} | ||
} | ||
@@ -551,7 +581,4 @@ } | ||
setInputOutputDir() { | ||
let inputdir = this.router.path('inputdir'); | ||
let outputdir = this.router.path('outputdir') | ||
this.inputdir = inputdir; | ||
this.outputdir = outputdir; | ||
this.inputdir = this.router.path('inputdir'); | ||
this.outputdir = this.router.path('outputdir'); | ||
} | ||
@@ -558,0 +585,0 @@ |
{ | ||
"compilerOptions": { | ||
"module": "ES2020", | ||
"target": "ESNext", | ||
"declaration": true, | ||
"module": "CommonJS", | ||
"target": "ES6", | ||
"lib": [ | ||
"ES6" | ||
], | ||
"outDir": "./lib", | ||
"strict": true, | ||
"moduleResolution": "node", | ||
"allowSyntheticDefaultImports": true | ||
"noImplicitAny": true, | ||
"removeComments": true, | ||
"preserveConstEnums": true, | ||
"sourceMap": true | ||
}, | ||
@@ -11,0 +15,0 @@ "include": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
69359
22
1320
1
10
9
+ Addedaforwait@^1.1.11
+ Addeddel@^6.0.0
+ Addednode-fetch@^2.6.1
+ Added@nodelib/fs.scandir@2.1.5(transitive)
+ Added@nodelib/fs.stat@2.0.5(transitive)
+ Added@nodelib/fs.walk@1.2.8(transitive)
+ Addedaforwait@1.2.0(transitive)
+ Addedaggregate-error@3.1.0(transitive)
+ Addedarray-union@2.1.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedclean-stack@2.2.0(transitive)
+ Addeddel@6.1.1(transitive)
+ Addeddir-glob@3.0.1(transitive)
+ Addedfast-glob@3.3.2(transitive)
+ Addedfastq@1.17.1(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedglobby@11.1.0(transitive)
+ Addedignore@5.3.2(transitive)
+ Addedindent-string@4.0.0(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedis-path-cwd@2.2.0(transitive)
+ Addedis-path-inside@3.0.3(transitive)
+ Addedmerge2@1.4.1(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addedp-map@4.0.0(transitive)
+ Addedpath-type@4.0.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedreusify@1.0.4(transitive)
+ Addedrun-parallel@1.2.0(transitive)
+ Addedslash@3.0.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
- Removedrimraf@^3.0.2