oneuptime/File/API/File.ts

51 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-12-18 09:34:28 +00:00
import FileModel from 'Common/Models/FileModel';
2022-12-18 08:17:51 +00:00
import NotFoundException from 'Common/Types/Exception/NotFoundException';
import ObjectID from 'Common/Types/ObjectID';
import FileService from 'CommonServer/Services/FileService';
import Express, {
ExpressRequest,
ExpressResponse,
NextFunction,
2022-12-18 09:34:28 +00:00
ExpressRouter,
2022-12-18 08:17:51 +00:00
} from 'CommonServer/Utils/Express';
import Response from 'CommonServer/Utils/Response';
export default class FileAPI {
public router!: ExpressRouter;
public constructor() {
this.router = Express.getRouter();
this.router.get(
`//image/:imageId`,
async (
req: ExpressRequest,
res: ExpressResponse,
_next: NextFunction
) => {
2022-12-18 09:34:28 +00:00
const file: FileModel | null = await FileService.findOneById({
2022-12-18 08:17:51 +00:00
id: new ObjectID(req.params['imageId']!),
props: {
isRoot: true,
ignoreHooks: true,
},
select: {
file: true,
type: true,
2022-12-18 09:34:28 +00:00
},
2022-12-18 08:17:51 +00:00
});
if (!file || !file.file || !file.type) {
2022-12-18 09:34:28 +00:00
return Response.sendErrorResponse(
req,
res,
new NotFoundException('File not found')
);
2022-12-18 08:17:51 +00:00
}
2022-12-18 09:34:28 +00:00
return Response.sendFileResponse(req, res, file);
2022-12-18 08:17:51 +00:00
}
);
}
2022-12-18 09:34:28 +00:00
}