pangea-server 3.3.34 → 3.3.36
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.
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export declare function
|
|
6
|
-
export declare function
|
|
7
|
-
export declare function
|
|
8
|
-
export declare function
|
|
1
|
+
type GeneralUploadParamsConfig = {
|
|
2
|
+
folder: string;
|
|
3
|
+
contentType: string;
|
|
4
|
+
};
|
|
5
|
+
export declare function uploadImage(image: UploadableImage, folder: string, previousFile?: string | null): Promise<string>;
|
|
6
|
+
export declare function uploadImages(images: UploadableImage[], folder: string): Promise<string[]>;
|
|
7
|
+
export declare function uploadVideo(file: MulterFile, folder: string): Promise<string>;
|
|
8
|
+
export declare function uploadVideos(files: MulterFile[], folder: string): Promise<string[]>;
|
|
9
|
+
export declare function generateUploadUrl(config: GeneralUploadParamsConfig): Promise<{
|
|
9
10
|
url: string;
|
|
10
11
|
key: string;
|
|
11
12
|
}>;
|
|
12
|
-
export declare function generateUploadUrls(
|
|
13
|
+
export declare function generateUploadUrls(folder: string, contentTypes: string[]): Promise<{
|
|
13
14
|
url: string;
|
|
14
15
|
key: string;
|
|
15
16
|
}[]>;
|
|
17
|
+
export declare function deleteFile(filename: string): Promise<void>;
|
|
18
|
+
export declare function deleteFiles(filenames: string[]): Promise<void[]>;
|
|
16
19
|
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.
|
|
6
|
+
exports.deleteFiles = exports.deleteFile = exports.generateUploadUrls = exports.generateUploadUrl = 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,48 +19,43 @@ 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
|
-
const
|
|
31
|
-
const
|
|
32
|
-
|
|
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 isString = typeof image === 'string';
|
|
26
|
+
const buffer = isString ? await promises_1.default.readFile(path_1.default.join(process.cwd(), image)) : image.buffer;
|
|
27
|
+
const contentType = isString ? 'image/png' : image.mimetype;
|
|
28
|
+
return uploadFile(buffer, { folder, contentType });
|
|
38
29
|
}
|
|
39
30
|
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)));
|
|
31
|
+
function uploadImages(images, folder) {
|
|
32
|
+
return Promise.all(images.map((image) => uploadImage(image, folder)));
|
|
44
33
|
}
|
|
45
34
|
exports.uploadImages = uploadImages;
|
|
46
|
-
async function uploadVideo(file,
|
|
47
|
-
if (!file)
|
|
48
|
-
error_helpers_1.AppError.Throw({ statusCodeName: 'BAD_REQUEST', errorCode: 'VIDEO_NOT_SENT' });
|
|
49
|
-
if (previousFile)
|
|
50
|
-
await deleteFile(previousFile);
|
|
35
|
+
async function uploadVideo(file, folder) {
|
|
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, contentType: 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;
|
|
49
|
+
async function generateUploadUrl(config) {
|
|
50
|
+
const params = { ...getGeneralUploadParams(config), Expires: 60 };
|
|
51
|
+
const url = await s3.getSignedUrlPromise('putObject', params);
|
|
52
|
+
return { url, key: params.Key };
|
|
53
|
+
}
|
|
54
|
+
exports.generateUploadUrl = generateUploadUrl;
|
|
55
|
+
async function generateUploadUrls(folder, contentTypes) {
|
|
56
|
+
return Promise.all(contentTypes.map((contentType) => generateUploadUrl({ folder, contentType })));
|
|
57
|
+
}
|
|
58
|
+
exports.generateUploadUrls = generateUploadUrls;
|
|
66
59
|
async function deleteFile(filename) {
|
|
67
60
|
await s3.deleteObject({ Bucket: bucketName, Key: filename }).promise();
|
|
68
61
|
(0, print_helpers_1.printInfo)('file', `file deleted from ${filename}`);
|
|
@@ -72,33 +65,14 @@ function deleteFiles(filenames) {
|
|
|
72
65
|
return Promise.all(filenames.map((filename) => deleteFile(filename)));
|
|
73
66
|
}
|
|
74
67
|
exports.deleteFiles = deleteFiles;
|
|
75
|
-
async function generateUploadUrl(contentType, ...folders) {
|
|
76
|
-
const key = `${folders.join('/')}/${(0, random_helpers_1.getRandomString)('short')}`;
|
|
77
|
-
const params = {
|
|
78
|
-
Bucket: bucketName,
|
|
79
|
-
Key: key,
|
|
80
|
-
ContentType: contentType,
|
|
81
|
-
ACL: 'public-read',
|
|
82
|
-
Expires: 60,
|
|
83
|
-
};
|
|
84
|
-
const url = await s3.getSignedUrlPromise('putObject', params);
|
|
85
|
-
return { url, key };
|
|
86
|
-
}
|
|
87
|
-
exports.generateUploadUrl = generateUploadUrl;
|
|
88
|
-
async function generateUploadUrls(contentTypes, ...folders) {
|
|
89
|
-
return Promise.all(contentTypes.map((contentType) => generateUploadUrl(contentType, ...folders)));
|
|
90
|
-
}
|
|
91
|
-
exports.generateUploadUrls = generateUploadUrls;
|
|
92
68
|
// internal functions
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
ACL: 'public-read',
|
|
100
|
-
};
|
|
69
|
+
function getGeneralUploadParams(config) {
|
|
70
|
+
const key = `${config.folder}/${(0, random_helpers_1.getRandomString)('short')}`;
|
|
71
|
+
return { Bucket: bucketName, Key: key, ContentType: config.contentType, ACL: 'public-read' };
|
|
72
|
+
}
|
|
73
|
+
async function uploadFile(body, config) {
|
|
74
|
+
const params = { ...getGeneralUploadParams(config), Body: body };
|
|
101
75
|
const res = await s3.upload(params).promise();
|
|
102
76
|
(0, print_helpers_1.printInfo)('file', `file uploaded to ${res.Location}`);
|
|
103
|
-
return
|
|
77
|
+
return params.Key;
|
|
104
78
|
}
|
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.36",
|
|
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": {
|