const path = require('path'); const fs = require('fs'); const fse = require('fs-extra'); const os = require('os'); const through = require('through2'); const gulp = require('gulp'); const Sdk = require('./sdk'); const exec = require('./exec'); const PluginError = require('plugin-error'); const colors = require('ansi-colors'); const log = require('fancy-log'); class FlashPlayer extends Sdk { constructor(version) { super(FlashPlayer.ID, version || FlashPlayer.VERSION); } playerPath(debug) { const v = this.version.split('.'); const dir = `${v[0]}_${v[1]}_r${v[2]}_${v[3]}`; return `${this.path}/${dir}${debug ? '_debug': ''}`; } prepare() { let p = super.prepare(); if (!this.prepared && os.type() === 'Linux') { p = p.then(() => { let arch = null; if (os.arch() === 'ia32') { arch = 'i386'} if (os.arch() === 'x64') { arch = 'x86_64'} const extract = (debug) => { const v = this.version.split('.'); const playerPath = this.playerPath(debug); // ToDo: fix const archive = `${playerPath}/flashplayer${v[0]}_${v[1]}r${v[2]}_${v[3]}_linux_sa${debug ? '_debug' : ''}.${arch}.tar.gz`; return gulp.src(archive) .pipe(gunzip()).pipe(untar()) .pipe(gulp.dest(playerPath)); }; return new Promise((success, fail) => { extract().on('end', () => { extract(true).on('end', success).on('error', fail) }).on('error', fail); }) }) } return p; } get prepared() { try { return fs.existsSync(`${this.flashPlayerBin()}`); } catch (e) { return false; } } get link() { return `https://fpdownload.macromedia.com/pub/flashplayer/installers/archive/fp_${this.version}_archive.zip`; } flashPlayerBin(debug) { if (os.type() === 'Windows_NT') { const v = this.version.split('.'); const playerName = `flashplayer${v[0]}_${v[1]}r${v[2]}_${v[3]}_win_sa${debug ? '_debug' : ''}.exe`; return `${this.playerPath(debug)}/${playerName}`; } else if (os.type() === 'Linux') { const binPath = `${this.playerPath(debug)}/${debug ? 'flashplayerdebugger': 'flashplayer'}`; fs.chmodSync(binPath, 0o755); return binPath; } else { throw `Unsupported os '${os.type()}'`; } } static get flashPlayerDir() { if (os.type() === 'Windows_NT') { return `${process.env.APPDATA}/Macromedia/Flash Player`; } else if (os.type() === 'Linux') { return `${os.homedir()}/.macromedia/Flash_Player`; } } static get log() { return `${this.flashPlayerDir}/Logs/flashlog.txt`; } static enableLog() { const filename = `${os.homedir()}/mm.cfg`; const value = 'TraceOutputFileEnable=1'; if (fs.exists(filename)) { const data = fs.readFileSync(filename); if (data.indexOf(value) === -1) { fs.appendFileSync(filename, `${value}\n`); } } else { fs.writeFileSync(filename, `${value}\n`); } } static trust(value) { const filename = `${this.flashPlayerDir}/#Security/FlashPlayerTrust/gulp.cfg`; if (fs.exists(filename)) { const data = fs.readFileSync(filename); if (data.indexOf(value) === -1) { fs.appendFileSync(filename, `${value}\n`); } } else { if (!fs.exists(path.dirname(filename))) { fse.ensureDirSync(path.dirname(filename)); } fs.writeFileSync(filename, `${value}\n`); } } run(debug) { let stream = null; const bufferContents = (file, enc, callback) => { log(this.tag, colors.cyan("run"), colors.magenta(file.path)); FlashPlayer.trust(file.path); FlashPlayer.enableLog(); exec('.', [this.flashPlayerBin(debug), file.path].join(' ')).then(() => { stream.emit('end'); callback(); }).catch((error) => { stream.emit('error', new PluginError({plugin: this.tag, message: error})); callback(); }); //stream.push(file); // ToDo: watch when file is exists // or create log file in FlashPlayer.enableLog()? /*stream.push(new Vinyl({ path: FlashPlayer.log }));*/ stream.push(file); }; return stream = through.obj(bufferContents); } } FlashPlayer.ID = "flashplayer"; FlashPlayer.VERSION_11_2_202_644 = '11.2.202.644'; FlashPlayer.VERSION_11 = FlashPlayer.VERSION_11_2_202_644; FlashPlayer.VERSION_24_0_0_186 = '24.0.0.186'; FlashPlayer.VERSION_24_0_0_194 = '24.0.0.194'; FlashPlayer.VERSION_24_0_0_221 = '24.0.0.221'; FlashPlayer.VERSION_24 = FlashPlayer.VERSION_24_0_0_221; FlashPlayer.VERSION_25_0_0_127 = '25.0.0.127'; FlashPlayer.VERSION_25_0_0_148 = '25.0.0.148'; FlashPlayer.VERSION_25_0_0_163 = '25.0.0.163'; FlashPlayer.VERSION_25_0_0_117 = '25.0.0.171'; FlashPlayer.VERSION_25 = FlashPlayer.VERSION_25_0_0_117; FlashPlayer.VERSION_26_0_0_131 = '26.0.0.131'; FlashPlayer.VERSION_26 = FlashPlayer.VERSION_26_0_0_131; if (os.type() === 'Linux' && os.arch() === 'ia32') { //Linux i386 FlashPlayer.VERSION = FlashPlayer.VERSION_11; } else { FlashPlayer.VERSION = FlashPlayer.VERSION_26; } module.exports = FlashPlayer;