fix(file): ensure full valid URLs in publicUrl and privateUrl virtual getters
deploy-chabokan / deploy (push) Canceled after 0s
deploy-chabokan / deploy (push) Canceled after 0s
This commit is contained in:
@@ -25,11 +25,18 @@ const fileSchema = new mongoose.Schema(
|
||||
)
|
||||
|
||||
fileSchema.virtual('publicUrl').get(function () {
|
||||
return this.fileEndpoint + '/' + this.bucketName + '/' + this.fileName
|
||||
const protocol = process.env.AWS_USE_SSL === 'true' ? 'https://' : 'http://'
|
||||
let endpoint = this.fileEndpoint || process.env.AWS_ENDPOINT || 'localhost'
|
||||
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
|
||||
const hostPort = process.env.AWS_PORT ? `${endpoint}:${process.env.AWS_PORT}` : endpoint
|
||||
endpoint = `${protocol}${hostPort}`
|
||||
}
|
||||
return `${endpoint.replace(/\/$/, '')}/${this.bucketName}/${this.fileName}`
|
||||
})
|
||||
|
||||
fileSchema.virtual('privateUrl').get(function () {
|
||||
return process.env.API_URL + '/api/files/get-image/' + this.bucketName + '/' + this.fileName
|
||||
const apiUrl = (process.env.API_URL || '').replace(/\/$/, '')
|
||||
return `${apiUrl}/api/files/get-image/${this.bucketName}/${this.fileName}`
|
||||
})
|
||||
|
||||
const File = mongoose.model('file', fileSchema)
|
||||
|
||||
Reference in New Issue
Block a user