Skip to content

Commit 4a748cb

Browse files
authored
Merge pull request #1484 from hackmdio:feat/optimize-module-size
Feat/optimize module size
2 parents e72bcfe + 2fe10a7 commit 4a748cb

File tree

4 files changed

+1490
-748
lines changed

4 files changed

+1490
-748
lines changed

lib/imageRouter/lutim.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const config = require('../config')
33
const logger = require('../logger')
44

5-
const lutim = require('lib/imageRouter/lutim')
5+
const lutim = require('lutim')
66

77
exports.uploadImage = function (imagePath, callback) {
88
if (!imagePath || typeof imagePath !== 'string') {

lib/imageRouter/s3.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ const config = require('../config')
66
const { getImageMimeType } = require('../utils')
77
const logger = require('../logger')
88

9-
const AWS = require('aws-sdk')
10-
const awsConfig = new AWS.Config(config.s3)
11-
const s3 = new AWS.S3(awsConfig)
9+
const { S3Client } = require('@aws-sdk/client-s3-node/S3Client')
10+
const { PutObjectCommand } = require('@aws-sdk/client-s3-node/commands/PutObjectCommand')
11+
12+
const s3 = new S3Client(config.s3)
1213

1314
exports.uploadImage = function (imagePath, callback) {
1415
if (!imagePath || typeof imagePath !== 'string') {
@@ -32,23 +33,23 @@ exports.uploadImage = function (imagePath, callback) {
3233
Body: buffer,
3334
ACL: 'public-read'
3435
}
35-
3636
const mimeType = getImageMimeType(imagePath)
3737
if (mimeType) { params.ContentType = mimeType }
3838

39-
s3.putObject(params, function (err, data) {
40-
if (err) {
41-
callback(new Error(err), null)
42-
return
43-
}
39+
const command = new PutObjectCommand(params)
4440

41+
s3.send(command).then(data => {
4542
let s3Endpoint = 's3.amazonaws.com'
4643
if (config.s3.endpoint) {
4744
s3Endpoint = config.s3.endpoint
4845
} else if (config.s3.region && config.s3.region !== 'us-east-1') {
4946
s3Endpoint = `s3-${config.s3.region}.amazonaws.com`
5047
}
5148
callback(null, `https://${s3Endpoint}/${config.s3bucket}/${params.Key}`)
49+
}).catch(err => {
50+
if (err) {
51+
callback(new Error(err), null)
52+
}
5253
})
5354
})
5455
}

0 commit comments

Comments
 (0)