| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import loggerFactory from '~/utils/logger';
- import S2sMessage from '../models/vo/s2s-message';
- import type { ConfigManager } from './config-manager';
- import type { S2sMessagingService } from './s2s-messaging/base';
- import type { S2sMessageHandlable } from './s2s-messaging/handlable';
- const logger = loggerFactory('growi:service:FileUploaderSwitch');
- class FileUploaderSwitch implements S2sMessageHandlable {
- crowi: any;
- configManager: ConfigManager;
- s2sMessagingService: S2sMessagingService;
- lastLoadedAt?: Date;
- constructor(crowi) {
- this.crowi = crowi;
- this.configManager = crowi.configManager;
- this.s2sMessagingService = crowi.s2sMessagingService;
- }
- /**
- * @inheritdoc
- */
- shouldHandleS2sMessage(s2sMessage) {
- const { eventName, updatedAt } = s2sMessage;
- if (eventName !== 'fileUploadServiceUpdated' || updatedAt == null) {
- return false;
- }
- return this.lastLoadedAt == null || this.lastLoadedAt < new Date(s2sMessage.updatedAt);
- }
- /**
- * @inheritdoc
- */
- async handleS2sMessage(s2sMessage) {
- const { configManager } = this;
- logger.info('Reset fileupload service by pubsub notification');
- await configManager.loadConfigs();
- await this.crowi.setUpFileUpload(true);
- }
- async publishUpdatedMessage() {
- const { s2sMessagingService } = this;
- if (s2sMessagingService != null) {
- const s2sMessage = new S2sMessage('fileUploadServiceUpdated', { updatedAt: new Date() });
- try {
- await s2sMessagingService.publish(s2sMessage);
- }
- catch (e) {
- logger.error('Failed to publish update message with S2sMessagingService: ', e.message);
- }
- }
- }
- }
- module.exports = FileUploaderSwitch;
|