@ruiapp/rapid-core 0.2.12 → 0.2.14

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
@@ -2548,11 +2548,16 @@ async function findEntities(server, dataAccessor, options) {
2548
2548
  return entities;
2549
2549
  }
2550
2550
  async function findEntity(server, dataAccessor, options) {
2551
- const entities = await findEntities(server, dataAccessor, options);
2551
+ const entities = await findEntities(server, dataAccessor, {
2552
+ ...options,
2553
+ ...{
2554
+ limit: 1,
2555
+ },
2556
+ });
2552
2557
  return lodash.first(entities);
2553
2558
  }
2554
2559
  async function findById(server, dataAccessor, options) {
2555
- const { id, properties, keepNonPropertyFields, routeContext } = options;
2560
+ const { id, properties, relations, keepNonPropertyFields, routeContext } = options;
2556
2561
  return await findEntity(server, dataAccessor, {
2557
2562
  filters: [
2558
2563
  {
@@ -2562,6 +2567,7 @@ async function findById(server, dataAccessor, options) {
2562
2567
  },
2563
2568
  ],
2564
2569
  properties,
2570
+ relations,
2565
2571
  keepNonPropertyFields,
2566
2572
  routeContext,
2567
2573
  });
@@ -6333,6 +6339,11 @@ async function appendFile(path, data) {
6333
6339
  });
6334
6340
  }
6335
6341
 
6342
+ function getFileBaseName(pathname) {
6343
+ const extName = path__default["default"].extname(pathname);
6344
+ return path__default["default"].basename(pathname, extName);
6345
+ }
6346
+
6336
6347
  const code$8 = "downloadDocument";
6337
6348
  async function handler$8(plugin, ctx, options) {
6338
6349
  const { server, applicationConfig, routerContext, input } = ctx;
@@ -6340,24 +6351,52 @@ async function handler$8(plugin, ctx, options) {
6340
6351
  const documentDataAccessor = ctx.server.getDataAccessor({
6341
6352
  singularCode: "ecm_document",
6342
6353
  });
6354
+ const revisionDataAccessor = ctx.server.getDataAccessor({
6355
+ singularCode: "ecm_revision",
6356
+ });
6343
6357
  const storageDataAccessor = ctx.server.getDataAccessor({
6344
6358
  singularCode: "ecm_storage_object",
6345
6359
  });
6346
- const document = await documentDataAccessor.findById(input.documentId);
6347
- if (!document) {
6348
- ctx.output = { error: new Error("Document not found.") };
6360
+ let storageObjectId = 0;
6361
+ let fileName;
6362
+ let { revisionId, documentId } = input;
6363
+ if (revisionId) {
6364
+ const revision = await revisionDataAccessor.findById(revisionId);
6365
+ if (!revision) {
6366
+ ctx.output = { error: new Error(`Revision with id "${revisionId}" was not found.`) };
6367
+ return;
6368
+ }
6369
+ storageObjectId = revision.storage_object_id;
6370
+ documentId = revision.document_id;
6371
+ const document = await documentDataAccessor.findById(documentId);
6372
+ if (!document) {
6373
+ ctx.output = { error: new Error(`Document with id "${documentId}" was not found.`) };
6374
+ return;
6375
+ }
6376
+ fileName = `${getFileBaseName(document.name)}${revision.ext_name}`;
6377
+ }
6378
+ else if (documentId) {
6379
+ const document = await documentDataAccessor.findById(documentId);
6380
+ if (!document) {
6381
+ ctx.output = { error: new Error(`Document with id "${documentId}" was not found.`) };
6382
+ return;
6383
+ }
6384
+ storageObjectId = document.storage_object_id;
6385
+ fileName = document.name;
6386
+ }
6387
+ else {
6388
+ ctx.output = { error: new Error(`Parameter "revisionId" or "documentId" must be provided.`) };
6349
6389
  return;
6350
6390
  }
6351
- const storageObject = await storageDataAccessor.findById(document.storage_object_id);
6391
+ const storageObject = await storageDataAccessor.findById(storageObjectId);
6352
6392
  if (!storageObject) {
6353
- ctx.output = { error: new Error("Storage object not found.") };
6393
+ ctx.output = { error: new Error(`Storage object with id "${storageObjectId}" was not found.`) };
6354
6394
  return;
6355
6395
  }
6356
6396
  const fileKey = storageObject.key;
6357
6397
  const filePathName = path__default["default"].join(server.config.localFileStoragePath, fileKey);
6358
- const attachmentFileName = document.name;
6359
6398
  response.body = await readFile(filePathName);
6360
- response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(attachmentFileName)}"`);
6399
+ response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(fileName)}"`);
6361
6400
  }
6362
6401
 
6363
6402
  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.14",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -347,12 +347,17 @@ async function findEntities(server: IRpdServer, dataAccessor: IRpdDataAccessor,
347
347
  }
348
348
 
349
349
  async function findEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor, options: FindEntityOptions) {
350
- const entities = await findEntities(server, dataAccessor, options);
350
+ const entities = await findEntities(server, dataAccessor, {
351
+ ...options,
352
+ ...{
353
+ limit: 1,
354
+ },
355
+ });
351
356
  return first(entities);
352
357
  }
353
358
 
354
359
  async function findById(server: IRpdServer, dataAccessor: IRpdDataAccessor, options: FindEntityByIdOptions): Promise<any> {
355
- const { id, properties, keepNonPropertyFields, routeContext } = options;
360
+ const { id, properties, relations, keepNonPropertyFields, routeContext } = options;
356
361
  return await findEntity(server, dataAccessor, {
357
362
  filters: [
358
363
  {
@@ -362,6 +367,7 @@ async function findById(server: IRpdServer, dataAccessor: IRpdDataAccessor, opti
362
367
  },
363
368
  ],
364
369
  properties,
370
+ relations,
365
371
  keepNonPropertyFields,
366
372
  routeContext,
367
373
  });
@@ -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
+ }