@kipicore/dbcore 1.1.337 → 1.1.339
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.
|
@@ -11,6 +11,8 @@ const aws_1 = require("../configs/aws");
|
|
|
11
11
|
const env_1 = require("../configs/env");
|
|
12
12
|
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
13
13
|
const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
|
|
14
|
+
const date_fns_1 = require("date-fns");
|
|
15
|
+
const psql_1 = require("../models/psql");
|
|
14
16
|
const uploadFileToS3 = async (localFilePath, s3Key, s3Bucket = env_1.ENV_VARIABLE.AWS_BUCKET_NAME) => {
|
|
15
17
|
try {
|
|
16
18
|
const fileContent = fs_1.default.readFileSync(localFilePath);
|
|
@@ -35,12 +37,26 @@ const uploadFileToS3 = async (localFilePath, s3Key, s3Bucket = env_1.ENV_VARIABL
|
|
|
35
37
|
};
|
|
36
38
|
exports.uploadFileToS3 = uploadFileToS3;
|
|
37
39
|
const getPresignedUrl = async (bucket, fileStorage, expiresIn = 60 * 60 * 24) => {
|
|
40
|
+
if (fileStorage.expiredAt && fileStorage.filePath) {
|
|
41
|
+
const currentDate = (0, date_fns_1.getTime)((0, date_fns_1.subMinutes)(new Date(), 5));
|
|
42
|
+
const expireDate = (0, date_fns_1.getTime)(new Date(fileStorage.expiredAt));
|
|
43
|
+
if (currentDate < expireDate)
|
|
44
|
+
return fileStorage.filePath;
|
|
45
|
+
}
|
|
38
46
|
const s3Key = `${fileStorage.storagePath}/${fileStorage.storageFileName}`;
|
|
39
47
|
const command = new client_s3_1.GetObjectCommand({
|
|
40
48
|
Bucket: bucket,
|
|
41
49
|
Key: s3Key,
|
|
42
50
|
});
|
|
43
51
|
const signedUrl = await (0, s3_request_presigner_1.getSignedUrl)(aws_1.s3, command, { expiresIn }); // in seconds
|
|
52
|
+
if (fileStorage.id) {
|
|
53
|
+
await psql_1.FileStorageModel.update({
|
|
54
|
+
filePath: signedUrl,
|
|
55
|
+
expiredAt: (0, date_fns_1.addDays)(new Date(), 1),
|
|
56
|
+
}, {
|
|
57
|
+
where: { id: fileStorage.id },
|
|
58
|
+
});
|
|
59
|
+
}
|
|
44
60
|
return signedUrl;
|
|
45
61
|
};
|
|
46
62
|
exports.getPresignedUrl = getPresignedUrl;
|
|
@@ -3,5 +3,6 @@ import { IFileStorageAttributes } from '../../interfaces/fileStorageInterface';
|
|
|
3
3
|
import { TFileStorageModelCreationAttributes } from '../../types/fileStorageType';
|
|
4
4
|
declare class FileStorageModel extends Model<IFileStorageAttributes, TFileStorageModelCreationAttributes> {
|
|
5
5
|
static associate(models: any): void;
|
|
6
|
+
static addHooks(): void;
|
|
6
7
|
}
|
|
7
8
|
export default FileStorageModel;
|
|
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const sequelize_1 = require("sequelize");
|
|
4
4
|
const index_1 = require("./index");
|
|
5
5
|
const app_1 = require("../../constants/app");
|
|
6
|
+
const helpers_1 = require("../../helpers");
|
|
7
|
+
const configs_1 = require("../../configs");
|
|
6
8
|
class FileStorageModel extends sequelize_1.Model {
|
|
7
9
|
static associate(models) {
|
|
8
10
|
const { UserModel, UserDirectoryModel } = models;
|
|
@@ -47,6 +49,17 @@ class FileStorageModel extends sequelize_1.Model {
|
|
|
47
49
|
as: 'userDirectoryFile',
|
|
48
50
|
});
|
|
49
51
|
}
|
|
52
|
+
static addHooks() {
|
|
53
|
+
FileStorageModel.afterFind(async (instances) => {
|
|
54
|
+
if (!instances)
|
|
55
|
+
return;
|
|
56
|
+
const instanceArray = Array.isArray(instances) ? instances : [instances];
|
|
57
|
+
await Promise.all(instanceArray.map(async (instance) => {
|
|
58
|
+
const signedUrl = await (0, helpers_1.getPresignedUrl)(configs_1.ENV_VARIABLE.AWS_BUCKET_NAME, instance.get());
|
|
59
|
+
instance.setDataValue('filePath', signedUrl);
|
|
60
|
+
}));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
50
63
|
}
|
|
51
64
|
FileStorageModel.init({
|
|
52
65
|
id: {
|
package/package.json
CHANGED