2024-08-16 17:57:11 -03:00
|
|
|
import { Response } from "express";
|
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2024-08-16 19:33:57 -04:00
|
|
|
import {
|
|
|
|
legacyCrawlerOptions,
|
|
|
|
mapRequestSchema,
|
|
|
|
RequestWithAuth,
|
|
|
|
} from "./types";
|
2024-08-16 17:57:11 -03:00
|
|
|
import { crawlToCrawler, StoredCrawl } from "../../lib/crawl-redis";
|
2024-08-16 19:33:57 -04:00
|
|
|
import { MapResponse, MapRequest } from "./types";
|
2024-08-16 17:57:11 -03:00
|
|
|
import { Logger } from "../../lib/logger";
|
|
|
|
import { configDotenv } from "dotenv";
|
|
|
|
import { search } from "../../search";
|
2024-08-16 19:33:57 -04:00
|
|
|
import {
|
|
|
|
checkAndUpdateURL,
|
|
|
|
checkAndUpdateURLForMap,
|
|
|
|
isSameDomain,
|
|
|
|
isSameSubdomain,
|
|
|
|
} from "../../lib/validateUrl";
|
|
|
|
import { fireEngineMap } from "../../search/fireEngine";
|
2024-08-16 17:57:11 -03:00
|
|
|
|
|
|
|
configDotenv();
|
2024-08-06 15:24:45 -03:00
|
|
|
|
2024-08-16 19:33:57 -04:00
|
|
|
export async function mapController(
|
|
|
|
req: RequestWithAuth<{}, MapResponse, MapRequest>,
|
|
|
|
res: Response<MapResponse>
|
|
|
|
) {
|
2024-08-15 23:30:33 +02:00
|
|
|
req.body = mapRequestSchema.parse(req.body);
|
2024-08-06 15:24:45 -03:00
|
|
|
|
2024-08-16 17:57:11 -03:00
|
|
|
const id = uuidv4();
|
|
|
|
let links: string[] = [req.body.url];
|
|
|
|
|
|
|
|
const crawlerOptions = legacyCrawlerOptions(req.body);
|
|
|
|
|
|
|
|
const sc: StoredCrawl = {
|
|
|
|
originUrl: req.body.url,
|
|
|
|
crawlerOptions,
|
|
|
|
pageOptions: {},
|
|
|
|
team_id: req.auth.team_id,
|
|
|
|
createdAt: Date.now(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const crawler = crawlToCrawler(id, sc);
|
|
|
|
|
2024-08-16 19:33:57 -04:00
|
|
|
const sitemap = sc.crawlerOptions.ignoreSitemap
|
|
|
|
? null
|
|
|
|
: await crawler.tryGetSitemap();
|
2024-08-16 17:57:11 -03:00
|
|
|
|
|
|
|
if (sitemap !== null) {
|
2024-08-16 19:33:57 -04:00
|
|
|
sitemap.map((x) => {
|
|
|
|
links.push(x.url);
|
|
|
|
});
|
2024-08-16 17:57:11 -03:00
|
|
|
}
|
|
|
|
|
2024-08-16 19:33:57 -04:00
|
|
|
const mapResults = await fireEngineMap(req.body.url, {
|
|
|
|
numResults: 50,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (mapResults.length > 0) {
|
|
|
|
mapResults.map((x) => {
|
|
|
|
links.push(x.url);
|
|
|
|
});
|
2024-08-16 17:57:11 -03:00
|
|
|
}
|
|
|
|
|
2024-08-16 19:33:57 -04:00
|
|
|
links = links.map((x) => checkAndUpdateURLForMap(x).url);
|
|
|
|
|
|
|
|
// allows for subdomains to be included
|
|
|
|
links = links.filter((x) => isSameDomain(x, req.body.url));
|
|
|
|
|
|
|
|
// if includeSubdomains is false, filter out subdomains
|
|
|
|
if (!req.body.includeSubdomains) {
|
|
|
|
links = links.filter((x) => isSameSubdomain(x, req.body.url));
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove duplicates that could be due to http/https or www
|
|
|
|
|
2024-08-16 17:57:11 -03:00
|
|
|
links = [...new Set(links)];
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
success: true,
|
2024-08-16 19:33:57 -04:00
|
|
|
links,
|
2024-08-16 17:57:11 -03:00
|
|
|
});
|
2024-08-06 15:24:45 -03:00
|
|
|
}
|