mirror of
https://github.com/direct-img/direct-img.link.git
synced 2026-10-14 08:47:53 +00:00
A search's result URLs are kept in SurrealDB (results table) for 1 hour, fetched in the same transaction as the rate limit. Other ?i= of the query then skip the Brave/Bing search and don't count toward the daily limit. The record also tracks which results worked (pos) and how far it got (next), so an already-known ?i= downloads only its own image and a new one carries on from where the last left off. free.* searches Openverse and Wikimedia together so its whole list can be cached. Empty results aren't cached, since they can mean Brave and Bing failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
21 lines
1.0 KiB
JavaScript
21 lines
1.0 KiB
JavaScript
import { openverseImageSearch } from "./openverse.js";
|
|
import { wikimediaImageSearch } from "./wikimedia.js";
|
|
|
|
// Wikimedia's User-Agent policy requires contact info; also used when downloading free images
|
|
export const FREE_UA = "direct-img.link/1.0 (https://direct-img.link; contact@direct-img.link)";
|
|
|
|
// Tried in this order when no ?src= is given
|
|
const SOURCES = { openverse: openverseImageSearch, wikimedia: wikimediaImageSearch };
|
|
|
|
// ?src=openverse|wikimedia, or "auto" (every source in order) when omitted. Returns null if invalid.
|
|
export function parseFreeSource(searchParams) {
|
|
const src = searchParams.get("src")?.toLowerCase() || "auto";
|
|
return src === "auto" || Object.hasOwn(SOURCES, src) ? src : null;
|
|
}
|
|
|
|
// Every source's image URLs, one source after another. All are searched at once, since the whole list gets cached.
|
|
export async function freeImageUrls(query, src, env) {
|
|
const names = src === "auto" ? Object.keys(SOURCES) : [src];
|
|
return (await Promise.all(names.map(n => SOURCES[n](query, env, FREE_UA)))).flatMap(urls => urls || []);
|
|
}
|