修复: 网站预览资源路径+七牛目录层级
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -19,10 +18,7 @@ import (
|
||||
sftpclient "github.com/pkg/sftp"
|
||||
)
|
||||
|
||||
var (
|
||||
sftpResRegex = regexp.MustCompile(`(?:src|href|data|poster)=["']([^"']+)["']`)
|
||||
sftpCssUrlRe = regexp.MustCompile(`url\(\s*["']?([^"')]+)["']?\s*\)`)
|
||||
)
|
||||
|
||||
|
||||
// Service SFTP 文件操作服务
|
||||
type Service struct {
|
||||
@@ -383,7 +379,7 @@ func (s *Service) DownloadSiteForPreview(connID string, remotePath string) (stri
|
||||
if err != nil {
|
||||
return htmlLocalPath, nil
|
||||
}
|
||||
resources := sftpExtractResources(string(htmlContent))
|
||||
resources := filesystem.ExtractHtmlResources(string(htmlContent))
|
||||
|
||||
// 5. 下载静态引用资源(嗅探网站根)
|
||||
htmlRemoteDir := keyDir
|
||||
@@ -404,7 +400,7 @@ func (s *Service) DownloadSiteForPreview(connID string, remotePath string) (stri
|
||||
}
|
||||
|
||||
for _, resPath := range resources {
|
||||
if sftpShouldSkip(resPath) {
|
||||
if filesystem.ShouldSkipResource(resPath) {
|
||||
continue
|
||||
}
|
||||
isAbsolute := strings.HasPrefix(resPath, "/")
|
||||
@@ -513,9 +509,7 @@ func sftpSupplementDir(s *Service, c *Client, remoteDir string, tmpDir string, s
|
||||
continue
|
||||
}
|
||||
fullPath := path.Join(remoteDir, entry.Name())
|
||||
relPath := strings.TrimPrefix(fullPath, siteRoot)
|
||||
relPath = strings.TrimPrefix(relPath, "/")
|
||||
localPath := filepath.Join(tmpDir, filepath.FromSlash(relPath))
|
||||
localPath := filepath.Join(tmpDir, filepath.FromSlash(fullPath))
|
||||
if _, err := os.Stat(localPath); err == nil {
|
||||
continue
|
||||
}
|
||||
@@ -707,38 +701,3 @@ func toFileOperationResult(m map[string]interface{}, isDir bool) *filesystem.Fil
|
||||
}
|
||||
}
|
||||
|
||||
// sftpExtractResources 从 HTML 内容提取资源路径
|
||||
func sftpExtractResources(html string) []string {
|
||||
seen := make(map[string]bool)
|
||||
var resources []string
|
||||
add := func(v string) {
|
||||
v = strings.TrimSpace(v)
|
||||
if v != "" && !seen[v] {
|
||||
seen[v] = true
|
||||
resources = append(resources, v)
|
||||
}
|
||||
}
|
||||
for _, m := range sftpResRegex.FindAllStringSubmatch(html, -1) {
|
||||
if len(m) > 1 {
|
||||
add(m[1])
|
||||
}
|
||||
}
|
||||
for _, m := range sftpCssUrlRe.FindAllStringSubmatch(html, -1) {
|
||||
if len(m) > 1 {
|
||||
add(m[1])
|
||||
}
|
||||
}
|
||||
return resources
|
||||
}
|
||||
|
||||
// sftpShouldSkip 判断资源路径是否应跳过
|
||||
func sftpShouldSkip(p string) bool {
|
||||
return strings.HasPrefix(p, "data:") ||
|
||||
strings.HasPrefix(p, "http://") ||
|
||||
strings.HasPrefix(p, "https://") ||
|
||||
strings.HasPrefix(p, "//") ||
|
||||
strings.HasPrefix(p, "#") ||
|
||||
strings.HasPrefix(p, "javascript:") ||
|
||||
strings.HasPrefix(p, "mailto:") ||
|
||||
strings.HasPrefix(p, "blob:")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user