[新增] OpenFlare Pages

This commit is contained in:
ryan
2026-06-03 17:48:01 +08:00
parent 1088086399
commit 36ae6247f9
5 changed files with 186 additions and 70 deletions
+54 -1
View File
@@ -22,7 +22,7 @@ import (
const (
pagesMaxDeploymentFiles = 1000
pagesMaxDeploymentBytes = 25 * 1024 * 1024
pagesMaxDeploymentBytes = 100 * 1024 * 1024
defaultPagesEntryFile = "index.html"
defaultPagesFallbackPath = "/index.html"
)
@@ -560,12 +560,60 @@ func persistPagesUploadTemp(fileHeader *multipart.FileHeader) (string, string, e
return temp.Name(), hex.EncodeToString(hash.Sum(nil)), nil
}
func findCommonRootPrefix(files []*zip.File) (string, error) {
var firstFilePath string
hasMultipleFiles := false
for _, item := range files {
normalizedPath, skip, err := normalizePagesZipPath(item.Name)
if err != nil {
return "", err
}
if skip {
continue
}
if firstFilePath == "" {
firstFilePath = normalizedPath
} else {
hasMultipleFiles = true
}
}
if firstFilePath == "" {
return "", nil
}
parts := strings.Split(firstFilePath, "/")
if len(parts) <= 1 {
return "", nil
}
commonPrefix := parts[0] + "/"
if hasMultipleFiles {
for _, item := range files {
normalizedPath, skip, err := normalizePagesZipPath(item.Name)
if err != nil {
return "", err
}
if skip {
continue
}
if !strings.HasPrefix(normalizedPath, commonPrefix) {
return "", nil
}
}
}
return commonPrefix, nil
}
func inspectPagesZip(zipPath string, entryFile string) (*pagesDeploymentManifest, error) {
reader, err := zip.OpenReader(zipPath)
if err != nil {
return nil, errors.New("Pages 部署包不是有效 zip 文件")
}
defer reader.Close()
commonPrefix, err := findCommonRootPrefix(reader.File)
if err != nil {
return nil, err
}
manifest := &pagesDeploymentManifest{
Files: []model.PagesDeploymentFile{},
EntryFile: entryFile,
@@ -579,6 +627,11 @@ func inspectPagesZip(zipPath string, entryFile string) (*pagesDeploymentManifest
if skip {
continue
}
if commonPrefix != "" {
normalizedPath = strings.TrimPrefix(normalizedPath, commonPrefix)
}
if item.FileInfo().Mode()&os.ModeSymlink != 0 {
return nil, fmt.Errorf("Pages 部署包不支持符号链接: %s", normalizedPath)
}
+28
View File
@@ -243,3 +243,31 @@ func TestDeletePagesDeploymentRejectsActiveDeployment(t *testing.T) {
t.Fatalf("expected active deployment to remain: %v", err)
}
}
func TestUploadPagesDeploymentWithTopLevelFolder(t *testing.T) {
setupServiceTestDB(t)
project, err := CreatePagesProject(PagesProjectInput{
Name: "Folder Site",
Slug: "folder-site",
Enabled: true,
})
if err != nil {
t.Fatalf("CreatePagesProject failed: %v", err)
}
// Upload a zip with all files inside a top-level directory "Speed-Test-source/"
uploadHeader := multipartFileHeader(t, "site.zip", testPagesZip(t, map[string]string{
"Speed-Test-source/index.html": "<h1>Hello Pages</h1>",
"Speed-Test-source/assets/app.js": "console.log('pages')",
}))
deployment, err := UploadPagesDeployment(project.ID, uploadHeader, "index.html", "root")
if err != nil {
t.Fatalf("UploadPagesDeployment with folder failed: %v", err)
}
if deployment.FileCount != 2 {
t.Fatalf("expected 2 files, got %d", deployment.FileCount)
}
if deployment.EntryFile != "index.html" {
t.Fatalf("expected EntryFile to be index.html, got %q", deployment.EntryFile)
}
}