| import { coverImageConfig } from "@/config/coverImageConfig";
|
| import { siteConfig } from "@/config/siteConfig";
|
| import type { ImageFormat } from "@/types/config";
|
|
|
| const { randomCoverImage } = coverImageConfig;
|
|
|
| |
| |
| |
| |
| |
|
|
| export function processCoverImageSync(
|
| image: string | undefined,
|
| seed?: string,
|
| ): string {
|
| if (!image || image === "") {
|
| return "";
|
| }
|
|
|
| if (image !== "api") {
|
| return image;
|
| }
|
|
|
| if (
|
| !randomCoverImage.enable ||
|
| !randomCoverImage.apis ||
|
| randomCoverImage.apis.length === 0
|
| ) {
|
| return "";
|
| }
|
|
|
|
|
| const hash = seed
|
| ? seed.split("").reduce((acc, char) => {
|
| return ((acc << 5) - acc + char.charCodeAt(0)) | 0;
|
| }, 0)
|
| : 0;
|
|
|
|
|
| const apiIndex = Math.abs(hash) % randomCoverImage.apis.length;
|
| let apiUrl = randomCoverImage.apis[apiIndex];
|
|
|
|
|
| if (seed) {
|
| const separator = apiUrl.includes("?") ? "&" : "?";
|
| apiUrl = `${apiUrl}${separator}v=${Math.abs(hash)}`;
|
| }
|
|
|
| return apiUrl;
|
| }
|
|
|
| |
| |
|
|
| export function getImageFormats(): ImageFormat[] {
|
| const formatConfig = siteConfig.imageOptimization?.formats ?? "both";
|
| switch (formatConfig) {
|
| case "avif":
|
| return ["avif"];
|
| case "webp":
|
| return ["webp"];
|
| default:
|
| return ["avif", "webp"];
|
| }
|
| }
|
|
|
| |
| |
|
|
| export function getImageQuality(): number {
|
| return siteConfig.imageOptimization?.quality ?? 80;
|
| }
|
|
|
| |
| |
|
|
| export function getFallbackFormat(): "avif" | "webp" {
|
| const formatConfig = siteConfig.imageOptimization?.formats ?? "both";
|
| return formatConfig === "avif" ? "avif" : "webp";
|
| }
|
|
|