@ruiapp/rapid-core 0.2.12 → 0.2.13

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.
package/dist/index.js CHANGED
@@ -6333,6 +6333,11 @@ async function appendFile(path, data) {
6333
6333
  });
6334
6334
  }
6335
6335
 
6336
+ function getFileBaseName(pathname) {
6337
+ const extName = path__default["default"].extname(pathname);
6338
+ return path__default["default"].basename(pathname, extName);
6339
+ }
6340
+
6336
6341
  const code$8 = "downloadDocument";
6337
6342
  async function handler$8(plugin, ctx, options) {
6338
6343
  const { server, applicationConfig, routerContext, input } = ctx;
@@ -6340,24 +6345,52 @@ async function handler$8(plugin, ctx, options) {
6340
6345
  const documentDataAccessor = ctx.server.getDataAccessor({
6341
6346
  singularCode: "ecm_document",
6342
6347
  });
6348
+ const revisionDataAccessor = ctx.server.getDataAccessor({
6349
+ singularCode: "ecm_revision",
6350
+ });
6343
6351
  const storageDataAccessor = ctx.server.getDataAccessor({
6344
6352
  singularCode: "ecm_storage_object",
6345
6353
  });
6346
- const document = await documentDataAccessor.findById(input.documentId);
6347
- if (!document) {
6348
- ctx.output = { error: new Error("Document not found.") };
6354
+ let storageObjectId = 0;
6355
+ let fileName;
6356
+ let { revisionId, documentId } = input;
6357
+ if (revisionId) {
6358
+ const revision = await revisionDataAccessor.findById(revisionId);
6359
+ if (!revision) {
6360
+ ctx.output = { error: new Error(`Revision with id "${revisionId}" was not found.`) };
6361
+ return;
6362
+ }
6363
+ storageObjectId = revision.storage_object_id;
6364
+ documentId = revision.document_id;
6365
+ const document = await documentDataAccessor.findById(documentId);
6366
+ if (!document) {
6367
+ ctx.output = { error: new Error(`Document with id "${documentId}" was not found.`) };
6368
+ return;
6369
+ }
6370
+ fileName = `${getFileBaseName(document.name)}${revision.ext_name}`;
6371
+ }
6372
+ else if (documentId) {
6373
+ const document = await documentDataAccessor.findById(documentId);
6374
+ if (!document) {
6375
+ ctx.output = { error: new Error(`Document with id "${documentId}" was not found.`) };
6376
+ return;
6377
+ }
6378
+ storageObjectId = document.storage_object_id;
6379
+ fileName = document.name;
6380
+ }
6381
+ else {
6382
+ ctx.output = { error: new Error(`Parameter "revisionId" or "documentId" must be provided.`) };
6349
6383
  return;
6350
6384
  }
6351
- const storageObject = await storageDataAccessor.findById(document.storage_object_id);
6385
+ const storageObject = await storageDataAccessor.findById(storageObjectId);
6352
6386
  if (!storageObject) {
6353
- ctx.output = { error: new Error("Storage object not found.") };
6387
+ ctx.output = { error: new Error(`Storage object with id "${storageObjectId}" was not found.`) };
6354
6388
  return;
6355
6389
  }
6356
6390
  const fileKey = storageObject.key;
6357
6391
  const filePathName = path__default["default"].join(server.config.localFileStoragePath, fileKey);
6358
- const attachmentFileName = document.name;
6359
6392
  response.body = await readFile(filePathName);
6360
- response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(attachmentFileName)}"`);
6393
+ response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(fileName)}"`);
6361
6394
  }
6362
6395
 
6363
6396
  var downloadDocumentActionHandler = /*#__PURE__*/Object.freeze({
@@ -0,0 +1,3 @@
1
+ export declare function getFileExtensionName(pathname: string): string;
2
+ export declare function getFileName(pathname: string): string;
3
+ export declare function getFileBaseName(pathname: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.2.12",
3
+ "version": "0.2.13",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -2,6 +2,7 @@ import path from "path";
2
2
  import { ActionHandlerContext } from "~/core/actionHandler";
3
3
  import { RapidPlugin } from "~/core/server";
4
4
  import { readFile } from "~/utilities/fsUtility";
5
+ import { getFileBaseName } from "~/utilities/pathUtility";
5
6
 
6
7
  export const code = "downloadDocument";
7
8
 
@@ -12,25 +13,53 @@ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, op
12
13
  const documentDataAccessor = ctx.server.getDataAccessor({
13
14
  singularCode: "ecm_document",
14
15
  });
16
+ const revisionDataAccessor = ctx.server.getDataAccessor({
17
+ singularCode: "ecm_revision",
18
+ });
15
19
  const storageDataAccessor = ctx.server.getDataAccessor({
16
20
  singularCode: "ecm_storage_object",
17
21
  });
18
22
 
19
- const document = await documentDataAccessor.findById(input.documentId);
20
- if (!document) {
21
- ctx.output = { error: new Error("Document not found.") };
23
+ let storageObjectId = 0;
24
+ let fileName: string;
25
+ let { revisionId, documentId } = input;
26
+ if (revisionId) {
27
+ const revision = await revisionDataAccessor.findById(revisionId);
28
+ if (!revision) {
29
+ ctx.output = { error: new Error(`Revision with id "${revisionId}" was not found.`) };
30
+ return;
31
+ }
32
+ storageObjectId = revision.storage_object_id;
33
+
34
+ documentId = revision.document_id;
35
+ const document = await documentDataAccessor.findById(documentId);
36
+ if (!document) {
37
+ ctx.output = { error: new Error(`Document with id "${documentId}" was not found.`) };
38
+ return;
39
+ }
40
+ fileName = `${getFileBaseName(document.name!)}${revision.ext_name}`;
41
+ } else if (documentId) {
42
+ const document = await documentDataAccessor.findById(documentId);
43
+ if (!document) {
44
+ ctx.output = { error: new Error(`Document with id "${documentId}" was not found.`) };
45
+ return;
46
+ }
47
+ storageObjectId = document.storage_object_id;
48
+ fileName = document.name;
49
+ } else {
50
+ ctx.output = { error: new Error(`Parameter "revisionId" or "documentId" must be provided.`) };
22
51
  return;
23
52
  }
24
- const storageObject = await storageDataAccessor.findById(document.storage_object_id);
53
+
54
+ const storageObject = await storageDataAccessor.findById(storageObjectId);
25
55
  if (!storageObject) {
26
- ctx.output = { error: new Error("Storage object not found.") };
56
+ ctx.output = { error: new Error(`Storage object with id "${storageObjectId}" was not found.`) };
27
57
  return;
28
58
  }
29
59
 
30
60
  const fileKey = storageObject.key;
31
61
  const filePathName = path.join(server.config.localFileStoragePath, fileKey);
32
- const attachmentFileName = document.name;
33
62
 
34
63
  response.body = await readFile(filePathName);
35
- response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(attachmentFileName)}"`);
64
+ response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(fileName)}"`);
36
65
  }
@@ -0,0 +1,14 @@
1
+ import path from "path";
2
+
3
+ export function getFileExtensionName(pathname: string) {
4
+ return path.extname(pathname);
5
+ }
6
+
7
+ export function getFileName(pathname: string) {
8
+ return path.basename(pathname);
9
+ }
10
+
11
+ export function getFileBaseName(pathname: string) {
12
+ const extName = path.extname(pathname);
13
+ return path.basename(pathname, extName);
14
+ }