@airoom/nextmin-node 1.4.0 → 1.4.2

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.
@@ -0,0 +1,16 @@
1
+ import { FileStorageAdapter, UploadResult, UploadPayload, FileProvider } from './FileStorageAdapter';
2
+ export interface LocalFileStorageOptions {
3
+ uploadDir: string;
4
+ publicUrlPrefix: string;
5
+ }
6
+ export declare class LocalFileStorageAdapter implements FileStorageAdapter {
7
+ readonly name: FileProvider;
8
+ private uploadDir;
9
+ private publicUrlPrefix;
10
+ constructor(options: LocalFileStorageOptions);
11
+ upload(input: UploadPayload): Promise<UploadResult>;
12
+ delete(key: string): Promise<{
13
+ deleted: boolean;
14
+ }>;
15
+ getPublicUrl(key: string): string | null;
16
+ }
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.LocalFileStorageAdapter = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const stream_1 = require("stream");
10
+ const filename_1 = require("./filename");
11
+ const crypto_1 = __importDefault(require("crypto"));
12
+ class LocalFileStorageAdapter {
13
+ constructor(options) {
14
+ this.name = 'local';
15
+ this.uploadDir = options.uploadDir;
16
+ this.publicUrlPrefix = options.publicUrlPrefix.replace(/\/$/, '');
17
+ if (!fs_1.default.existsSync(this.uploadDir)) {
18
+ fs_1.default.mkdirSync(this.uploadDir, { recursive: true });
19
+ }
20
+ }
21
+ async upload(input) {
22
+ const originalName = input.originalFilename || 'unknown-file';
23
+ const randomId = crypto_1.default.randomBytes(4).toString('hex');
24
+ const uniqueName = input.key || (0, filename_1.filenameWithRandomId)(originalName, randomId);
25
+ const filePath = path_1.default.join(this.uploadDir, uniqueName);
26
+ const writeStream = fs_1.default.createWriteStream(filePath);
27
+ // Convert body to stream if it is a Buffer
28
+ const readStream = new stream_1.Readable();
29
+ readStream.push(input.body);
30
+ readStream.push(null);
31
+ await new Promise((resolve, reject) => {
32
+ readStream.pipe(writeStream);
33
+ readStream.on('error', reject);
34
+ writeStream.on('finish', () => resolve());
35
+ writeStream.on('error', reject);
36
+ });
37
+ return {
38
+ provider: 'local',
39
+ key: uniqueName,
40
+ url: `${this.publicUrlPrefix}/${uniqueName}`,
41
+ bucket: this.uploadDir,
42
+ size: input.body.byteLength,
43
+ contentType: input.contentType,
44
+ };
45
+ }
46
+ async delete(key) {
47
+ const filePath = path_1.default.join(this.uploadDir, key);
48
+ if (fs_1.default.existsSync(filePath)) {
49
+ await fs_1.default.promises.unlink(filePath);
50
+ return { deleted: true };
51
+ }
52
+ return { deleted: false };
53
+ }
54
+ getPublicUrl(key) {
55
+ return `${this.publicUrlPrefix}/${key}`;
56
+ }
57
+ }
58
+ exports.LocalFileStorageAdapter = LocalFileStorageAdapter;
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export { MongoAdapter } from './database/MongoAdapter';
3
3
  export { generateApiKey } from './utils/apiKey';
4
4
  export * from './files/FileStorageAdapter';
5
5
  export * from './files/S3FileStorageAdapter';
6
+ export * from './files/LocalFileStorageAdapter';
6
7
  export * from './files/filename';
7
8
  /**
8
9
  * Compose the public router:
package/dist/index.js CHANGED
@@ -28,6 +28,7 @@ var apiKey_1 = require("./utils/apiKey");
28
28
  Object.defineProperty(exports, "generateApiKey", { enumerable: true, get: function () { return apiKey_1.generateApiKey; } });
29
29
  __exportStar(require("./files/FileStorageAdapter"), exports);
30
30
  __exportStar(require("./files/S3FileStorageAdapter"), exports);
31
+ __exportStar(require("./files/LocalFileStorageAdapter"), exports);
31
32
  __exportStar(require("./files/filename"), exports);
32
33
  function isMultipart(req) {
33
34
  const ct = (req.headers['content-type'] || '').toLowerCase();
@@ -16,6 +16,7 @@ export interface Attributes {
16
16
  unique?: boolean;
17
17
  sensitive?: boolean;
18
18
  writeOnly?: boolean;
19
+ rich?: boolean;
19
20
  }
20
21
  export interface Schema {
21
22
  modelName: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@airoom/nextmin-node",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -42,4 +42,4 @@
42
42
  "publishConfig": {
43
43
  "access": "public"
44
44
  }
45
- }
45
+ }