pangea-server 3.3.33 → 3.3.35
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.
|
@@ -16,7 +16,7 @@ function initDatabase(models, seeds, config = {}) {
|
|
|
16
16
|
username: (0, helpers_1.getEnvStr)('DB_USERNAME'),
|
|
17
17
|
password: (0, helpers_1.getEnvStr)('DB_PASSWORD'),
|
|
18
18
|
database: (0, helpers_1.getEnvStr)('DB_DATABASE'),
|
|
19
|
-
pool: { max:
|
|
19
|
+
pool: { max: 10, min: 2, idle: 30000, acquire: 60000 },
|
|
20
20
|
models: Object.values(models),
|
|
21
21
|
logging: config.logging ? logQuery : false,
|
|
22
22
|
benchmark: true,
|
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
type PreviousFile = string | null
|
|
2
|
-
export declare function uploadImage(image: UploadableImage,
|
|
3
|
-
export declare function uploadImages(images: UploadableImage[],
|
|
4
|
-
export declare function uploadVideo(file: MulterFile,
|
|
5
|
-
export declare function uploadVideos(files: MulterFile[],
|
|
1
|
+
type PreviousFile = string | null;
|
|
2
|
+
export declare function uploadImage(image: UploadableImage, folder: string, previousFile?: PreviousFile): Promise<string>;
|
|
3
|
+
export declare function uploadImages(images: UploadableImage[], folder: string): Promise<string[]>;
|
|
4
|
+
export declare function uploadVideo(file: MulterFile, folder: string, previousFile?: PreviousFile): Promise<string>;
|
|
5
|
+
export declare function uploadVideos(files: MulterFile[], folder: string): Promise<string[]>;
|
|
6
6
|
export declare function deleteFile(filename: string): Promise<void>;
|
|
7
7
|
export declare function deleteFiles(filenames: string[]): Promise<void[]>;
|
|
8
|
+
export declare function generateUploadUrl(contentType: string, folder: string): Promise<{
|
|
9
|
+
url: string;
|
|
10
|
+
key: string;
|
|
11
|
+
}>;
|
|
12
|
+
export declare function generateUploadUrls(contentTypes: string[], folder: string): Promise<{
|
|
13
|
+
url: string;
|
|
14
|
+
key: string;
|
|
15
|
+
}[]>;
|
|
8
16
|
export {};
|
|
@@ -3,14 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.deleteFiles = exports.deleteFile = exports.uploadVideos = exports.uploadVideo = exports.uploadImages = exports.uploadImage = void 0;
|
|
6
|
+
exports.generateUploadUrls = exports.generateUploadUrl = exports.deleteFiles = exports.deleteFile = exports.uploadVideos = exports.uploadVideo = exports.uploadImages = exports.uploadImage = void 0;
|
|
7
7
|
const path_1 = __importDefault(require("path"));
|
|
8
8
|
const promises_1 = __importDefault(require("fs/promises"));
|
|
9
9
|
const fs_1 = __importDefault(require("fs"));
|
|
10
10
|
const aws_sdk_1 = __importDefault(require("aws-sdk"));
|
|
11
|
-
const sharp_1 = __importDefault(require("sharp"));
|
|
12
11
|
// helpers
|
|
13
|
-
const error_helpers_1 = require("./error.helpers");
|
|
14
12
|
const print_helpers_1 = require("./print.helpers");
|
|
15
13
|
const env_helpers_1 = require("./env.helpers");
|
|
16
14
|
const random_helpers_1 = require("./random.helpers");
|
|
@@ -21,46 +19,31 @@ const s3 = new aws_sdk_1.default.S3({
|
|
|
21
19
|
accessKeyId: (0, env_helpers_1.getEnvStr)('DIGITAL_OCEAN_SPACES_ACCESS_KEY'),
|
|
22
20
|
secretAccessKey: (0, env_helpers_1.getEnvStr)('DIGITAL_OCEAN_SPACES_SECRET_KEY'),
|
|
23
21
|
});
|
|
24
|
-
async function uploadImage(image,
|
|
25
|
-
if (!image)
|
|
26
|
-
error_helpers_1.AppError.Throw({ statusCodeName: 'BAD_REQUEST', errorCode: 'IMAGE_NOT_SENT' });
|
|
22
|
+
async function uploadImage(image, folder, previousFile) {
|
|
27
23
|
if (previousFile)
|
|
28
24
|
await deleteFile(previousFile);
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
const maxSizeBytes = maxSizeKb * 1024;
|
|
32
|
-
const qualityRatio = imageBuffer.length > maxSizeBytes ? maxSizeBytes / imageBuffer.length : 1;
|
|
33
|
-
const qualityMultiplier = 2;
|
|
34
|
-
const targetQuality = Math.floor(qualityRatio * 100 * qualityMultiplier);
|
|
35
|
-
const quality = Math.max(10, Math.min(100, targetQuality));
|
|
36
|
-
const bufferCompressed = await (0, sharp_1.default)(imageBuffer).png({ quality }).toBuffer();
|
|
37
|
-
return uploadFile(bufferCompressed, 'image/jpeg', ...folders);
|
|
25
|
+
const buffer = typeof image === 'string' ? await promises_1.default.readFile(path_1.default.join(process.cwd(), image)) : image.buffer;
|
|
26
|
+
return uploadFile(buffer, folder, 'image/png');
|
|
38
27
|
}
|
|
39
28
|
exports.uploadImage = uploadImage;
|
|
40
|
-
function uploadImages(images,
|
|
41
|
-
|
|
42
|
-
error_helpers_1.AppError.Throw({ statusCodeName: 'BAD_REQUEST', errorCode: 'IMAGES_NOT_SENT' });
|
|
43
|
-
return Promise.all(images.map((image) => uploadImage(image, undefined, ...folders)));
|
|
29
|
+
function uploadImages(images, folder) {
|
|
30
|
+
return Promise.all(images.map((image) => uploadImage(image, folder)));
|
|
44
31
|
}
|
|
45
32
|
exports.uploadImages = uploadImages;
|
|
46
|
-
async function uploadVideo(file,
|
|
47
|
-
if (!file)
|
|
48
|
-
error_helpers_1.AppError.Throw({ statusCodeName: 'BAD_REQUEST', errorCode: 'VIDEO_NOT_SENT' });
|
|
33
|
+
async function uploadVideo(file, folder, previousFile) {
|
|
49
34
|
if (previousFile)
|
|
50
35
|
await deleteFile(previousFile);
|
|
51
36
|
const stream = fs_1.default.createReadStream(file.path);
|
|
52
37
|
try {
|
|
53
|
-
return await uploadFile(stream, file.mimetype
|
|
38
|
+
return await uploadFile(stream, folder, file.mimetype);
|
|
54
39
|
}
|
|
55
40
|
finally {
|
|
56
41
|
promises_1.default.unlink(file.path);
|
|
57
42
|
}
|
|
58
43
|
}
|
|
59
44
|
exports.uploadVideo = uploadVideo;
|
|
60
|
-
function uploadVideos(files,
|
|
61
|
-
|
|
62
|
-
error_helpers_1.AppError.Throw({ statusCodeName: 'BAD_REQUEST', errorCode: 'VIDEOS_NOT_SENT' });
|
|
63
|
-
return Promise.all(files.map((file) => uploadVideo(file, undefined, ...folders)));
|
|
45
|
+
function uploadVideos(files, folder) {
|
|
46
|
+
return Promise.all(files.map((file) => uploadVideo(file, folder)));
|
|
64
47
|
}
|
|
65
48
|
exports.uploadVideos = uploadVideos;
|
|
66
49
|
async function deleteFile(filename) {
|
|
@@ -72,16 +55,22 @@ function deleteFiles(filenames) {
|
|
|
72
55
|
return Promise.all(filenames.map((filename) => deleteFile(filename)));
|
|
73
56
|
}
|
|
74
57
|
exports.deleteFiles = deleteFiles;
|
|
58
|
+
async function generateUploadUrl(contentType, folder) {
|
|
59
|
+
const key = `${folder}/${(0, random_helpers_1.getRandomString)('short')}`;
|
|
60
|
+
const params = { Bucket: bucketName, Key: key, ContentType: contentType, ACL: 'public-read', Expires: 60 };
|
|
61
|
+
const url = await s3.getSignedUrlPromise('putObject', params);
|
|
62
|
+
return { url, key };
|
|
63
|
+
}
|
|
64
|
+
exports.generateUploadUrl = generateUploadUrl;
|
|
65
|
+
async function generateUploadUrls(contentTypes, folder) {
|
|
66
|
+
return Promise.all(contentTypes.map((contentType) => generateUploadUrl(contentType, folder)));
|
|
67
|
+
}
|
|
68
|
+
exports.generateUploadUrls = generateUploadUrls;
|
|
75
69
|
// internal functions
|
|
76
|
-
async function uploadFile(body,
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
Key: `${folders.join('/')}/${(0, random_helpers_1.getRandomString)('short')}`,
|
|
80
|
-
Body: body,
|
|
81
|
-
ContentType: contentType,
|
|
82
|
-
ACL: 'public-read',
|
|
83
|
-
};
|
|
70
|
+
async function uploadFile(body, folder, contentType) {
|
|
71
|
+
const key = `${folder}/${(0, random_helpers_1.getRandomString)('short')}`;
|
|
72
|
+
const params = { Bucket: bucketName, Key: key, ContentType: contentType, Body: body, ACL: 'public-read' };
|
|
84
73
|
const res = await s3.upload(params).promise();
|
|
85
74
|
(0, print_helpers_1.printInfo)('file', `file uploaded to ${res.Location}`);
|
|
86
|
-
return
|
|
75
|
+
return key;
|
|
87
76
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pangea-server",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "3.3.
|
|
4
|
+
"version": "3.3.35",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
7
7
|
],
|
|
@@ -58,7 +58,6 @@
|
|
|
58
58
|
"reflect-metadata": "0.2.2",
|
|
59
59
|
"sequelize": "6.37.7",
|
|
60
60
|
"sequelize-typescript": "2.1.6",
|
|
61
|
-
"sharp": "0.34.1",
|
|
62
61
|
"typia": "9.7.2"
|
|
63
62
|
},
|
|
64
63
|
"devDependencies": {
|