| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- const logger = require('@alias/logger')('growi:services:ExportService'); // eslint-disable-line no-unused-vars
- const fs = require('fs');
- const path = require('path');
- const mongoose = require('mongoose');
- const { Transform } = require('stream');
- const streamToPromise = require('stream-to-promise');
- const archiver = require('archiver');
- const toArrayIfNot = require('../../lib/util/toArrayIfNot');
- class ExportingProgress {
- constructor(collectionName, totalCount) {
- this.collectionName = collectionName;
- this.currentCount = 0;
- this.totalCount = totalCount;
- }
- }
- class ExportingStatus {
- constructor() {
- this.totalCount = 0;
- this.progressList = null;
- this.progressMap = {};
- }
- async init(collections) {
- const promisesForCreatingInstance = collections.map(async(collectionName) => {
- const collection = mongoose.connection.collection(collectionName);
- const totalCount = await collection.count();
- return new ExportingProgress(collectionName, totalCount);
- });
- this.progressList = await Promise.all(promisesForCreatingInstance);
- // collection name to instance mapping
- this.progressList.forEach((p) => {
- this.progressMap[p.collectionName] = p;
- this.totalCount += p.totalCount;
- });
- }
- get currentCount() {
- return this.progressList.reduce(
- (acc, crr) => acc + crr.currentCount,
- 0,
- );
- }
- }
- class ExportService {
- constructor(crowi) {
- this.crowi = crowi;
- this.appService = crowi.appService;
- this.growiBridgeService = crowi.growiBridgeService;
- this.getFile = this.growiBridgeService.getFile.bind(this);
- this.baseDir = path.join(crowi.tmpDir, 'downloads');
- this.per = 100;
- this.zlibLevel = 9; // 0(min) - 9(max)
- this.adminEvent = crowi.event('admin');
- this.currentExportingStatus = null;
- }
- /**
- * parse all zip files in downloads dir
- *
- * @memberOf ExportService
- * @return {object} info for zip files and whether currentExportingStatus exists
- */
- async getStatus() {
- const zipFiles = fs.readdirSync(this.baseDir).filter((file) => { return path.extname(file) === '.zip' });
- const zipFileStats = await Promise.all(zipFiles.map((file) => {
- const zipFile = this.getFile(file);
- return this.growiBridgeService.parseZipFile(zipFile);
- }));
- // filter null object (broken zip)
- const filtered = zipFileStats.filter(element => element != null);
- const isExporting = this.currentExportingStatus != null;
- return {
- zipFileStats: filtered,
- isExporting,
- progressList: isExporting ? this.currentExportingStatus.progressList : null,
- };
- }
- /**
- * create meta.json
- *
- * @memberOf ExportService
- * @return {string} path to meta.json
- */
- async createMetaJson() {
- const metaJson = path.join(this.baseDir, this.growiBridgeService.getMetaFileName());
- const writeStream = fs.createWriteStream(metaJson, { encoding: this.growiBridgeService.getEncoding() });
- const metaData = {
- version: this.crowi.version,
- url: this.appService.getSiteUrl(),
- passwordSeed: this.crowi.env.PASSWORD_SEED,
- exportedAt: new Date(),
- };
- writeStream.write(JSON.stringify(metaData));
- writeStream.close();
- await streamToPromise(writeStream);
- return metaJson;
- }
- /**
- *
- * @param {ExportProguress} exportProgress
- * @return {Transform}
- */
- generateLogStream(exportProgress) {
- const logProgress = this.logProgress.bind(this);
- let count = 0;
- return new Transform({
- transform(chunk, encoding, callback) {
- count++;
- logProgress(exportProgress, count);
- this.push(chunk);
- callback();
- },
- });
- }
- /**
- * insert beginning/ending brackets and comma separator for Json Array
- *
- * @memberOf ExportService
- * @return {TransformStream}
- */
- generateTransformStream() {
- let isFirst = true;
- const transformStream = new Transform({
- transform(chunk, encoding, callback) {
- // write beginning brace
- if (isFirst) {
- this.push('[');
- isFirst = false;
- }
- // write separator
- else {
- this.push(',');
- }
- this.push(chunk);
- callback();
- },
- final(callback) {
- // write ending brace
- this.push(']');
- callback();
- },
- });
- return transformStream;
- }
- /**
- * dump a mongodb collection into json
- *
- * @memberOf ExportService
- * @param {string} collectionName collection name
- * @return {string} path to zip file
- */
- async exportCollectionToJson(collectionName) {
- const collection = mongoose.connection.collection(collectionName);
- const nativeCursor = collection.find();
- const readStream = nativeCursor
- .snapshot()
- .stream({ transform: JSON.stringify });
- // get TransformStream
- const transformStream = this.generateTransformStream();
- // log configuration
- const exportProgress = this.currentExportingStatus.progressMap[collectionName];
- const logStream = this.generateLogStream(exportProgress);
- // create WritableStream
- const jsonFileToWrite = path.join(this.baseDir, `${collectionName}.json`);
- const writeStream = fs.createWriteStream(jsonFileToWrite, { encoding: this.growiBridgeService.getEncoding() });
- readStream
- .pipe(logStream)
- .pipe(transformStream)
- .pipe(writeStream);
- await streamToPromise(writeStream);
- return writeStream.path;
- }
- /**
- * export multiple Collections into json and Zip
- *
- * @memberOf ExportService
- * @param {Array.<string>} collections array of collection name
- * @return {Array.<string>} paths to json files created
- */
- async exportCollectionsToZippedJson(collections) {
- const metaJson = await this.createMetaJson();
- const promises = collections.map(collectionName => this.exportCollectionToJson(collectionName));
- const jsonFiles = await Promise.all(promises);
- // send terminate event
- this.emitStartZippingEvent();
- // zip json
- const configs = jsonFiles.map((jsonFile) => { return { from: jsonFile, as: path.basename(jsonFile) } });
- // add meta.json in zip
- configs.push({ from: metaJson, as: path.basename(metaJson) });
- // exec zip
- const zipFile = await this.zipFiles(configs);
- // get stats for the zip file
- const addedZipFileStat = await this.growiBridgeService.parseZipFile(zipFile);
- // send terminate event
- this.emitTerminateEvent(addedZipFileStat);
- // TODO: remove broken zip file
- }
- async export(collections) {
- if (this.currentExportingStatus != null) {
- throw new Error('There is an exporting process running.');
- }
- this.currentExportingStatus = new ExportingStatus();
- await this.currentExportingStatus.init(collections);
- try {
- await this.exportCollectionsToZippedJson(collections);
- }
- finally {
- this.currentExportingStatus = null;
- }
- }
- /**
- * log export progress
- *
- * @memberOf ExportService
- *
- * @param {ExportProgress} exportProgress
- * @param {number} currentCount number of items exported
- */
- logProgress(exportProgress, currentCount) {
- const output = `${exportProgress.collectionName}: ${currentCount}/${exportProgress.totalCount} written`;
- // update exportProgress.currentCount
- exportProgress.currentCount = currentCount;
- // output every this.per items
- if (currentCount % this.per === 0) {
- logger.debug(output);
- this.emitProgressEvent();
- }
- // output last item
- else if (currentCount === exportProgress.totalCount) {
- logger.info(output);
- this.emitProgressEvent();
- }
- }
- /**
- * emit progress event
- * @param {ExportProgress} exportProgress
- */
- emitProgressEvent(exportProgress) {
- const { currentCount, totalCount, progressList } = this.currentExportingStatus;
- const data = {
- currentCount,
- totalCount,
- progressList,
- };
- // send event (in progress in global)
- this.adminEvent.emit('onProgressForExport', data);
- }
- /**
- * emit start zipping event
- */
- emitStartZippingEvent() {
- this.adminEvent.emit('onStartZippingForExport', {});
- }
- /**
- * emit terminate event
- * @param {object} zipFileStat added zip file status data
- */
- emitTerminateEvent(zipFileStat) {
- this.adminEvent.emit('onTerminateForExport', { addedZipFileStat: zipFileStat });
- }
- /**
- * zip files into one zip file
- *
- * @memberOf ExportService
- * @param {object|array<object>} configs object or array of object { from: "path to source file", as: "file name after unzipped" }
- * @return {string} absolute path to the zip file
- * @see https://www.archiverjs.com/#quick-start
- */
- async zipFiles(_configs) {
- const configs = toArrayIfNot(_configs);
- const appTitle = this.appService.getAppTitle();
- const timeStamp = (new Date()).getTime();
- const zipFile = path.join(this.baseDir, `${appTitle}-${timeStamp}.zip`);
- const archive = archiver('zip', {
- zlib: { level: this.zlibLevel },
- });
- // good practice to catch warnings (ie stat failures and other non-blocking errors)
- archive.on('warning', (err) => {
- if (err.code === 'ENOENT') logger.error(err);
- else throw err;
- });
- // good practice to catch this error explicitly
- archive.on('error', (err) => { throw err });
- for (const { from, as } of configs) {
- const input = fs.createReadStream(from);
- // append a file from stream
- archive.append(input, { name: as });
- }
- const output = fs.createWriteStream(zipFile);
- // pipe archive data to the file
- archive.pipe(output);
- // finalize the archive (ie we are done appending files but streams have to finish yet)
- // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
- archive.finalize();
- await streamToPromise(archive);
- logger.info(`zipped growi data into ${zipFile} (${archive.pointer()} bytes)`);
- // delete json files
- for (const { from } of configs) {
- fs.unlinkSync(from);
- }
- return zipFile;
- }
- }
- module.exports = ExportService;
|