修改上传的挂载路径
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user