import { error } from "@sveltejs/kit";
import { blobRawUrl } from "$lib/api/gitclient";
import { resolveRepoFromParams } from "$lib/server/repo";
import type { RequestHandler } from "./$types";

// redirecting instead of proxying keeps repo content off our own origin, same
// as camo and avatar
export const GET: RequestHandler = async (event) => {
	const { ref, path } = event.params;

	if (path === "") error(404, "Not found");

	const { git } = await resolveRepoFromParams(event);

	// a branch points at a new commit after every push, so this cannot cache
	const location = blobRawUrl(git, { ref, path });

	return new Response(null, {
		status: 302,
		headers: {
			location,
			"cache-control": "public, no-cache"
		}
	});
};
