enzostvs HF Staff commited on
Commit
cd5dbc3
·
1 Parent(s): dbf8f03

Fix promote issue

Browse files
app/api/me/projects/[namespace]/[repoId]/commits/[commitId]/promote/route.ts CHANGED
@@ -1,5 +1,5 @@
1
  import { NextRequest, NextResponse } from "next/server";
2
- import { RepoDesignation, listFiles, spaceInfo, uploadFiles, deleteFiles } from "@huggingface/hub";
3
 
4
  import { isAuthenticated } from "@/lib/auth";
5
  import { Page } from "@/types";
@@ -49,13 +49,13 @@ export async function POST(
49
  );
50
  }
51
 
52
- // Fetch files from the specific commit
53
  const files: File[] = [];
54
  const pages: Page[] = [];
 
55
  const allowedExtensions = ["html", "md", "css", "js", "json", "txt"];
 
56
  const commitFilePaths: Set<string> = new Set();
57
 
58
- // Get all files from the specific commit
59
  for await (const fileInfo of listFiles({
60
  repo,
61
  accessToken: user.token as string,
@@ -63,26 +63,24 @@ export async function POST(
63
  })) {
64
  const fileExtension = fileInfo.path.split('.').pop()?.toLowerCase();
65
 
66
- if (allowedExtensions.includes(fileExtension || "")) {
67
  commitFilePaths.add(fileInfo.path);
68
 
69
- // Fetch the file content from the specific commit
70
- const response = await fetch(
71
- `https://huggingface.co/spaces/${namespace}/${repoId}/raw/${commitId}/${fileInfo.path}`
72
- );
 
 
 
 
73
 
74
- if (response.ok) {
75
- const content = await response.text();
76
  let mimeType = "text/plain";
77
 
78
  switch (fileExtension) {
79
  case "html":
80
  mimeType = "text/html";
81
- // Add HTML files to pages array for client-side setPages
82
- pages.push({
83
- path: fileInfo.path,
84
- html: content,
85
- });
86
  break;
87
  case "css":
88
  mimeType = "text/css";
@@ -93,18 +91,62 @@ export async function POST(
93
  case "json":
94
  mimeType = "application/json";
95
  break;
96
- case "md":
97
- mimeType = "text/markdown";
98
- break;
 
 
 
 
 
 
 
 
 
99
  }
100
 
101
  const file = new File([content], fileInfo.path, { type: mimeType });
102
  files.push(file);
103
  }
104
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  }
106
 
107
- // Get files currently in main branch to identify files to delete
108
  const mainBranchFilePaths: Set<string> = new Set();
109
  for await (const fileInfo of listFiles({
110
  repo,
@@ -118,7 +160,6 @@ export async function POST(
118
  }
119
  }
120
 
121
- // Identify files to delete (exist in main but not in commit)
122
  const filesToDelete: string[] = [];
123
  for (const mainFilePath of mainBranchFilePaths) {
124
  if (!commitFilePaths.has(mainFilePath)) {
@@ -133,7 +174,6 @@ export async function POST(
133
  );
134
  }
135
 
136
- // Delete files that exist in main but not in the commit being promoted
137
  if (filesToDelete.length > 0) {
138
  await deleteFiles({
139
  repo,
@@ -144,7 +184,6 @@ export async function POST(
144
  });
145
  }
146
 
147
- // Upload the files to the main branch with a promotion commit message
148
  if (files.length > 0) {
149
  await uploadFiles({
150
  repo,
@@ -161,6 +200,7 @@ export async function POST(
161
  message: "Version promoted successfully",
162
  promotedCommit: commitId,
163
  pages: pages,
 
164
  },
165
  { status: 200 }
166
  );
 
1
  import { NextRequest, NextResponse } from "next/server";
2
+ import { RepoDesignation, listFiles, spaceInfo, uploadFiles, deleteFiles, downloadFile } from "@huggingface/hub";
3
 
4
  import { isAuthenticated } from "@/lib/auth";
5
  import { Page } from "@/types";
 
49
  );
50
  }
51
 
 
52
  const files: File[] = [];
53
  const pages: Page[] = [];
54
+ const mediaFiles: string[] = [];
55
  const allowedExtensions = ["html", "md", "css", "js", "json", "txt"];
56
+ 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"];
57
  const commitFilePaths: Set<string> = new Set();
58
 
 
59
  for await (const fileInfo of listFiles({
60
  repo,
61
  accessToken: user.token as string,
 
63
  })) {
64
  const fileExtension = fileInfo.path.split('.').pop()?.toLowerCase();
65
 
66
+ if (fileInfo.path.endsWith(".html") || fileInfo.path.endsWith(".css") || fileInfo.path.endsWith(".js") || fileInfo.path.endsWith(".json")) {
67
  commitFilePaths.add(fileInfo.path);
68
 
69
+ const blob = await downloadFile({
70
+ repo,
71
+ accessToken: user.token as string,
72
+ path: fileInfo.path,
73
+ revision: commitId,
74
+ raw: true
75
+ });
76
+ const content = await blob?.text();
77
 
78
+ if (content) {
 
79
  let mimeType = "text/plain";
80
 
81
  switch (fileExtension) {
82
  case "html":
83
  mimeType = "text/html";
 
 
 
 
 
84
  break;
85
  case "css":
86
  mimeType = "text/css";
 
91
  case "json":
92
  mimeType = "application/json";
93
  break;
94
+ }
95
+
96
+ if (fileInfo.path === "index.html") {
97
+ pages.unshift({
98
+ path: fileInfo.path,
99
+ html: content,
100
+ });
101
+ } else {
102
+ pages.push({
103
+ path: fileInfo.path,
104
+ html: content,
105
+ });
106
  }
107
 
108
  const file = new File([content], fileInfo.path, { type: mimeType });
109
  files.push(file);
110
  }
111
  }
112
+ else if (fileInfo.type === "directory" && (["videos", "images", "audio"].includes(fileInfo.path) || fileInfo.path === "components")) {
113
+ for await (const subFileInfo of listFiles({
114
+ repo,
115
+ accessToken: user.token as string,
116
+ revision: commitId,
117
+ path: fileInfo.path,
118
+ })) {
119
+ if (subFileInfo.path.includes("components")) {
120
+ commitFilePaths.add(subFileInfo.path);
121
+ const blob = await downloadFile({
122
+ repo,
123
+ accessToken: user.token as string,
124
+ path: subFileInfo.path,
125
+ revision: commitId,
126
+ raw: true
127
+ });
128
+ const content = await blob?.text();
129
+
130
+ if (content) {
131
+ pages.push({
132
+ path: subFileInfo.path,
133
+ html: content,
134
+ });
135
+
136
+ const file = new File([content], subFileInfo.path, { type: "text/html" });
137
+ files.push(file);
138
+ }
139
+ } else if (allowedFilesExtensions.includes(subFileInfo.path.split(".").pop() || "")) {
140
+ commitFilePaths.add(subFileInfo.path);
141
+ mediaFiles.push(`https://huggingface.co/spaces/${namespace}/${repoId}/resolve/main/${subFileInfo.path}`);
142
+ }
143
+ }
144
+ }
145
+ else if (allowedExtensions.includes(fileExtension || "")) {
146
+ commitFilePaths.add(fileInfo.path);
147
+ }
148
  }
149
 
 
150
  const mainBranchFilePaths: Set<string> = new Set();
151
  for await (const fileInfo of listFiles({
152
  repo,
 
160
  }
161
  }
162
 
 
163
  const filesToDelete: string[] = [];
164
  for (const mainFilePath of mainBranchFilePaths) {
165
  if (!commitFilePaths.has(mainFilePath)) {
 
174
  );
175
  }
176
 
 
177
  if (filesToDelete.length > 0) {
178
  await deleteFiles({
179
  repo,
 
184
  });
185
  }
186
 
 
187
  if (files.length > 0) {
188
  await uploadFiles({
189
  repo,
 
200
  message: "Version promoted successfully",
201
  promotedCommit: commitId,
202
  pages: pages,
203
+ files: mediaFiles,
204
  },
205
  { status: 200 }
206
  );
components/editor/preview/index.tsx CHANGED
@@ -30,6 +30,7 @@ export const Preview = ({ isNew }: { isNew: boolean }) => {
30
  setCurrentPage,
31
  previewPage,
32
  setPreviewPage,
 
33
  } = useEditor();
34
  const {
35
  isEditableModeEnabled,
@@ -300,6 +301,7 @@ export const Preview = ({ isNew }: { isNew: boolean }) => {
300
  setCurrentCommit(null);
301
  setPages(res.data.pages);
302
  setCurrentPage(res.data.pages[0].path);
 
303
  setPreviewPage(res.data.pages[0].path);
304
  toast.success("Version promoted successfully");
305
  }
 
30
  setCurrentPage,
31
  previewPage,
32
  setPreviewPage,
33
+ setLastSavedPages,
34
  } = useEditor();
35
  const {
36
  isEditableModeEnabled,
 
301
  setCurrentCommit(null);
302
  setPages(res.data.pages);
303
  setCurrentPage(res.data.pages[0].path);
304
+ setLastSavedPages(res.data.pages);
305
  setPreviewPage(res.data.pages[0].path);
306
  toast.success("Version promoted successfully");
307
  }