mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 17:55:17 -04:00
rewrote ImageRouter
- introduced UploadProvider interface - rewrote all current UploadProviders Signed-off-by: Philip Molares <philip.molares@udo.edu> Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
ab5a654068
commit
982bbe9728
14 changed files with 312 additions and 256 deletions
59
lib/web/imageRouter/s3.ts
Normal file
59
lib/web/imageRouter/s3.ts
Normal file
|
@ -0,0 +1,59 @@
|
|||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import AWS from 'aws-sdk'
|
||||
|
||||
import { config } from '../../config'
|
||||
// import { getImageMimeType } from '../../utils'
|
||||
import { logger } from '../../logger'
|
||||
import { UploadProvider } from './index'
|
||||
|
||||
const awsConfig = new AWS.Config(config.s3)
|
||||
const s3 = new AWS.S3(awsConfig)
|
||||
|
||||
const S3UploadProvider: UploadProvider = {
|
||||
uploadImage: (imagePath, callback) => {
|
||||
if (!imagePath) {
|
||||
callback(new Error('Image path is missing or wrong'), undefined)
|
||||
return
|
||||
}
|
||||
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
logger.error('Callback has to be a function')
|
||||
return
|
||||
}
|
||||
|
||||
fs.readFile(imagePath, function (err, buffer) {
|
||||
if (err) {
|
||||
callback(new Error(err.message), undefined)
|
||||
return
|
||||
}
|
||||
const params = {
|
||||
Bucket: config.s3bucket,
|
||||
Key: path.join('uploads', path.basename(imagePath)),
|
||||
Body: buffer
|
||||
}
|
||||
|
||||
// ToDo: This does not exist (anymore?)
|
||||
// const mimeType = getImageMimeType(imagePath)
|
||||
// if (mimeType) { params.ContentType = mimeType }
|
||||
|
||||
logger.debug(`S3 object parameters: ${JSON.stringify(params)}`)
|
||||
s3.putObject(params, function (err, _) {
|
||||
if (err) {
|
||||
callback(new Error(err.message), undefined)
|
||||
return
|
||||
}
|
||||
|
||||
let s3Endpoint = 's3.amazonaws.com'
|
||||
if (config.s3.endpoint) {
|
||||
s3Endpoint = config.s3.endpoint
|
||||
} else if (config.s3.region && config.s3.region !== 'us-east-1') {
|
||||
s3Endpoint = `s3-${config.s3.region}.amazonaws.com`
|
||||
}
|
||||
callback(undefined, `https://${s3Endpoint}/${config.s3bucket}/${params.Key}`)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export { S3UploadProvider }
|
Loading…
Add table
Add a link
Reference in a new issue