Spaces:
Running
Running
Fix promote issue
Browse filesFiles changed (2)
1234543 unmodified lines495051525354555657585960611 unmodified line63646566676869707172737475767778798081828384858687884 unmodified lines939495969798991001011021031041051061071081091107 unmodified lines1181191201211221231248 unmodified lines1331341351361371381394 unmodified lines14414514614714814915010 unmodified lines161162163164165166import { NextRequest, NextResponse } from "next/server";import { RepoDesignation, listFiles, spaceInfo, uploadFiles, deleteFiles } from "@huggingface/hub";import { isAuthenticated } from "@/lib/auth";import { Page } from "@/types";43 unmodified lines);}// Fetch files from the specific commitconst files: File[] = [];const pages: Page[] = [];const allowedExtensions = ["html", "md", "css", "js", "json", "txt"];const commitFilePaths: Set<string> = new Set();// Get all files from the specific commitfor await (const fileInfo of listFiles({repo,accessToken: user.token as string,1 unmodified line})) {const fileExtension = fileInfo.path.split('.').pop()?.toLowerCase();if (allowedExtensions.includes(fileExtension || "")) {commitFilePaths.add(fileInfo.path);// Fetch the file content from the specific commitconst response = await fetch(`https://huggingface.co/spaces/${namespace}/${repoId}/raw/${commitId}/${fileInfo.path}`);if (response.ok) {const content = await response.text();let mimeType = "text/plain";switch (fileExtension) {case "html":mimeType = "text/html";// Add HTML files to pages array for client-side setPagespages.push({path: fileInfo.path,html: content,});break;case "css":mimeType = "text/css";4 unmodified linescase "json":mimeType = "application/json";break;case "md":mimeType = "text/markdown";break;}const file = new File([content], fileInfo.path, { type: mimeType });files.push(file);}}}// Get files currently in main branch to identify files to deleteconst mainBranchFilePaths: Set<string> = new Set();for await (const fileInfo of listFiles({repo,7 unmodified lines}}// Identify files to delete (exist in main but not in commit)const filesToDelete: string[] = [];for (const mainFilePath of mainBranchFilePaths) {if (!commitFilePaths.has(mainFilePath)) {8 unmodified lines);}// Delete files that exist in main but not in the commit being promotedif (filesToDelete.length > 0) {await deleteFiles({repo,4 unmodified lines});}// Upload the files to the main branch with a promotion commit messageif (files.length > 0) {await uploadFiles({repo,10 unmodified linesmessage: "Version promoted successfully",promotedCommit: commitId,pages: pages,},{ status: 200 });1234543 unmodified lines495051525354555657585960611 unmodified line6364656667686970717273747576777879808182838485864 unmodified lines9192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511527 unmodified lines1601611621631641658 unmodified lines1741751761771781794 unmodified lines18418518618718818910 unmodified lines200201202203204205206import { NextRequest, NextResponse } from "next/server";import { RepoDesignation, listFiles, spaceInfo, uploadFiles, deleteFiles, downloadFile } from "@huggingface/hub";import { isAuthenticated } from "@/lib/auth";import { Page } from "@/types";43 unmodified lines);}const files: File[] = [];const pages: Page[] = [];const mediaFiles: string[] = [];const allowedExtensions = ["html", "md", "css", "js", "json", "txt"];const allowedFilesExtensions = ["jpg", "jpeg", "png", "gif", "svg", "webp", "avif", "heic", "heif", "ico", "bmp", "tiff", "tif", "mp4", "webm", "ogg", "avi", "mov", "mp3", "wav", "ogg", "aac", "m4a"];const commitFilePaths: Set<string> = new Set();for await (const fileInfo of listFiles({repo,accessToken: user.token as string,1 unmodified line})) {const fileExtension = fileInfo.path.split('.').pop()?.toLowerCase();if (fileInfo.path.endsWith(".html") || fileInfo.path.endsWith(".css") || fileInfo.path.endsWith(".js") || fileInfo.path.endsWith(".json")) {commitFilePaths.add(fileInfo.path);const blob = await downloadFile({repo,accessToken: user.token as string,path: fileInfo.path,revision: commitId,raw: true});const content = await blob?.text();if (content) {let mimeType = "text/plain";switch (fileExtension) {case "html":mimeType = "text/html";break;case "css":mimeType = "text/css";4 unmodified linescase "json":mimeType = "application/json";break;}if (fileInfo.path === "index.html") {pages.unshift({path: fileInfo.path,html: content,});} else {pages.push({path: fileInfo.path,html: content,});}const file = new File([content], fileInfo.path, { type: mimeType });files.push(file);}}else if (fileInfo.type === "directory" && (["videos", "images", "audio"].includes(fileInfo.path) || fileInfo.path === "components")) {for await (const subFileInfo of listFiles({repo,accessToken: user.token as string,revision: commitId,path: fileInfo.path,})) {if (subFileInfo.path.includes("components")) {commitFilePaths.add(subFileInfo.path);const blob = await downloadFile({repo,accessToken: user.token as string,path: subFileInfo.path,revision: commitId,raw: true});const content = await blob?.text();if (content) {pages.push({path: subFileInfo.path,html: content,});const file = new File([content], subFileInfo.path, { type: "text/html" });files.push(file);}} else if (allowedFilesExtensions.includes(subFileInfo.path.split(".").pop() || "")) {commitFilePaths.add(subFileInfo.path);mediaFiles.push(`https://huggingface.co/spaces/${namespace}/${repoId}/resolve/main/${subFileInfo.path}`);}}}else if (allowedExtensions.includes(fileExtension || "")) {commitFilePaths.add(fileInfo.path);}}const mainBranchFilePaths: Set<string> = new Set();for await (const fileInfo of listFiles({repo,7 unmodified lines}}const filesToDelete: string[] = [];for (const mainFilePath of mainBranchFilePaths) {if (!commitFilePaths.has(mainFilePath)) {8 unmodified lines);}if (filesToDelete.length > 0) {await deleteFiles({repo,4 unmodified lines});}if (files.length > 0) {await uploadFiles({repo,10 unmodified linesmessage: "Version promoted successfully",promotedCommit: commitId,pages: pages,files: mediaFiles,},{ status: 200 });
29 unmodified lines303132333435264 unmodified lines30030130230330430529 unmodified linessetCurrentPage,previewPage,setPreviewPage,} = useEditor();const {isEditableModeEnabled,264 unmodified linessetCurrentCommit(null);setPages(res.data.pages);setCurrentPage(res.data.pages[0].path);setPreviewPage(res.data.pages[0].path);toast.success("Version promoted successfully");}29 unmodified lines30313233343536264 unmodified lines30130230330430530630729 unmodified linessetCurrentPage,previewPage,setPreviewPage,setLastSavedPages,} = useEditor();const {isEditableModeEnabled,264 unmodified linessetCurrentCommit(null);setPages(res.data.pages);setCurrentPage(res.data.pages[0].path);setLastSavedPages(res.data.pages);setPreviewPage(res.data.pages[0].path);toast.success("Version promoted successfully");}
app/api/me/projects/[namespace]/[repoId]/commits/[commitId]/promote/route.ts+62-22
components/editor/preview/index.tsx+2