Files
firecrawl/apps/api/src/controllers/v0/crawl-cancel.ts
T

63 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-05-06 17:16:43 -07:00
import { Request, Response } from "express";
2024-08-26 18:48:00 -03:00
import { authenticateUser } from "../auth";
2024-08-15 21:51:59 +02:00
import { RateLimiterMode } from "../../../src/types";
import { supabase_service } from "../../../src/services/supabase";
2024-11-07 20:57:33 +01:00
import { logger } from "../../../src/lib/logger";
2024-08-15 21:51:59 +02:00
import { getCrawl, saveCrawl } from "../../../src/lib/crawl-redis";
import * as Sentry from "@sentry/node";
2024-09-04 15:57:57 -03:00
import { configDotenv } from "dotenv";
configDotenv();
2024-05-06 17:16:43 -07:00
export async function crawlCancelController(req: Request, res: Response) {
try {
2024-07-07 15:06:31 +02:00
const useDbAuthentication = process.env.USE_DB_AUTHENTICATION === 'true';
2024-11-07 20:57:33 +01:00
const auth = await authenticateUser(
2024-05-06 17:16:43 -07:00
req,
res,
RateLimiterMode.CrawlStatus
);
2024-11-07 20:57:33 +01:00
if (!auth.success) {
return res.status(auth.status).json({ error: auth.error });
2024-05-06 17:16:43 -07:00
}
2024-08-13 20:51:43 +02:00
2024-11-07 20:57:33 +01:00
const { team_id } = auth;
2024-08-13 20:51:43 +02:00
const sc = await getCrawl(req.params.jobId);
if (!sc) {
2024-05-06 17:16:43 -07:00
return res.status(404).json({ error: "Job not found" });
}
// check if the job belongs to the team
2024-07-07 15:06:31 +02:00
if (useDbAuthentication) {
const { data, error: supaError } = await supabase_service
.from("bulljobs_teams")
.select("*")
.eq("job_id", req.params.jobId)
.eq("team_id", team_id);
if (supaError) {
return res.status(500).json({ error: supaError.message });
}
2024-05-06 17:16:43 -07:00
2024-07-07 15:06:31 +02:00
if (data.length === 0) {
return res.status(403).json({ error: "Unauthorized" });
}
2024-05-06 17:16:43 -07:00
}
2024-07-07 15:06:31 +02:00
2024-05-06 17:16:43 -07:00
try {
2024-08-13 20:51:43 +02:00
sc.cancelled = true;
await saveCrawl(req.params.jobId, sc);
2024-05-06 17:16:43 -07:00
} catch (error) {
2024-11-07 20:57:33 +01:00
logger.error(error);
2024-05-06 17:16:43 -07:00
}
res.json({
2024-07-18 13:43:03 -04:00
status: "cancelled"
2024-05-06 17:16:43 -07:00
});
} catch (error) {
Sentry.captureException(error);
2024-11-07 20:57:33 +01:00
logger.error(error);
2024-05-06 17:16:43 -07:00
return res.status(500).json({ error: error.message });
}
}