plugin-file-preview-auth 1.3.9 → 1.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,9 @@
1
1
  import { TesseractRunner } from './tesseract-runner';
2
2
  import path from 'path';
3
3
 
4
+ export const WORKER_JOB_FILE_PREVIEW_OCR_PROCESS = 'file-preview-auth:ocr';
5
+ export const FILE_PREVIEW_OCR_QUEUE_REDIS_KEY = 'file-preview-auth.ocr.queue';
6
+
4
7
  export class TesseractWorker {
5
8
  private app: any;
6
9
  private db: any;
@@ -8,7 +11,7 @@ export class TesseractWorker {
8
11
  private runner: TesseractRunner;
9
12
  private isRunning = false;
10
13
  private pollTimer: NodeJS.Timeout | null = null;
11
- private redisKey = 'file-preview-auth.ocr.queue';
14
+ private redisKey = FILE_PREVIEW_OCR_QUEUE_REDIS_KEY;
12
15
 
13
16
  constructor(app: any) {
14
17
  this.app = app;
@@ -7,14 +7,18 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
- import { Plugin } from '@nocobase/server';
10
+ import { Plugin, type Application } from '@nocobase/server';
11
11
  import { koaMulter as multer } from '@nocobase/utils';
12
12
  import { ExcelParserHandler } from './excel-parser-handler';
13
13
  import { readFile, unlink } from 'fs/promises';
14
14
  import os from 'os';
15
15
  import path from 'path';
16
16
  import { col } from 'sequelize';
17
- import { TesseractWorker } from './ocr/tesseract-worker';
17
+ import {
18
+ FILE_PREVIEW_OCR_QUEUE_REDIS_KEY,
19
+ TesseractWorker,
20
+ WORKER_JOB_FILE_PREVIEW_OCR_PROCESS,
21
+ } from './ocr/tesseract-worker';
18
22
 
19
23
  const FILE_PREVIEW_WORK_CONTEXT_TYPE = 'file-preview';
20
24
  const MAX_AI_CONTEXT_CHARS = 50000;
@@ -39,8 +43,10 @@ export class PluginFilePreviewAuthServer extends Plugin {
39
43
 
40
44
  this.app.on('afterStart', async () => {
41
45
  await this.disableBuiltinOfficePreviewer();
42
- if (this.ocrWorker) {
46
+ if (this.ocrWorker && isFilePreviewOcrWorker(this.app)) {
43
47
  await this.ocrWorker.start();
48
+ } else {
49
+ this.log.debug('[FilePreviewAuth] OCR worker disabled on this node by WORKER_MODE.');
44
50
  }
45
51
  });
46
52
  }
@@ -380,9 +386,23 @@ export class PluginFilePreviewAuthServer extends Plugin {
380
386
  ctx.attachment(attachmentObj.filename);
381
387
  ctx.body = stream;
382
388
  // S3 Private Bucket Handler
383
- } catch (err) {
389
+ } catch (err: any) {
384
390
  this.log.error(`[FilePreviewAuth] Error fetching stream for URL ${url}: ${err.message}`);
385
- ctx.throw(500, 'Failed to fetch the file from storage');
391
+ try {
392
+ require('fs').writeFileSync(
393
+ require('path').join(process.cwd(), 'preview_error.log'),
394
+ `Error fetching stream for URL ${url}:\n` +
395
+ `Time: ${new Date().toISOString()}\n` +
396
+ `Message: ${err.message}\n` +
397
+ `Stack: ${err.stack}\n` +
398
+ `Attachment: ${JSON.stringify(attachment, null, 2)}\n` +
399
+ `AttachmentObj: ${JSON.stringify(attachmentObj, null, 2)}\n` +
400
+ `StorageModel: ${JSON.stringify(storageModel, null, 2)}\n`,
401
+ );
402
+ } catch (fsErr: any) {
403
+ this.log.error(`[FilePreviewAuth] Failed to write preview_error.log: ${fsErr.message}`);
404
+ }
405
+ ctx.throw(500, `Failed to fetch the file from storage: ${err.message}`);
386
406
  }
387
407
 
388
408
  await next();
@@ -1187,6 +1207,24 @@ export class PluginFilePreviewAuthServer extends Plugin {
1187
1207
 
1188
1208
  export default PluginFilePreviewAuthServer;
1189
1209
 
1210
+ function isFilePreviewOcrWorker(app: Application) {
1211
+ return app.serving(WORKER_JOB_FILE_PREVIEW_OCR_PROCESS) || workerModeServesFilePreviewOcr();
1212
+ }
1213
+
1214
+ function workerModeServesFilePreviewOcr() {
1215
+ const workerModes = String(process.env.WORKER_MODE || '')
1216
+ .split(',')
1217
+ .map((mode) => mode.trim())
1218
+ .filter(Boolean);
1219
+
1220
+ return workerModes.some((mode) => {
1221
+ if (mode === '*' || mode === 'worker' || mode === 'task' || mode === WORKER_JOB_FILE_PREVIEW_OCR_PROCESS) {
1222
+ return true;
1223
+ }
1224
+ return mode === FILE_PREVIEW_OCR_QUEUE_REDIS_KEY;
1225
+ });
1226
+ }
1227
+
1190
1228
  function safeDebugJson(value: unknown): string {
1191
1229
  try {
1192
1230
  return JSON.stringify(value, (_key, item) => (typeof item === 'bigint' ? item.toString() : item));