pdf.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { BodyParams } from '@tsed/common';
  2. import { Controller } from '@tsed/di';
  3. import { BadRequest, InternalServerError } from '@tsed/exceptions';
  4. import { Logger } from '@tsed/logger';
  5. import {
  6. Description,
  7. Enum,
  8. Integer,
  9. Post,
  10. Required,
  11. Returns,
  12. } from '@tsed/schema';
  13. import PdfConvertService, {
  14. JobStatus,
  15. JobStatusSharedWithGrowi,
  16. } from '../service/pdf-convert.js';
  17. @Controller('/pdf')
  18. class PdfCtrl {
  19. constructor(
  20. private readonly pdfConvertService: PdfConvertService,
  21. private readonly logger: Logger,
  22. ) {}
  23. @Post('/sync-job')
  24. @(
  25. Returns(202)
  26. .ContentType('application/json')
  27. .Schema({
  28. type: 'object',
  29. properties: {
  30. status: { type: 'string', enum: Object.values(JobStatus) },
  31. },
  32. required: ['status'],
  33. })
  34. )
  35. @Returns(500)
  36. @Description(`
  37. Sync job pdf convert status with GROWI.
  38. Register or update job inside pdf-converter with given jobId, expirationDate, and status.
  39. Return resulting status of job to GROWI.
  40. `)
  41. async syncJobStatus(
  42. @Required() @BodyParams('jobId') jobId: string,
  43. @Required() @BodyParams('expirationDate') expirationDateStr: string,
  44. @Required()
  45. @BodyParams('status')
  46. @Enum(Object.values(JobStatusSharedWithGrowi))
  47. growiJobStatus: JobStatusSharedWithGrowi,
  48. @Integer() @BodyParams('appId') appId?: number, // prevent path traversal attack
  49. ): Promise<{ status: JobStatus } | undefined> {
  50. // prevent path traversal attack
  51. if (!/^[a-f\d]{24}$/i.test(jobId)) {
  52. throw new BadRequest('jobId must be a valid MongoDB ObjectId');
  53. }
  54. const expirationDate = new Date(expirationDateStr);
  55. try {
  56. await this.pdfConvertService.registerOrUpdateJob(
  57. jobId,
  58. expirationDate,
  59. growiJobStatus,
  60. appId,
  61. );
  62. const status = this.pdfConvertService.getJobStatus(jobId); // get status before cleanup
  63. this.pdfConvertService.cleanUpJobList();
  64. return { status };
  65. } catch (err) {
  66. this.logger.error('Failed to register or update job', err);
  67. if (err instanceof Error) {
  68. throw new InternalServerError(err.message);
  69. }
  70. }
  71. }
  72. }
  73. export default PdfCtrl;