feat: rehashdoc
This commit is contained in:
parent
58adb46323
commit
f543ad1cf4
@ -1,7 +1,17 @@
|
||||
import useSWR from "swr";
|
||||
import useSWR, { useSWRConfig } from "swr";
|
||||
import type { Document } from "dbtype";
|
||||
import { fetcher } from "./fetcher";
|
||||
|
||||
export function useGalleryDoc(id: string) {
|
||||
return useSWR<Document>(`/api/doc/${id}`, fetcher);
|
||||
}
|
||||
|
||||
export function useRehashDoc() {
|
||||
const { mutate } = useSWRConfig();
|
||||
return async (id: string) => {
|
||||
await fetch(`/api/doc/${id}/_rehash`, {
|
||||
method: "POST",
|
||||
});
|
||||
mutate(`/api/doc/${id}`);
|
||||
};
|
||||
}
|
@ -1,10 +1,11 @@
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { useGalleryDoc } from "../hook/useGalleryDoc.ts";
|
||||
import { useGalleryDoc, useRehashDoc } from "../hook/useGalleryDoc.ts";
|
||||
import TagBadge from "@/components/gallery/TagBadge";
|
||||
import StyledLink from "@/components/gallery/StyledLink";
|
||||
import { Link } from "wouter";
|
||||
import { classifyTags } from "../lib/classifyTags.tsx";
|
||||
import { DescTagItem, DescItem } from "../components/gallery/DescItem.tsx";
|
||||
import { Button } from "@/components/ui/button.tsx";
|
||||
|
||||
export interface ContentInfoPageProps {
|
||||
params: {
|
||||
@ -14,6 +15,7 @@ export interface ContentInfoPageProps {
|
||||
|
||||
export function ContentInfoPage({ params }: ContentInfoPageProps) {
|
||||
const { data, error, isLoading } = useGalleryDoc(params.id);
|
||||
const rehashDoc = useRehashDoc();
|
||||
|
||||
if (isLoading) {
|
||||
return <div className="p-4">Loading...</div>
|
||||
@ -43,7 +45,13 @@ export function ContentInfoPage({ params }: ContentInfoPageProps) {
|
||||
alt={data.title} />
|
||||
</div>
|
||||
</Link>
|
||||
<Card className="flex-1">
|
||||
<Card className="flex-1 relative">
|
||||
<div className="absolute top-0 right-0 p-2">
|
||||
<Button variant="ghost" onClick={async () => {
|
||||
// Rehash
|
||||
await rehashDoc(params.id);
|
||||
}}>Rehash</Button>
|
||||
</div>
|
||||
<CardHeader>
|
||||
<CardTitle>
|
||||
<StyledLink to={contentLocation}>
|
||||
|
@ -15,6 +15,8 @@ import { AllContentRouter } from "./all.ts";
|
||||
import type { ContentLocation } from "./context.ts";
|
||||
import { sendError } from "./error_handler.ts";
|
||||
import { ParseQueryArgString, ParseQueryArray, ParseQueryBoolean, ParseQueryNumber } from "./util.ts";
|
||||
import { oshash } from "src/util/oshash.ts";
|
||||
|
||||
|
||||
const ContentIDHandler = (controller: DocumentAccessor) => async (ctx: Context, next: Next) => {
|
||||
const num = Number.parseInt(ctx.params.num);
|
||||
@ -154,6 +156,34 @@ const ContentHandler = (controller: DocumentAccessor) => async (ctx: Context, ne
|
||||
await next();
|
||||
};
|
||||
|
||||
function RehashContentHandler(controller: DocumentAccessor) {
|
||||
return async (ctx: Context, next: Next) => {
|
||||
const num = Number.parseInt(ctx.params.num);
|
||||
const c = await controller.findById(num);
|
||||
if (c === undefined || c.deleted_at !== null) {
|
||||
return sendError(404);
|
||||
}
|
||||
const filepath = join(c.basepath, c.filename);
|
||||
let new_hash: string;
|
||||
try {
|
||||
new_hash = (await oshash(filepath)).toString();
|
||||
}
|
||||
catch (e) {
|
||||
// if file is not found, return 404
|
||||
if ( (e as NodeJS.ErrnoException).code === "ENOENT") {
|
||||
return sendError(404, "file not found");
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
const r = await controller.update({
|
||||
id: num,
|
||||
content_hash: new_hash,
|
||||
});
|
||||
ctx.body = JSON.stringify(r);
|
||||
ctx.type = "json";
|
||||
};
|
||||
}
|
||||
|
||||
export const getContentRouter = (controller: DocumentAccessor) => {
|
||||
const ret = new Router();
|
||||
ret.get("/search", PerCheck(Per.QueryContent), ContentQueryHandler(controller));
|
||||
@ -167,6 +197,7 @@ export const getContentRouter = (controller: DocumentAccessor) => {
|
||||
ret.del("/:num(\\d+)", AdminOnly, DeleteContentHandler(controller));
|
||||
ret.all("/:num(\\d+)/(.*)", PerCheck(Per.QueryContent), ContentHandler(controller));
|
||||
ret.use("/:num(\\d+)", PerCheck(Per.QueryContent), new AllContentRouter().routes());
|
||||
ret.post("/:num(\\d+)/_rehash", AdminOnly, RehashContentHandler(controller));
|
||||
return ret;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user