117 lines
2.9 KiB
TypeScript
117 lines
2.9 KiB
TypeScript
import { HandlerContext, Handlers } from "$fresh/server.ts";
|
|
import {
|
|
createDocument,
|
|
getDocument,
|
|
transformDocument,
|
|
} from "@lib/documents.ts";
|
|
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
|
|
import * as tmdb from "@lib/tmdb.ts";
|
|
import { parse, stringify } from "https://deno.land/std@0.194.0/yaml/mod.ts";
|
|
import { formatDate, safeFileName } from "@lib/string.ts";
|
|
import { json } from "@lib/helpers.ts";
|
|
import {
|
|
AccessDeniedError,
|
|
BadRequestError,
|
|
NotFoundError,
|
|
} from "@lib/errors.ts";
|
|
import { getSeries, Series } from "@lib/resource/series.ts";
|
|
|
|
async function updateSeriesMetadata(
|
|
name: string,
|
|
metadata: Partial<Series["meta"]>,
|
|
) {
|
|
const docId = `Media/series/${name}.md`;
|
|
|
|
console.log({ docId, metadata });
|
|
|
|
let currentDoc = await getDocument(docId);
|
|
if (!currentDoc) {
|
|
throw new NotFoundError();
|
|
}
|
|
|
|
if (!currentDoc.startsWith("---\n---\n")) {
|
|
currentDoc = `---\n---\n\n${currentDoc}`;
|
|
}
|
|
|
|
const newDoc = transformDocument(currentDoc, (root) => {
|
|
const frontmatterNode = root.children.find((c) => c.type === "yaml");
|
|
|
|
const frontmatter = frontmatterNode?.value as string;
|
|
|
|
const value = parse(frontmatter) as Series["meta"];
|
|
|
|
const newValue = {
|
|
...metadata,
|
|
date: formatDate(metadata.date),
|
|
...value,
|
|
};
|
|
|
|
frontmatterNode.value = stringify(newValue);
|
|
|
|
return root;
|
|
});
|
|
|
|
console.log(newDoc);
|
|
|
|
return createDocument(docId, newDoc);
|
|
}
|
|
|
|
const POST = async (
|
|
req: Request,
|
|
ctx: HandlerContext,
|
|
): Promise<Response> => {
|
|
const session = ctx.state.session;
|
|
if (!session) {
|
|
throw new AccessDeniedError();
|
|
}
|
|
|
|
const body = await req.json();
|
|
const name = ctx.params.name;
|
|
const { tmdbId } = body;
|
|
if (!name || !tmdbId) {
|
|
throw new BadRequestError();
|
|
}
|
|
|
|
const series = await getSeries(ctx.params.name);
|
|
if (!series) {
|
|
throw new NotFoundError();
|
|
}
|
|
|
|
const seriesDetails = await tmdb.getSeries(tmdbId);
|
|
const seriesCredits = !series.meta.author &&
|
|
await tmdb.getSeriesCredits(tmdbId);
|
|
|
|
const releaseDate = seriesDetails.first_air_date;
|
|
const posterPath = seriesDetails.poster_path;
|
|
const director = seriesCredits &&
|
|
seriesCredits.crew?.filter?.((person) => person.job === "Director")[0];
|
|
|
|
let finalPath = "";
|
|
if (posterPath && !series.meta.image) {
|
|
const poster = await tmdb.getMoviePoster(posterPath);
|
|
const extension = fileExtension(posterPath);
|
|
|
|
finalPath = `Media/series/images/${safeFileName(name)}_cover.${extension}`;
|
|
await createDocument(finalPath, poster);
|
|
}
|
|
|
|
const metadata = {} as Series["meta"];
|
|
if (releaseDate) {
|
|
metadata.date = new Date(releaseDate);
|
|
}
|
|
if (finalPath) {
|
|
metadata.image = finalPath;
|
|
}
|
|
if (director && director.name) {
|
|
metadata.author = director.name;
|
|
}
|
|
|
|
await updateSeriesMetadata(name, metadata);
|
|
|
|
return json(seriesCredits);
|
|
};
|
|
|
|
export const handler: Handlers = {
|
|
POST,
|
|
};
|