重构: 死代码清理 + 拷贝优化 + 滚动条修复
This commit is contained in:
@@ -2,7 +2,6 @@ package filesystem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -303,52 +302,6 @@ func getContentType(ext string) string {
|
||||
return defaultFileTypeManager.GetMIMEType(ext)
|
||||
}
|
||||
|
||||
// ReadFileAsBase64 读取文件并返回 base64 编码的字符串
|
||||
// 用于读取从 ZIP 提取的临时图片文件
|
||||
func ReadFileAsBase64(filePath string) (string, error) {
|
||||
log.Printf("[ReadFileAsBase64] 读取文件: %s", filePath)
|
||||
|
||||
if !isSafePath(filePath) {
|
||||
return "", fmt.Errorf("路径不安全")
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
fileInfo, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return "", fmt.Errorf("文件不存在: %s", filePath)
|
||||
}
|
||||
return "", fmt.Errorf("无法访问文件: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("[ReadFileAsBase64] 文件大小: %d bytes", fileInfo.Size())
|
||||
|
||||
// 读取文件
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("读取文件失败: %v", err)
|
||||
}
|
||||
|
||||
// 编码为 base64
|
||||
encoded := base64.StdEncoding.EncodeToString(data)
|
||||
log.Printf("[ReadFileAsBase64] 编码成功: 原始=%d, base64=%d", len(data), len(encoded))
|
||||
|
||||
// 获取文件扩展名并确定 MIME 类型
|
||||
ext := strings.ToLower(filepath.Ext(filePath))
|
||||
mimeType := getContentType(ext)
|
||||
|
||||
// 返回 data URI 格式: data:image/png;base64,iVBORw0KG...
|
||||
return fmt.Sprintf("data:%s;base64,%s", mimeType, encoded), nil
|
||||
}
|
||||
|
||||
// HandleLocalFile 处理 /localfs/ 路由的 HTTP 请求
|
||||
// 前端可以请求 http://localhost:18765/localfs/C:/path/to/image.jpg
|
||||
// 注意:此函数与 ServeHTTP 功能重复,建议统一使用 ServeHTTP
|
||||
func HandleLocalFile(w http.ResponseWriter, r *http.Request) {
|
||||
handler := NewLocalFileHandler()
|
||||
handler.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// isAllowedFileType 检查文件类型是否在白名单中
|
||||
func isAllowedFileType(ext string) bool {
|
||||
return defaultFileTypeManager.IsAllowed(ext)
|
||||
|
||||
Reference in New Issue
Block a user