diff --git a/components/file/fileModel.js b/components/file/fileModel.js index 122aa26..d6e4fc1 100644 --- a/components/file/fileModel.js +++ b/components/file/fileModel.js @@ -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)