From c67346626abfe6a7b1f7039e392d6bffcd921ee4 Mon Sep 17 00:00:00 2001 From: whm <973418690@qq.com> Date: Wed, 18 Mar 2026 18:31:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=8A=E4=BC=A0=E7=9A=84?= =?UTF-8?q?=E6=8C=82=E8=BD=BD=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 3 ++- server/handlers/module_upload.go | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index e5f9a51..fa241f1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,11 +13,12 @@ services: container_name: yh_api volumes: - ./deploy/api:/app:ro - - ./data/uploads:/app/uploads + - ./data/uploads:/uploads env_file: - ./server/.env environment: - PORT=8088 + - UPLOAD_DIR=/uploads - MONGODB_URI=${MONGODB_URI:-mongodb://mongo:27017} - MONGODB_DB=${MONGODB_DB:-yxd-agent-testing} - GIN_MODE=release diff --git a/server/handlers/module_upload.go b/server/handlers/module_upload.go index 1d29cfa..8e3b82d 100644 --- a/server/handlers/module_upload.go +++ b/server/handlers/module_upload.go @@ -19,7 +19,13 @@ import ( "github.com/gin-gonic/gin" ) -const uploadDir = "uploads" +// getUploadDir 上传根目录:容器内通过 UPLOAD_DIR 挂载到独立可写路径(如 /uploads),避免 /app 只读 +func getUploadDir() string { + if d := os.Getenv("UPLOAD_DIR"); d != "" { + return d + } + return "uploads" +} // pathPrefix 站点下相对路径前缀,用于多级目录 func pathPrefix(siteID string) string { @@ -99,7 +105,7 @@ func listSubDirs(c *gin.Context, siteID, currentPath string) []string { } } // 再扫描物理目录 - baseDir := filepath.Join(uploadDir, filepath.FromSlash(prefix)) + baseDir := filepath.Join(getUploadDir(), filepath.FromSlash(prefix)) entries, _ := os.ReadDir(baseDir) for _, e := range entries { if e.IsDir() { @@ -130,7 +136,7 @@ func UploadSiteAsset(c *gin.Context) { folder := c.PostForm("folder") downloadable := c.PostForm("downloadable") == "true" || c.PostForm("downloadable") == "1" - baseDir := filepath.Join(uploadDir, "sites", siteID, filepath.Clean(folder)) + baseDir := filepath.Join(getUploadDir(), "sites", siteID, filepath.Clean(folder)) if err := os.MkdirAll(baseDir, 0755); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "创建目录失败"}) return @@ -142,7 +148,7 @@ func UploadSiteAsset(c *gin.Context) { saveName := nameNoExt + "_" + time.Now().Format("20060102150405") + ext relPath := filepath.Join("sites", siteID, filepath.Clean(folder), saveName) relPath = filepath.ToSlash(relPath) - destPath := filepath.Join(uploadDir, filepath.FromSlash(relPath)) + destPath := filepath.Join(getUploadDir(), filepath.FromSlash(relPath)) if err := c.SaveUploadedFile(file, destPath); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "保存文件失败"}) @@ -204,7 +210,7 @@ func DeleteSiteAsset(c *gin.Context) { return } - fullPath := filepath.Join(uploadDir, filepath.FromSlash(asset.FilePath)) + fullPath := filepath.Join(getUploadDir(), filepath.FromSlash(asset.FilePath)) os.Remove(fullPath) _, err = coll.DeleteOne(ctx, bson.M{"_id": oid}) @@ -237,7 +243,7 @@ func CreateSiteFolder(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": "无效的目录路径"}) return } - baseDir := filepath.Join(uploadDir, "sites", siteID, clean) + baseDir := filepath.Join(getUploadDir(), "sites", siteID, clean) if err := os.MkdirAll(baseDir, 0755); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "创建目录失败"}) return