修改上传的挂载路径
This commit is contained in:
@@ -13,11 +13,12 @@ services:
|
|||||||
container_name: yh_api
|
container_name: yh_api
|
||||||
volumes:
|
volumes:
|
||||||
- ./deploy/api:/app:ro
|
- ./deploy/api:/app:ro
|
||||||
- ./data/uploads:/app/uploads
|
- ./data/uploads:/uploads
|
||||||
env_file:
|
env_file:
|
||||||
- ./server/.env
|
- ./server/.env
|
||||||
environment:
|
environment:
|
||||||
- PORT=8088
|
- PORT=8088
|
||||||
|
- UPLOAD_DIR=/uploads
|
||||||
- MONGODB_URI=${MONGODB_URI:-mongodb://mongo:27017}
|
- MONGODB_URI=${MONGODB_URI:-mongodb://mongo:27017}
|
||||||
- MONGODB_DB=${MONGODB_DB:-yxd-agent-testing}
|
- MONGODB_DB=${MONGODB_DB:-yxd-agent-testing}
|
||||||
- GIN_MODE=release
|
- GIN_MODE=release
|
||||||
|
|||||||
@@ -19,7 +19,13 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"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 站点下相对路径前缀,用于多级目录
|
// pathPrefix 站点下相对路径前缀,用于多级目录
|
||||||
func pathPrefix(siteID string) string {
|
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)
|
entries, _ := os.ReadDir(baseDir)
|
||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
if e.IsDir() {
|
if e.IsDir() {
|
||||||
@@ -130,7 +136,7 @@ func UploadSiteAsset(c *gin.Context) {
|
|||||||
|
|
||||||
folder := c.PostForm("folder")
|
folder := c.PostForm("folder")
|
||||||
downloadable := c.PostForm("downloadable") == "true" || c.PostForm("downloadable") == "1"
|
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 {
|
if err := os.MkdirAll(baseDir, 0755); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建目录失败"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建目录失败"})
|
||||||
return
|
return
|
||||||
@@ -142,7 +148,7 @@ func UploadSiteAsset(c *gin.Context) {
|
|||||||
saveName := nameNoExt + "_" + time.Now().Format("20060102150405") + ext
|
saveName := nameNoExt + "_" + time.Now().Format("20060102150405") + ext
|
||||||
relPath := filepath.Join("sites", siteID, filepath.Clean(folder), saveName)
|
relPath := filepath.Join("sites", siteID, filepath.Clean(folder), saveName)
|
||||||
relPath = filepath.ToSlash(relPath)
|
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 {
|
if err := c.SaveUploadedFile(file, destPath); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "保存文件失败"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "保存文件失败"})
|
||||||
@@ -204,7 +210,7 @@ func DeleteSiteAsset(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fullPath := filepath.Join(uploadDir, filepath.FromSlash(asset.FilePath))
|
fullPath := filepath.Join(getUploadDir(), filepath.FromSlash(asset.FilePath))
|
||||||
os.Remove(fullPath)
|
os.Remove(fullPath)
|
||||||
|
|
||||||
_, err = coll.DeleteOne(ctx, bson.M{"_id": oid})
|
_, err = coll.DeleteOne(ctx, bson.M{"_id": oid})
|
||||||
@@ -237,7 +243,7 @@ func CreateSiteFolder(c *gin.Context) {
|
|||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的目录路径"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的目录路径"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
baseDir := filepath.Join(uploadDir, "sites", siteID, clean)
|
baseDir := filepath.Join(getUploadDir(), "sites", siteID, clean)
|
||||||
if err := os.MkdirAll(baseDir, 0755); err != nil {
|
if err := os.MkdirAll(baseDir, 0755); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建目录失败"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建目录失败"})
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user