plugin.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { execSync } from 'child_process';
  2. import fs from 'fs';
  3. import path from 'path';
  4. import streamToPromise from 'stream-to-promise';
  5. import unzipper from 'unzipper';
  6. import { GrowiPlugin, GrowiPluginOrigin } from '~/interfaces/plugin';
  7. import loggerFactory from '~/utils/logger';
  8. import { resolveFromRoot } from '~/utils/project-dir-utils';
  9. // eslint-disable-next-line import/no-cycle
  10. import Crowi from '../crowi';
  11. const logger = loggerFactory('growi:plugins:plugin-utils');
  12. const pluginStoringPath = resolveFromRoot('tmp/plugins');
  13. export class PluginService {
  14. crowi: any;
  15. growiBridgeService: any;
  16. baseDir: any;
  17. getFile:any;
  18. constructor(crowi) {
  19. this.crowi = crowi;
  20. this.growiBridgeService = crowi.growiBridgeService;
  21. this.baseDir = path.join(crowi.tmpDir, 'plugins');
  22. this.getFile = this.growiBridgeService.getFile.bind(this);
  23. }
  24. async install(crowi: Crowi, origin: GrowiPluginOrigin): Promise<void> {
  25. // download
  26. const ghUrl = origin.url;
  27. const downloadDir = path.join(process.cwd(), 'tmp/plugins/');
  28. try {
  29. await this.downloadZipFile(`${ghUrl}/archive/refs/heads/master.zip`, downloadDir);
  30. }
  31. catch (err) {
  32. // TODO: error handling
  33. }
  34. // TODO: detect plugins
  35. // TODO: save documents
  36. return;
  37. }
  38. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  39. static async detectPlugins(origin: GrowiPluginOrigin, installedPath: string, parentPackageJson?: any): Promise<GrowiPlugin[]> {
  40. const packageJsonPath = path.resolve(pluginStoringPath, installedPath, 'package.json');
  41. const packageJson = await import(packageJsonPath);
  42. const { growiPlugin } = packageJson;
  43. const {
  44. name: packageName, description: packageDesc, author: packageAuthor,
  45. } = parentPackageJson ?? packageJson;
  46. if (growiPlugin == null) {
  47. throw new Error('This package does not include \'growiPlugin\' section.');
  48. }
  49. // detect sub plugins for monorepo
  50. if (growiPlugin.isMonorepo && growiPlugin.packages != null) {
  51. const plugins = await Promise.all(
  52. growiPlugin.packages.map(async(subPackagePath) => {
  53. const subPackageInstalledPath = path.join(installedPath, subPackagePath);
  54. return this.detectPlugins(origin, subPackageInstalledPath, packageJson);
  55. }),
  56. );
  57. return plugins.flat();
  58. }
  59. if (growiPlugin.types == null) {
  60. throw new Error('\'growiPlugin\' section must have a \'types\' property.');
  61. }
  62. const plugin = {
  63. isEnabled: true,
  64. installedPath,
  65. origin,
  66. meta: {
  67. name: growiPlugin.name ?? packageName,
  68. desc: growiPlugin.desc ?? packageDesc,
  69. author: growiPlugin.author ?? packageAuthor,
  70. types: growiPlugin.types,
  71. },
  72. };
  73. logger.info('Plugin detected => ', plugin);
  74. return [plugin];
  75. }
  76. async listPlugins(): Promise<GrowiPlugin[]> {
  77. return [];
  78. }
  79. sleep(waitMsec) {
  80. const startMsec = new Date();
  81. while (new Date() - startMsec < waitMsec);
  82. }
  83. async downloadZipFile(ghUrl: string, filePath:string): Promise<void> {
  84. console.log(`rm ${filePath}master.zip`);
  85. const stdout1 = execSync(`wget ${ghUrl} -O ${filePath}master.zip`);
  86. console.log(`wget ${ghUrl} -O ${filePath}master.zip`);
  87. console.log(`unzip ${filePath}master.zip -d ${filePath}`);
  88. this.sleep(5000);
  89. const stdout2 = execSync(`unzip ${filePath}master.zip -d ${filePath}`);
  90. console.log(`unzip ${filePath}master.zip -d ${filePath}`);
  91. const stdout3 = execSync(`rm ${filePath}master.zip`);
  92. // try {
  93. // const zipFile = await this.getFile('master.zip');
  94. // // await this.unzip('/workspace/growi/packages/app/tmp/plugins/master.zip');
  95. // }
  96. // catch (err) {
  97. // console.log(err);
  98. // }
  99. return;
  100. }
  101. /**
  102. * extract a zip file
  103. *
  104. * @memberOf ImportService
  105. * @param {string} zipFile absolute path to zip file
  106. * @return {Array.<string>} array of absolute paths to extracted files
  107. */
  108. async unzip(zipFile) {
  109. // const stream = fs.createReadStream(zipFile).pipe(unzipper.Extract({ path: '/workspace/growi/packages/app/tmp/plugins/master' }));
  110. // try {
  111. // await streamToPromise(stream);
  112. // }
  113. // catch (err) {
  114. // console.log('err', err);
  115. // }
  116. const readStream = fs.createReadStream(zipFile);
  117. const unzipStream = readStream.pipe(unzipper.Parse());
  118. const files: any = [];
  119. unzipStream.on('entry', async(entry) => {
  120. const fileName = entry.path;
  121. // https://regex101.com/r/mD4eZs/6
  122. // prevent from unexpecting attack doing unzip file (path traversal attack)
  123. // FOR EXAMPLE
  124. // ../../src/server/views/admin/markdown.html
  125. if (fileName.match(/(\.\.\/|\.\.\\)/)) {
  126. logger.error('File path is not appropriate.', fileName);
  127. return;
  128. }
  129. if (fileName === this.growiBridgeService.getMetaFileName()) {
  130. // skip meta.json
  131. entry.autodrain();
  132. }
  133. else {
  134. const jsonFile = path.join(this.baseDir, fileName);
  135. const writeStream = fs.createWriteStream(jsonFile, { encoding: this.growiBridgeService.getEncoding() });
  136. entry.pipe(writeStream);
  137. files.push(jsonFile);
  138. }
  139. });
  140. await streamToPromise(unzipStream);
  141. return files;
  142. }
  143. }