plugin-file-preview-auth 1.2.4 → 1.2.6

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.
@@ -188,45 +188,44 @@ class PluginFilePreviewAuthServer extends import_server.Plugin {
188
188
  await this.assertCanAccessAttachment(ctx, attachment);
189
189
  }
190
190
  try {
191
+ let matchedKey = null;
191
192
  const rawStorageId = attachment.storageId || getAttachmentValue(attachment, "storageId");
192
193
  if (rawStorageId) {
193
194
  const strId = String(rawStorageId);
194
- let matchedKey = null;
195
195
  for (const key of fileManager.storagesCache.keys()) {
196
196
  if (String(key) === strId) {
197
197
  matchedKey = key;
198
198
  break;
199
199
  }
200
200
  }
201
- if (matchedKey !== null) {
202
- if (typeof attachment.set === "function") {
203
- attachment.set("storageId", matchedKey);
204
- }
205
- attachment.storageId = matchedKey;
206
- }
207
201
  }
208
- const storageModel = getStorageFromCache(fileManager.storagesCache, attachment.storageId);
209
- if (storageModel && (storageModel.type === "s3" || storageModel.type === "aws-s3")) {
202
+ const attachmentObj = typeof attachment.toJSON === "function" ? attachment.toJSON() : { ...attachment };
203
+ if (matchedKey !== null) {
204
+ attachmentObj.storageId = matchedKey;
205
+ }
206
+ const storageModel = getStorageFromCache(fileManager.storagesCache, attachmentObj.storageId);
207
+ if (storageModel && (storageModel.type === "s3" || storageModel.type === "aws-s3" || storageModel.type === "s3-private")) {
210
208
  const StorageTypeClass = fileManager.storageTypes.get(storageModel.type);
211
209
  const storageInstance = new StorageTypeClass(storageModel);
212
- if (storageInstance.client) {
210
+ const s3Client = storageInstance.client || (typeof storageInstance.getS3Client === "function" ? storageInstance.getS3Client() : null);
211
+ if (s3Client) {
213
212
  const { GetObjectCommand } = require("@aws-sdk/client-s3");
214
- const key = storageInstance.getFileKey(attachment);
213
+ const key = storageInstance.getFileKey(attachmentObj);
215
214
  const getCommand = new GetObjectCommand({
216
215
  Bucket: storageModel.options.bucket,
217
216
  Key: key
218
217
  });
219
- const response = await storageInstance.client.send(getCommand);
220
- ctx.type = response.ContentType || attachment.mimetype || "application/octet-stream";
221
- ctx.attachment(attachment.filename);
218
+ const response = await s3Client.send(getCommand);
219
+ ctx.type = response.ContentType || attachmentObj.mimetype || "application/octet-stream";
220
+ ctx.attachment(attachmentObj.filename);
222
221
  ctx.body = response.Body;
223
222
  await next();
224
223
  return;
225
224
  }
226
225
  }
227
- const { stream, contentType } = await fileManager.getFileStream(attachment);
228
- ctx.type = contentType || attachment.mimetype || "application/octet-stream";
229
- ctx.attachment(attachment.filename);
226
+ const { stream, contentType } = await fileManager.getFileStream(attachmentObj);
227
+ ctx.type = contentType || attachmentObj.mimetype || "application/octet-stream";
228
+ ctx.attachment(attachmentObj.filename);
230
229
  ctx.body = stream;
231
230
  } catch (err) {
232
231
  this.log.error(`[FilePreviewAuth] Error fetching stream for URL ${url}: ${err.message}`);
@@ -450,24 +449,22 @@ class PluginFilePreviewAuthServer extends import_server.Plugin {
450
449
  if (!(fileManager == null ? void 0 : fileManager.getFileStream)) {
451
450
  return "";
452
451
  }
452
+ let matchedKey = null;
453
453
  const rawStorageId = attachment.storageId || getAttachmentValue(attachment, "storageId");
454
454
  if (rawStorageId) {
455
455
  const strId = String(rawStorageId);
456
- let matchedKey = null;
457
456
  for (const key of fileManager.storagesCache.keys()) {
458
457
  if (String(key) === strId) {
459
458
  matchedKey = key;
460
459
  break;
461
460
  }
462
461
  }
463
- if (matchedKey !== null) {
464
- if (typeof attachment.set === "function") {
465
- attachment.set("storageId", matchedKey);
466
- }
467
- attachment.storageId = matchedKey;
468
- }
469
462
  }
470
- const { stream } = await fileManager.getFileStream(attachment);
463
+ const attachmentObj = typeof attachment.toJSON === "function" ? attachment.toJSON() : { ...attachment };
464
+ if (matchedKey !== null) {
465
+ attachmentObj.storageId = matchedKey;
466
+ }
467
+ const { stream } = await fileManager.getFileStream(attachmentObj);
471
468
  const chunks = [];
472
469
  for await (const chunk of stream) {
473
470
  chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "description": "Preview PDF, image, and text files with Bearer token authentication via blob URLs.",
7
7
  "description.vi-VN": "Xem trước file PDF, hình ảnh và văn bản với xác thực Bearer token qua blob URL.",
8
8
  "description.zh-CN": "通过 Bearer 令牌认证和 Blob URL 预览 PDF、图片和文本文件。",
9
- "version": "1.2.4",
9
+ "version": "1.2.6",
10
10
  "main": "dist/server/index.js",
11
11
  "files": [
12
12
  "dist",