Spaces:
Running
Running
File size: 5,780 Bytes
13ae717 0b4acc5 13ae717 0b4acc5 13ae717 0b4acc5 09515ea 0b4acc5 3cbf49c 0b4acc5 |
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
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") {
return NextResponse.json(
{
ok: false,
error: "Space is not a static space",
},
{ 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 });
}
export async function POST(
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 space = await spaceInfo({
name: namespace + "/" + repoId,
accessToken: user.token as string,
additionalFields: ["author"],
});
if (!space || space.sdk !== "static") {
return NextResponse.json(
{
ok: false,
error: "Space is not a static space",
},
{ status: 404 }
);
}
if (space.author !== user.name) {
return NextResponse.json(
{
ok: false,
error: "Space does not belong to the authenticated user",
},
{ status: 403 }
);
}
const project = await Project.findOne({
user_id: user.id,
space_id: `${namespace}/${repoId}`,
}).lean();
if (project) {
// redirect to the project page if it already exists
return NextResponse.json(
{
ok: false,
error: "Project already exists",
redirect: `/projects/${namespace}/${repoId}`,
},
{ status: 400 }
);
}
const newProject = new Project({
user_id: user.id,
space_id: `${namespace}/${repoId}`,
prompts: [],
});
await newProject.save();
return NextResponse.json(
{
ok: true,
project: {
id: newProject._id,
space_id: newProject.space_id,
prompts: newProject.prompts,
},
},
{ status: 201 }
);
}
|