@ainias42/typeorm-helper 0.0.7 → 0.0.8
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/DefaultSubscriber.js +5 -3
- package/dist/DefaultSubscriber.js.map +1 -1
- package/dist/decorators/FileColumn/FileColumn.js +2 -2
- package/dist/decorators/FileColumn/FileColumn.js.map +1 -1
- package/dist/decorators/FileColumn/FileType.d.ts +1 -1
- package/dist/decorators/FileColumn/fileWriter.d.ts +7 -0
- package/dist/decorators/FileColumn/fileWriter.js +62 -0
- package/dist/decorators/FileColumn/fileWriter.js.map +1 -0
- package/dist/helper/getFileUrl.d.ts +2 -0
- package/dist/helper/getFileUrl.js +10 -0
- package/dist/helper/getFileUrl.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -3
- package/src/DefaultSubscriber.ts +6 -6
- package/src/decorators/FileColumn/FileColumn.ts +2 -2
- package/src/decorators/FileColumn/FileType.ts +7 -5
- package/src/decorators/FileColumn/fileWriter.ts +68 -0
- package/src/helper/getFileUrl.ts +8 -0
- package/src/index.ts +2 -1
- package/dist/decorators/FileColumn/FileWriter.d.ts +0 -3
- package/dist/decorators/FileColumn/FileWriter.js +0 -46
- package/dist/decorators/FileColumn/FileWriter.js.map +0 -1
- package/src/decorators/FileColumn/FileWriter.ts +0 -49
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ainias42/typeorm-helper",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -29,10 +29,12 @@
|
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@ainias42/config": "*",
|
|
31
31
|
"@ainias42/js-helper": ">=0.8.19",
|
|
32
|
-
"
|
|
33
|
-
"reflect-metadata": "^0.2.2"
|
|
32
|
+
"mime-types": "^3.0.2",
|
|
33
|
+
"reflect-metadata": "^0.2.2",
|
|
34
|
+
"typeorm": "^0.3.20"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
37
|
+
"@types/mime-types": "^3.0.1",
|
|
36
38
|
"prettier": "^3.3.3"
|
|
37
39
|
}
|
|
38
40
|
}
|
package/src/DefaultSubscriber.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
} from 'typeorm';
|
|
11
11
|
import { FileTransformer } from '@/decorators/FileColumn/FileTransformer';
|
|
12
12
|
import { FileType } from '@/decorators/FileColumn/FileType';
|
|
13
|
-
import {
|
|
13
|
+
import { fileWriter } from '@/decorators/FileColumn/fileWriter';
|
|
14
14
|
|
|
15
15
|
@EventSubscriber()
|
|
16
16
|
export class DefaultSubscriber implements EntitySubscriberInterface {
|
|
@@ -29,11 +29,11 @@ export class DefaultSubscriber implements EntitySubscriberInterface {
|
|
|
29
29
|
promises.push(
|
|
30
30
|
Promise.all(
|
|
31
31
|
values.map((value) =>
|
|
32
|
-
|
|
33
|
-
(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
fileWriter
|
|
33
|
+
.writeToFile(value, transformer.fileOptions.saveDirectory)
|
|
34
|
+
.then((newFileType) => {
|
|
35
|
+
return newFileType;
|
|
36
|
+
}),
|
|
37
37
|
),
|
|
38
38
|
).then((newValues) => {
|
|
39
39
|
if (single) {
|
|
@@ -23,7 +23,7 @@ export function FileColumn(options: { saveDirectory: string; publicPath: string
|
|
|
23
23
|
single = true;
|
|
24
24
|
}
|
|
25
25
|
for (const value of values) {
|
|
26
|
-
if (value.src.startsWith(options.publicPath)) {
|
|
26
|
+
if ('src' in value && value.src.startsWith(options.publicPath)) {
|
|
27
27
|
value.src = value.src.slice(options.publicPath.length);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
@@ -41,7 +41,7 @@ export function FileColumn(options: { saveDirectory: string; publicPath: string
|
|
|
41
41
|
single = true;
|
|
42
42
|
}
|
|
43
43
|
for (const value of values) {
|
|
44
|
-
if (!value.src.startsWith('data:')) {
|
|
44
|
+
if ('src' in value && !value.src.startsWith('data:')) {
|
|
45
45
|
value.src = options.publicPath + value.src;
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/* eslint-disable class-methods-use-this */
|
|
2
|
+
import { PassThrough, Readable } from 'node:stream';
|
|
3
|
+
import { createWriteStream, existsSync, mkdirSync } from 'fs';
|
|
4
|
+
import { extension } from 'mime-types';
|
|
5
|
+
import crypto from 'crypto';
|
|
6
|
+
import type { FileType } from '@/decorators/FileColumn/FileType';
|
|
7
|
+
|
|
8
|
+
class FileWriter {
|
|
9
|
+
private stringSrcToArrayBuffer(src: string) {
|
|
10
|
+
const base64SearchText = ';base64,';
|
|
11
|
+
const indexBase64SearchText = src.indexOf(base64SearchText);
|
|
12
|
+
const indexSlash = src.indexOf('/');
|
|
13
|
+
|
|
14
|
+
// file is already a url
|
|
15
|
+
if (indexBase64SearchText === -1 || indexSlash === -1 || !src.startsWith('data:')) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const data = src.slice(Math.max(0, indexBase64SearchText + base64SearchText.length));
|
|
20
|
+
|
|
21
|
+
const dataBuffer = Buffer.from(data, 'base64');
|
|
22
|
+
return dataBuffer;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async writeToFile(file: FileType, saveDirectory: string, name?: string): Promise<FileType> {
|
|
26
|
+
let dataBuffer: Buffer<ArrayBuffer> | ArrayBuffer | undefined;
|
|
27
|
+
let dataStream: Readable;
|
|
28
|
+
if (file instanceof File) {
|
|
29
|
+
// @ts-expect-error there seems to be an type error inside the native types
|
|
30
|
+
dataStream = Readable.fromWeb(file.stream());
|
|
31
|
+
} else {
|
|
32
|
+
const buffer = this.stringSrcToArrayBuffer(file.src);
|
|
33
|
+
if (!buffer) {
|
|
34
|
+
return file;
|
|
35
|
+
}
|
|
36
|
+
dataBuffer = buffer;
|
|
37
|
+
dataStream = new PassThrough();
|
|
38
|
+
dataStream.push(dataBuffer);
|
|
39
|
+
// eslint-disable-next-line unicorn/prefer-single-call
|
|
40
|
+
dataStream.push(null);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const fileEnding = extension(file.type);
|
|
44
|
+
|
|
45
|
+
const seed = crypto.randomBytes(20) as string | NodeJS.ArrayBufferView;
|
|
46
|
+
const now = new Date();
|
|
47
|
+
|
|
48
|
+
// Month is 0-based. Add 1 to get 1-12
|
|
49
|
+
const fileName =
|
|
50
|
+
name ??
|
|
51
|
+
`${now.getUTCFullYear()}-${now.getUTCMonth() + 1}-${now.getUTCDate()}-${crypto
|
|
52
|
+
.createHash('sha1')
|
|
53
|
+
.update(seed)
|
|
54
|
+
.digest('hex')}.${fileEnding}`;
|
|
55
|
+
|
|
56
|
+
if (!existsSync(saveDirectory)) {
|
|
57
|
+
mkdirSync(saveDirectory, { recursive: true });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const writeStream = createWriteStream(saveDirectory + fileName);
|
|
61
|
+
const resultPromise = new Promise<void>((r) => writeStream.addListener('finish', r));
|
|
62
|
+
dataStream.pipe(writeStream);
|
|
63
|
+
await resultPromise;
|
|
64
|
+
return { type: file.type, name: file.name, src: fileName };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const fileWriter = new FileWriter();
|
package/src/index.ts
CHANGED
|
@@ -4,9 +4,10 @@ export * from './DbNamingStrategy';
|
|
|
4
4
|
export * from './DefaultSubscriber';
|
|
5
5
|
export * from './dataSource/dataSource';
|
|
6
6
|
export * from './dataSource/getRepository';
|
|
7
|
+
export * from './helper/getFileUrl';
|
|
7
8
|
export * from './migration/getCreateTableColumns';
|
|
8
9
|
export * from './migration/getCreateTableColumnsV1';
|
|
9
10
|
export * from './decorators/FileColumn/FileColumn';
|
|
10
11
|
export * from './decorators/FileColumn/FileTransformer';
|
|
11
12
|
export * from './decorators/FileColumn/FileType';
|
|
12
|
-
export * from './decorators/FileColumn/
|
|
13
|
+
export * from './decorators/FileColumn/fileWriter';
|
|
@@ -1,46 +0,0 @@
|
|
|
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.FileWriter = void 0;
|
|
7
|
-
const stream_1 = require("stream");
|
|
8
|
-
const fs_1 = require("fs");
|
|
9
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
10
|
-
exports.FileWriter = {
|
|
11
|
-
async writeToFile(src, saveDirectory) {
|
|
12
|
-
const base64SearchText = ';base64,';
|
|
13
|
-
const indexBase64SearchText = src.indexOf(base64SearchText);
|
|
14
|
-
const indexSlash = src.indexOf('/');
|
|
15
|
-
// file is already a url
|
|
16
|
-
if (indexBase64SearchText === -1 || indexSlash === -1 || !src.startsWith('data:')) {
|
|
17
|
-
return src;
|
|
18
|
-
}
|
|
19
|
-
const fileType = src.slice('data:'.length, indexSlash);
|
|
20
|
-
const fileEnding = src.slice(indexSlash + 1, indexBase64SearchText);
|
|
21
|
-
const data = src.slice(Math.max(0, indexBase64SearchText + base64SearchText.length));
|
|
22
|
-
const seed = crypto_1.default.randomBytes(20);
|
|
23
|
-
const now = new Date();
|
|
24
|
-
// Month is 0-based. Add 1 to get 1-12
|
|
25
|
-
const name = `${now.getUTCFullYear()}-${now.getUTCMonth() + 1}-${now.getUTCDate()}-${fileType}-${crypto_1.default
|
|
26
|
-
.createHash('sha1')
|
|
27
|
-
.update(seed)
|
|
28
|
-
.digest('hex')}.${fileEnding}`;
|
|
29
|
-
const dataBuffer = Buffer.from(data, 'base64');
|
|
30
|
-
const inputStream = new stream_1.Readable();
|
|
31
|
-
const dataStream = new stream_1.PassThrough();
|
|
32
|
-
if (!(0, fs_1.existsSync)(saveDirectory)) {
|
|
33
|
-
(0, fs_1.mkdirSync)(saveDirectory, { recursive: true });
|
|
34
|
-
}
|
|
35
|
-
const writeStream = (0, fs_1.createWriteStream)(saveDirectory + name);
|
|
36
|
-
inputStream.pipe(dataStream);
|
|
37
|
-
inputStream.push(dataBuffer);
|
|
38
|
-
// eslint-disable-next-line unicorn/prefer-single-call
|
|
39
|
-
inputStream.push(null);
|
|
40
|
-
const resultPromise = new Promise((r) => writeStream.addListener('finish', r));
|
|
41
|
-
dataStream.pipe(writeStream);
|
|
42
|
-
await resultPromise;
|
|
43
|
-
return name;
|
|
44
|
-
},
|
|
45
|
-
};
|
|
46
|
-
//# sourceMappingURL=FileWriter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FileWriter.js","sourceRoot":"","sources":["../../../src/decorators/FileColumn/FileWriter.ts"],"names":[],"mappings":";;;;;;AAAA,mCAA+C;AAC/C,2BAA8D;AAC9D,oDAA4B;AAEf,QAAA,UAAU,GAAG;IACtB,KAAK,CAAC,WAAW,CAAC,GAAW,EAAE,aAAqB;QAChD,MAAM,gBAAgB,GAAG,UAAU,CAAC;QACpC,MAAM,qBAAqB,GAAG,GAAG,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;QAC5D,MAAM,UAAU,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAEpC,wBAAwB;QACxB,IAAI,qBAAqB,KAAK,CAAC,CAAC,IAAI,UAAU,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;YAChF,OAAO,GAAG,CAAC;QACf,CAAC;QAED,MAAM,QAAQ,GAAG,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;QACvD,MAAM,UAAU,GAAG,GAAG,CAAC,KAAK,CAAC,UAAU,GAAG,C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C,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC;QACrF,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAC7B,MAAM,aAAa,CAAC;QACpB,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ,CAAC"}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { PassThrough, Readable } from 'stream';
|
|
2
|
-
import { createWriteStream, existsSync, mkdirSync } from 'fs';
|
|
3
|
-
import crypto from 'crypto';
|
|
4
|
-
|
|
5
|
-
export const FileWriter = {
|
|
6
|
-
async writeToFile(src: string, saveDirectory: string) {
|
|
7
|
-
const base64SearchText = ';base64,';
|
|
8
|
-
const indexBase64SearchText = src.indexOf(base64SearchText);
|
|
9
|
-
const indexSlash = src.indexOf('/');
|
|
10
|
-
|
|
11
|
-
// file is already a url
|
|
12
|
-
if (indexBase64SearchText === -1 || indexSlash === -1 || !src.startsWith('data:')) {
|
|
13
|
-
return src;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const fileType = src.slice('data:'.length, indexSlash);
|
|
17
|
-
const fileEnding = src.slice(indexSlash + 1, indexBase64SearchText);
|
|
18
|
-
const data = src.slice(Math.max(0, indexBase64SearchText + base64SearchText.length));
|
|
19
|
-
|
|
20
|
-
const seed = crypto.randomBytes(20) as string | NodeJS.ArrayBufferView;
|
|
21
|
-
const now = new Date();
|
|
22
|
-
|
|
23
|
-
// Month is 0-based. Add 1 to get 1-12
|
|
24
|
-
const name = `${now.getUTCFullYear()}-${now.getUTCMonth() + 1}-${now.getUTCDate()}-${fileType}-${crypto
|
|
25
|
-
.createHash('sha1')
|
|
26
|
-
.update(seed)
|
|
27
|
-
.digest('hex')}.${fileEnding}`;
|
|
28
|
-
|
|
29
|
-
const dataBuffer = Buffer.from(data, 'base64');
|
|
30
|
-
const inputStream = new Readable();
|
|
31
|
-
const dataStream = new PassThrough();
|
|
32
|
-
|
|
33
|
-
if (!existsSync(saveDirectory)) {
|
|
34
|
-
mkdirSync(saveDirectory, { recursive: true });
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const writeStream = createWriteStream(saveDirectory + name);
|
|
38
|
-
inputStream.pipe(dataStream);
|
|
39
|
-
|
|
40
|
-
inputStream.push(dataBuffer);
|
|
41
|
-
// eslint-disable-next-line unicorn/prefer-single-call
|
|
42
|
-
inputStream.push(null);
|
|
43
|
-
|
|
44
|
-
const resultPromise = new Promise<void>((r) => writeStream.addListener('finish', r));
|
|
45
|
-
dataStream.pipe(writeStream);
|
|
46
|
-
await resultPromise;
|
|
47
|
-
return name;
|
|
48
|
-
},
|
|
49
|
-
};
|