UNPKG

test-file-generator

Version:

Generates different file types for testing purposes. The content of the files is generated with a random function.

176 lines (175 loc) 5.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TestFileGenerator = void 0; const util_1 = require("../util/util"); const jimp_1 = __importDefault(require("jimp")); const fs_1 = __importDefault(require("fs")); const omggif_1 = __importDefault(require("omggif")); var FileType; (function (FileType) { FileType["jpeg"] = "jpeg"; FileType["cvs"] = "cvs"; FileType["txt"] = "txt"; FileType["gif"] = "gif"; })(FileType || (FileType = {})); class TestFileGenerator { constructor(type, clean) { this.created = Array(); this.sizeX = 100; this.sizeY = 100; this.setName(this.name); this.setType(type); this.setLocation(); this.setSize(); this.clean = clean; } generateFile() { this.setName(); this.created.push(this.completeLocation); switch (this.type) { case FileType.cvs: { return this.generateCVS(); } case FileType.jpeg: { return this.generateJPEG(); } case FileType.txt: { return this.generateTXT(); } case FileType.gif: { return this.generateGIF(); } } return false; } generateTXT() { let data = ''; while (this.getMemorySize(data) < this.size) { data = data + util_1.getRandomID(); } return this.writeFile(data); } generateCVS() { let data = '1, 2, 3, 4 \n'; while (this.getMemorySize(data) < this.size) { data = data + util_1.getRandomID() + ',' + util_1.getRandomID() + ',' + util_1.getRandomID() + ',' + util_1.getRandomID() + '\n'; } return this.writeFile(data); } generateJPEG() { const image = this.getRandomJimpImage(); image .writeAsync(this.completeLocation) .then(() => { if (this.clean) { fs_1.default.unlinkSync(this.completeLocation); } }) .catch((error) => { throw error; }); return true; } getRandomJimpImage() { const image = new jimp_1.default(this.sizeX, this.sizeY, 'white', (err, data) => { if (err) { throw err; } return data; }); for (let x = 0; x < this.sizeX; x++) { for (let y = 0; y < this.sizeY; y++) { image.setPixelColor(jimp_1.default.rgbaToInt(util_1.getRandomInt(0, 255), util_1.getRandomInt(0, 255), util_1.getRandomInt(0, 255), util_1.getRandomInt(0, 255)), x, y); } } return image; } generateGIF() { if (this.sizeX < 100 || this.sizeY < 100) { throw new Error('size shold be at least 100x100'); } const size = this.sizeX * this.sizeY; const buf = Buffer.alloc(size, 'base64'); const gf = new omggif_1.default.GifWriter(buf, this.sizeX, this.sizeY, { loop: 2 }); const indices = Array(); const palette = Array(); for (let i = 0; i < size; i++) { indices.push(i); } for (let j = 0; j < 256; j++) { // (r, g, b, a) const color = jimp_1.default.rgbaToInt(util_1.getRandomInt(0, 255), util_1.getRandomInt(0, 255), util_1.getRandomInt(0, 255), util_1.getRandomInt(0, 255)); palette.push(color); } gf.addFrame(0, 0, this.sizeX, this.sizeY, indices, { palette }); palette.reverse(); gf.addFrame(0, 0, this.sizeX, this.sizeY, indices, { palette }); const data = buf.slice(0, gf.end()); return this.writeFile(data); } writeFile(data) { try { fs_1.default.writeFileSync(this.completeLocation, data); if (this.clean) { fs_1.default.unlinkSync(this.completeLocation); } return true; } catch (error) { throw error; } } getMemorySize(data) { return Buffer.byteLength(data) / 1024; } getCreated() { return this.created; } getLocation() { return this.location; } getName() { return this.name; } getSize() { return this.size; } getFileType() { return this.type; } setCompleteLocation() { this.completeLocation = this.getLocation() + this.getName() + '.' + this.getFileType(); } setLocation(location) { this.location = location !== undefined ? location : './'; this.setCompleteLocation(); } setName(name) { this.name = name !== undefined ? name : util_1.getRandomID(); this.setCompleteLocation(); } setSize(size) { this.size = size !== undefined ? size : 20; if (size < 2 || size > 200) { throw new Error('size has to be between 2KB and 200KB.'); } } setType(type) { if (!(type in FileType)) { throw new Error('Unsupported file type: ' + type + '.'); } this.type = type; this.setCompleteLocation(); } setClean(clean) { this.clean = clean; } setImageSize(x, y) { this.sizeX = x; this.sizeY = y; } } exports.TestFileGenerator = TestFileGenerator;