@kipicore/dbcore 1.1.342 → 1.1.344
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/db/psql/migrations/20260324121350-add-file-path-file-storage-model.d.ts +2 -0
- package/dist/db/psql/migrations/20260324121350-add-file-path-file-storage-model.js +21 -0
- package/dist/helpers/s3Uploader.js +16 -16
- package/dist/models/psql/fileStorageModel.js +5 -0
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const up = async (queryInterface, Sequelize) => {
|
|
3
|
+
const table = await queryInterface.describeTable('file_storage');
|
|
4
|
+
if (!table.file_path) {
|
|
5
|
+
await queryInterface.addColumn('file_storage', 'file_path', {
|
|
6
|
+
type: Sequelize.STRING,
|
|
7
|
+
allowNull: true,
|
|
8
|
+
field: 'file_path',
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const down = async (queryInterface) => {
|
|
13
|
+
const table = await queryInterface.describeTable('file_storage');
|
|
14
|
+
if (table.file_path) {
|
|
15
|
+
await queryInterface.removeColumn('file_storage', 'file_path');
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
module.exports = {
|
|
19
|
+
up,
|
|
20
|
+
down,
|
|
21
|
+
};
|
|
@@ -11,8 +11,6 @@ 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");
|
|
16
14
|
const uploadFileToS3 = async (localFilePath, s3Key, s3Bucket = env_1.ENV_VARIABLE.AWS_BUCKET_NAME) => {
|
|
17
15
|
try {
|
|
18
16
|
const fileContent = fs_1.default.readFileSync(localFilePath);
|
|
@@ -37,26 +35,28 @@ const uploadFileToS3 = async (localFilePath, s3Key, s3Bucket = env_1.ENV_VARIABL
|
|
|
37
35
|
};
|
|
38
36
|
exports.uploadFileToS3 = uploadFileToS3;
|
|
39
37
|
const getPresignedUrl = async (bucket, fileStorage, expiresIn = 60 * 60 * 24) => {
|
|
40
|
-
if (fileStorage.expiredAt && fileStorage.filePath) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
38
|
+
// if (fileStorage.expiredAt && fileStorage.filePath) {
|
|
39
|
+
// const currentDate = getTime(subMinutes(new Date(), 5));
|
|
40
|
+
// const expireDate = getTime(new Date(fileStorage.expiredAt));
|
|
41
|
+
// if (currentDate < expireDate) return fileStorage.filePath as string;
|
|
42
|
+
// }
|
|
46
43
|
const s3Key = `${fileStorage.storagePath}/${fileStorage.storageFileName}`;
|
|
47
44
|
const command = new client_s3_1.GetObjectCommand({
|
|
48
45
|
Bucket: bucket,
|
|
49
46
|
Key: s3Key,
|
|
50
47
|
});
|
|
51
48
|
const signedUrl = await (0, s3_request_presigner_1.getSignedUrl)(aws_1.s3, command, { expiresIn }); // in seconds
|
|
52
|
-
if (fileStorage.id) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
49
|
+
// if (fileStorage.id) {
|
|
50
|
+
// await FileStorageModel.update(
|
|
51
|
+
// {
|
|
52
|
+
// filePath: signedUrl,
|
|
53
|
+
// expiredAt: addDays(new Date(), 1),
|
|
54
|
+
// },I
|
|
55
|
+
// {
|
|
56
|
+
// where: { id: fileStorage.id },
|
|
57
|
+
// },
|
|
58
|
+
// );
|
|
59
|
+
// }
|
|
60
60
|
return signedUrl;
|
|
61
61
|
};
|
|
62
62
|
exports.getPresignedUrl = getPresignedUrl;
|
|
@@ -101,6 +101,11 @@ FileStorageModel.init({
|
|
|
101
101
|
field: 'expired_at',
|
|
102
102
|
allowNull: true,
|
|
103
103
|
},
|
|
104
|
+
filePath: {
|
|
105
|
+
type: sequelize_1.DataTypes.STRING,
|
|
106
|
+
field: 'file_path',
|
|
107
|
+
allowNull: true,
|
|
108
|
+
},
|
|
104
109
|
}, {
|
|
105
110
|
modelName: 'FileStorageModel',
|
|
106
111
|
tableName: 'file_storage',
|
package/package.json
CHANGED