forked from noxious/server
39 lines
983 B
TypeScript
39 lines
983 B
TypeScript
import fs from 'fs'
|
|
|
|
import type { Response } from 'express'
|
|
|
|
import Logger, { LoggerType } from'@/application/logger'
|
|
|
|
export abstract class BaseController {
|
|
protected readonly logger = Logger.type(LoggerType.HTTP)
|
|
|
|
protected sendSuccess(res: Response, data?: any, message?: string, status: number = 200) {
|
|
return res.status(status).json({
|
|
success: true,
|
|
message,
|
|
data
|
|
})
|
|
}
|
|
|
|
protected sendError(res: Response, message: string, status: number = 400) {
|
|
return res.status(status).json({
|
|
success: false,
|
|
message
|
|
})
|
|
}
|
|
|
|
protected sendFile(res: Response, filePath: string) {
|
|
if (!fs.existsSync(filePath)) {
|
|
this.logger.error(`File not found: ${filePath}`)
|
|
return this.sendError(res, 'Asset not found', 404)
|
|
}
|
|
|
|
res.sendFile(filePath, (error) => {
|
|
if (error) {
|
|
this.logger.error('Error sending file:' + error)
|
|
this.sendError(res, 'Error downloading the asset', 500)
|
|
}
|
|
})
|
|
}
|
|
}
|