fix(file): ensure full valid URLs in publicUrl and privateUrl virtual getters
deploy-chabokan / deploy (push) Canceled after 0s

This commit is contained in:
2026-08-19 04:49:40 +03:30
parent 61c9012912
commit dd611df7e5
+9 -2
View File
@@ -25,11 +25,18 @@ const fileSchema = new mongoose.Schema(
) )
fileSchema.virtual('publicUrl').get(function () { 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 () { 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) const File = mongoose.model('file', fileSchema)