Spaces:
Running
Running
File size: 4,138 Bytes
13ae717 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
import { NextRequest, NextResponse } from "next/server";
import { RepoDesignation, spaceInfo, uploadFile } from "@huggingface/hub";
import { isAuthenticated } from "@/lib/auth";
import Project from "@/models/Project";
import dbConnect from "@/lib/mongodb";
import { getPTag } from "@/lib/utils";
export async function GET(
req: NextRequest,
{ params }: { params: Promise<{ namespace: string; repoId: string }> }
) {
const user = await isAuthenticated();
if (user instanceof NextResponse || !user) {
return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
}
await dbConnect();
const param = await params;
const { namespace, repoId } = param;
const project = await Project.findOne({
user_id: user.id,
space_id: `${namespace}/${repoId}`,
}).lean();
if (!project) {
return NextResponse.json(
{
ok: false,
error: "Project not found",
},
{ status: 404 }
);
}
const space_url = `https://huggingface.co/spaces/${namespace}/${repoId}/raw/main/index.html`;
try {
const space = await spaceInfo({
name: namespace + "/" + repoId,
accessToken: user.token as string,
additionalFields: ["author"],
});
if (!space || space.sdk !== "static" || space.private) {
return NextResponse.json(
{
ok: false,
error: "Space is not a static space or is private",
},
{ status: 404 }
);
}
if (space.author !== user.name) {
return NextResponse.json(
{
ok: false,
error: "Space does not belong to the authenticated user",
},
{ status: 403 }
);
}
const response = await fetch(space_url);
if (!response.ok) {
return NextResponse.json(
{
ok: false,
error: "Failed to fetch space HTML",
},
{ status: 404 }
);
}
let html = await response.text();
// remove the last p tag including this url https://enzostvs-deepsite.hf.space
html = html.replace(getPTag(namespace + "/" + repoId), "");
return NextResponse.json(
{
project: {
...project,
html,
},
ok: true,
},
{ status: 200 }
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.statusCode === 404) {
await Project.deleteOne({
user_id: user.id,
space_id: `${namespace}/${repoId}`,
});
return NextResponse.json(
{ error: "Space not found", ok: false },
{ status: 404 }
);
}
return NextResponse.json(
{ error: error.message, ok: false },
{ status: 500 }
);
}
}
export async function PUT(
req: NextRequest,
{ params }: { params: Promise<{ namespace: string; repoId: string }> }
) {
const user = await isAuthenticated();
if (user instanceof NextResponse || !user) {
return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
}
await dbConnect();
const param = await params;
const { namespace, repoId } = param;
const { html, prompts } = await req.json();
const project = await Project.findOne({
user_id: user.id,
space_id: `${namespace}/${repoId}`,
}).lean();
if (!project) {
return NextResponse.json(
{
ok: false,
error: "Project not found",
},
{ status: 404 }
);
}
const repo: RepoDesignation = {
type: "space",
name: `${namespace}/${repoId}`,
};
const newHtml = html.replace(/<\/body>/, `${getPTag(repo.name)}</body>`);
const file = new File([newHtml], "index.html", { type: "text/html" });
await uploadFile({
repo,
file,
accessToken: user.token as string,
commitTitle: `${prompts[prompts.length - 1]} - Follow Up Deployment`,
});
await Project.updateOne(
{ user_id: user.id, space_id: `${namespace}/${repoId}` },
{
$set: {
prompts: [
...(project && "prompts" in project ? project.prompts : []),
...prompts,
],
},
}
);
return NextResponse.json({ ok: true }, { status: 200 });
}
|