2023-12-25 13:22:16 +00:00
|
|
|
import {
|
|
|
|
ExpressRequest,
|
|
|
|
ExpressResponse,
|
|
|
|
NextFunction,
|
|
|
|
} from '../Utils/Express';
|
2023-12-25 12:21:13 +00:00
|
|
|
import Response from '../Utils/Response';
|
|
|
|
import FileService, {
|
|
|
|
Service as FileServiceType,
|
|
|
|
} from '../Services/FileService';
|
|
|
|
import BaseAPI from './BaseAPI';
|
|
|
|
import ObjectID from 'Common/Types/ObjectID';
|
|
|
|
import File from 'Model/Models/File';
|
|
|
|
import NotFoundException from 'Common/Types/Exception/NotFoundException';
|
|
|
|
|
2023-12-25 13:22:16 +00:00
|
|
|
export default class FileAPI extends BaseAPI<File, FileServiceType> {
|
2023-12-25 12:21:13 +00:00
|
|
|
public constructor() {
|
|
|
|
super(File, FileService);
|
|
|
|
|
|
|
|
this.router.get(
|
|
|
|
`/image/:imageId`,
|
|
|
|
async (
|
|
|
|
req: ExpressRequest,
|
|
|
|
res: ExpressResponse,
|
|
|
|
_next: NextFunction
|
|
|
|
) => {
|
|
|
|
const file: File | null = await FileService.findOneById({
|
|
|
|
id: new ObjectID(req.params['imageId']!),
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
ignoreHooks: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
file: true,
|
|
|
|
type: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!file || !file.file || !file.type) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new NotFoundException('File not found')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response.sendFileResponse(req, res, file);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|