2024-02-10 18:34:25 -06:00
|
|
|
import {
|
|
|
|
AccountSettings,
|
|
|
|
ArchivedFormat,
|
|
|
|
LinkIncludingShortenedCollectionAndTags,
|
|
|
|
} from "@/types/global";
|
2024-02-08 00:42:58 -06:00
|
|
|
import { LinksRouteTo } from "@prisma/client";
|
2024-02-10 18:34:25 -06:00
|
|
|
import {
|
|
|
|
pdfAvailable,
|
|
|
|
readabilityAvailable,
|
|
|
|
screenshotAvailable,
|
2024-06-18 11:19:52 -05:00
|
|
|
singlefileAvailable,
|
2024-02-10 18:34:25 -06:00
|
|
|
} from "../shared/getArchiveValidity";
|
2024-02-08 00:42:58 -06:00
|
|
|
|
2024-02-10 18:34:25 -06:00
|
|
|
export const generateLinkHref = (
|
|
|
|
link: LinkIncludingShortenedCollectionAndTags,
|
|
|
|
account: AccountSettings
|
|
|
|
): string => {
|
|
|
|
// Return the links href based on the account's preference
|
|
|
|
// If the user's preference is not available, return the original link
|
2024-04-01 01:56:54 -05:00
|
|
|
if (account.linksRouteTo === LinksRouteTo.ORIGINAL && link.type === "url") {
|
|
|
|
return link.url || "";
|
|
|
|
} else if (account.linksRouteTo === LinksRouteTo.PDF || link.type === "pdf") {
|
|
|
|
if (!pdfAvailable(link)) return link.url || "";
|
2024-02-08 00:42:58 -06:00
|
|
|
|
2024-04-01 01:56:54 -05:00
|
|
|
return `/preserved/${link?.id}?format=${ArchivedFormat.pdf}`;
|
|
|
|
} else if (
|
|
|
|
account.linksRouteTo === LinksRouteTo.READABLE &&
|
|
|
|
link.type === "url"
|
|
|
|
) {
|
|
|
|
if (!readabilityAvailable(link)) return link.url || "";
|
2024-02-08 00:59:17 -06:00
|
|
|
|
2024-04-01 01:56:54 -05:00
|
|
|
return `/preserved/${link?.id}?format=${ArchivedFormat.readability}`;
|
|
|
|
} else if (
|
|
|
|
account.linksRouteTo === LinksRouteTo.SCREENSHOT ||
|
|
|
|
link.type === "image"
|
|
|
|
) {
|
|
|
|
if (!screenshotAvailable(link)) return link.url || "";
|
2024-03-15 13:41:41 -05:00
|
|
|
|
2024-04-01 01:56:54 -05:00
|
|
|
return `/preserved/${link?.id}?format=${
|
|
|
|
link?.image?.endsWith("png") ? ArchivedFormat.png : ArchivedFormat.jpeg
|
|
|
|
}`;
|
2024-06-18 11:19:52 -05:00
|
|
|
} else if (account.linksRouteTo === LinksRouteTo.SINGLEFILE) {
|
|
|
|
if (!singlefileAvailable(link)) return link.url || "";
|
2024-02-08 00:59:17 -06:00
|
|
|
|
2024-06-18 11:19:52 -05:00
|
|
|
return `/preserved/${link?.id}?format=${ArchivedFormat.singlefile}`;
|
2024-04-01 01:56:54 -05:00
|
|
|
} else {
|
|
|
|
return link.url || "";
|
2024-02-10 18:34:25 -06:00
|
|
|
}
|
|
|
|
};
|