export.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import fs from 'fs';
  2. import path from 'path';
  3. import { Readable, Transform } from 'stream';
  4. import archiver from 'archiver';
  5. import { toArrayIfNot } from '~/utils/array-utils';
  6. import loggerFactory from '~/utils/logger';
  7. import CollectionProgress from '../models/vo/collection-progress';
  8. import CollectionProgressingStatus from '../models/vo/collection-progressing-status';
  9. import AppService from './app';
  10. import ConfigLoader from './config-loader';
  11. import GrowiBridgeService from './growi-bridge';
  12. import { ZipFileStat } from './interfaces/export';
  13. const logger = loggerFactory('growi:services:ExportService'); // eslint-disable-line no-unused-vars
  14. const mongoose = require('mongoose');
  15. const streamToPromise = require('stream-to-promise');
  16. class ExportProgressingStatus extends CollectionProgressingStatus {
  17. async init() {
  18. // retrieve total document count from each collections
  19. const promises = this.progressList.map(async(collectionProgress) => {
  20. const collection = mongoose.connection.collection(collectionProgress.collectionName);
  21. collectionProgress.totalCount = await collection.count();
  22. });
  23. await Promise.all(promises);
  24. this.recalculateTotalCount();
  25. }
  26. }
  27. class ExportService {
  28. crowi: any;
  29. appService: AppService;
  30. growiBridgeService: GrowiBridgeService;
  31. getFile: (filename: string) => string;
  32. per = 100;
  33. zlibLevel = 9; // 0(min) - 9(max)
  34. currentProgressingStatus: ExportProgressingStatus | null;
  35. baseDir: string;
  36. adminEvent: any;
  37. constructor(crowi) {
  38. this.crowi = crowi;
  39. this.appService = crowi.appService;
  40. this.growiBridgeService = crowi.growiBridgeService;
  41. this.getFile = this.growiBridgeService.getFile.bind(this);
  42. this.baseDir = path.join(crowi.tmpDir, 'downloads');
  43. this.adminEvent = crowi.event('admin');
  44. this.currentProgressingStatus = null;
  45. }
  46. /**
  47. * parse all zip files in downloads dir
  48. *
  49. * @memberOf ExportService
  50. * @return {object} info for zip files and whether currentProgressingStatus exists
  51. */
  52. async getStatus() {
  53. const zipFiles = fs.readdirSync(this.baseDir).filter(file => path.extname(file) === '.zip');
  54. // process serially so as not to waste memory
  55. const zipFileStats: Array<ZipFileStat | null> = [];
  56. const parseZipFilePromises = zipFiles.map((file) => {
  57. const zipFile = this.getFile(file);
  58. return this.growiBridgeService.parseZipFile(zipFile);
  59. });
  60. for await (const stat of parseZipFilePromises) {
  61. zipFileStats.push(stat);
  62. }
  63. // filter null object (broken zip)
  64. const filtered = zipFileStats.filter(element => element != null);
  65. const isExporting = this.currentProgressingStatus != null;
  66. return {
  67. zipFileStats: filtered,
  68. isExporting,
  69. progressList: isExporting ? this.currentProgressingStatus?.progressList : null,
  70. };
  71. }
  72. /**
  73. * create meta.json
  74. *
  75. * @memberOf ExportService
  76. * @return {string} path to meta.json
  77. */
  78. async createMetaJson(): Promise<string> {
  79. const metaJson = path.join(this.baseDir, this.growiBridgeService.getMetaFileName());
  80. const writeStream = fs.createWriteStream(metaJson, { encoding: this.growiBridgeService.getEncoding() });
  81. const passwordSeed = this.crowi.env.PASSWORD_SEED || null;
  82. const metaData = {
  83. version: this.crowi.version,
  84. url: this.appService.getSiteUrl(),
  85. passwordSeed,
  86. exportedAt: new Date(),
  87. envVars: await ConfigLoader.getEnvVarsForDisplay(),
  88. };
  89. writeStream.write(JSON.stringify(metaData));
  90. writeStream.close();
  91. await streamToPromise(writeStream);
  92. return metaJson;
  93. }
  94. /**
  95. *
  96. * @param {ExportProgress} exportProgress
  97. * @return {Transform}
  98. */
  99. generateLogStream(exportProgress: CollectionProgress | undefined): Transform {
  100. const logProgress = this.logProgress.bind(this);
  101. let count = 0;
  102. return new Transform({
  103. transform(chunk, encoding, callback) {
  104. count++;
  105. logProgress(exportProgress, count);
  106. this.push(chunk);
  107. callback();
  108. },
  109. });
  110. }
  111. /**
  112. * insert beginning/ending brackets and comma separator for Json Array
  113. *
  114. * @memberOf ExportService
  115. * @return {Transform}
  116. */
  117. generateTransformStream(): Transform {
  118. let isFirst = true;
  119. const transformStream = new Transform({
  120. transform(chunk, encoding, callback) {
  121. // write beginning brace
  122. if (isFirst) {
  123. this.push('[');
  124. isFirst = false;
  125. }
  126. // write separator
  127. else {
  128. this.push(',');
  129. }
  130. this.push(chunk);
  131. callback();
  132. },
  133. final(callback) {
  134. // write beginning brace
  135. if (isFirst) {
  136. this.push('[');
  137. }
  138. // write ending brace
  139. this.push(']');
  140. callback();
  141. },
  142. });
  143. return transformStream;
  144. }
  145. /**
  146. * dump a mongodb collection into json
  147. *
  148. * @memberOf ExportService
  149. * @param {string} collectionName collection name
  150. * @return {string} path to zip file
  151. */
  152. async exportCollectionToJson(collectionName: string): Promise<string> {
  153. const collection = mongoose.connection.collection(collectionName);
  154. const nativeCursor = collection.find();
  155. const readStream = nativeCursor.stream({ transform: JSON.stringify });
  156. // get TransformStream
  157. const transformStream = this.generateTransformStream();
  158. // log configuration
  159. const exportProgress = this.currentProgressingStatus?.progressMap[collectionName];
  160. const logStream = this.generateLogStream(exportProgress);
  161. // create WritableStream
  162. const jsonFileToWrite = path.join(this.baseDir, `${collectionName}.json`);
  163. const writeStream = fs.createWriteStream(jsonFileToWrite, { encoding: this.growiBridgeService.getEncoding() });
  164. readStream
  165. .pipe(logStream)
  166. .pipe(transformStream)
  167. .pipe(writeStream);
  168. await streamToPromise(writeStream);
  169. return writeStream.path.toString();
  170. }
  171. /**
  172. * export multiple Collections into json and Zip
  173. *
  174. * @memberOf ExportService
  175. * @param {Array.<string>} collections array of collection name
  176. * @return {Array.<ZipFileStat>} info of zip file created
  177. */
  178. async exportCollectionsToZippedJson(collections: string[]): Promise<ZipFileStat | null> {
  179. const metaJson = await this.createMetaJson();
  180. // process serially so as not to waste memory
  181. const jsonFiles: string[] = [];
  182. const jsonFilesPromises = collections.map(collectionName => this.exportCollectionToJson(collectionName));
  183. for await (const jsonFile of jsonFilesPromises) {
  184. jsonFiles.push(jsonFile);
  185. }
  186. // send terminate event
  187. this.emitStartZippingEvent();
  188. // zip json
  189. const configs = jsonFiles.map((jsonFile) => { return { from: jsonFile, as: path.basename(jsonFile) } });
  190. // add meta.json in zip
  191. configs.push({ from: metaJson, as: path.basename(metaJson) });
  192. // exec zip
  193. const zipFile = await this.zipFiles(configs);
  194. // get stats for the zip file
  195. const addedZipFileStat = await this.growiBridgeService.parseZipFile(zipFile);
  196. // send terminate event
  197. this.emitTerminateEvent(addedZipFileStat);
  198. return addedZipFileStat;
  199. // TODO: remove broken zip file
  200. }
  201. async export(collections: string[]): Promise<ZipFileStat | null> {
  202. if (this.currentProgressingStatus != null) {
  203. throw new Error('There is an exporting process running.');
  204. }
  205. this.currentProgressingStatus = new ExportProgressingStatus(collections);
  206. await this.currentProgressingStatus.init();
  207. let zipFileStat: ZipFileStat | null;
  208. try {
  209. zipFileStat = await this.exportCollectionsToZippedJson(collections);
  210. }
  211. finally {
  212. this.currentProgressingStatus = null;
  213. }
  214. return zipFileStat;
  215. }
  216. /**
  217. * log export progress
  218. *
  219. * @memberOf ExportService
  220. *
  221. * @param {CollectionProgress} collectionProgress
  222. * @param {number} currentCount number of items exported
  223. */
  224. logProgress(collectionProgress: CollectionProgress | undefined, currentCount: number): void {
  225. if (collectionProgress == null) return;
  226. const output = `${collectionProgress.collectionName}: ${currentCount}/${collectionProgress.totalCount} written`;
  227. // update exportProgress.currentCount
  228. collectionProgress.currentCount = currentCount;
  229. // output every this.per items
  230. if (currentCount % this.per === 0) {
  231. logger.debug(output);
  232. this.emitProgressEvent();
  233. }
  234. // output last item
  235. else if (currentCount === collectionProgress.totalCount) {
  236. logger.info(output);
  237. this.emitProgressEvent();
  238. }
  239. }
  240. /**
  241. * emit progress event
  242. */
  243. emitProgressEvent(): void {
  244. const data = {
  245. currentCount: this.currentProgressingStatus?.currentCount,
  246. totalCount: this.currentProgressingStatus?.totalCount,
  247. progressList: this.currentProgressingStatus?.progressList,
  248. };
  249. // send event (in progress in global)
  250. this.adminEvent.emit('onProgressForExport', data);
  251. }
  252. /**
  253. * emit start zipping event
  254. */
  255. emitStartZippingEvent(): void {
  256. this.adminEvent.emit('onStartZippingForExport', {});
  257. }
  258. /**
  259. * emit terminate event
  260. * @param {object} zipFileStat added zip file status data
  261. */
  262. emitTerminateEvent(zipFileStat: ZipFileStat | null): void {
  263. this.adminEvent.emit('onTerminateForExport', { addedZipFileStat: zipFileStat });
  264. }
  265. /**
  266. * zip files into one zip file
  267. *
  268. * @memberOf ExportService
  269. * @param {object|array<object>} configs object or array of object { from: "path to source file", as: "file name after unzipped" }
  270. * @return {string} absolute path to the zip file
  271. * @see https://www.archiverjs.com/#quick-start
  272. */
  273. async zipFiles(_configs: {from: string, as: string}[]): Promise<string> {
  274. const configs = toArrayIfNot(_configs);
  275. const appTitle = this.appService.getAppTitle();
  276. const timeStamp = (new Date()).getTime();
  277. const zipFile = path.join(this.baseDir, `${appTitle}-${timeStamp}.growi.zip`);
  278. const archive = archiver('zip', {
  279. zlib: { level: this.zlibLevel },
  280. });
  281. // good practice to catch warnings (ie stat failures and other non-blocking errors)
  282. archive.on('warning', (err) => {
  283. if (err.code === 'ENOENT') logger.error(err);
  284. else throw err;
  285. });
  286. // good practice to catch this error explicitly
  287. archive.on('error', (err) => { throw err });
  288. for (const { from, as } of configs) {
  289. const input = fs.createReadStream(from);
  290. // append a file from stream
  291. archive.append(input, { name: as });
  292. }
  293. const output = fs.createWriteStream(zipFile);
  294. // pipe archive data to the file
  295. archive.pipe(output);
  296. // finalize the archive (ie we are done appending files but streams have to finish yet)
  297. // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
  298. archive.finalize();
  299. await streamToPromise(archive);
  300. logger.info(`zipped GROWI data into ${zipFile} (${archive.pointer()} bytes)`);
  301. // delete json files
  302. for (const { from } of configs) {
  303. fs.unlinkSync(from);
  304. }
  305. return zipFile;
  306. }
  307. getReadStreamFromRevision(revision, format): Readable {
  308. const data = revision.body;
  309. const readable = new Readable();
  310. readable._read = () => {};
  311. readable.push(data);
  312. readable.push(null);
  313. return readable;
  314. }
  315. }
  316. // eslint-disable-next-line import/no-mutable-exports
  317. export let exportService: ExportService | undefined; // singleton instance
  318. export default function instanciate(crowi: any): void {
  319. exportService = new ExportService(crowi);
  320. }