103 lines
3.1 KiB
Go
103 lines
3.1 KiB
Go
package httpapi
|
||
|
||
import (
|
||
"net/http"
|
||
"path/filepath"
|
||
"sea-vhost/internal/nodefiles"
|
||
"strings"
|
||
)
|
||
|
||
func (s *Server) fileActions(w http.ResponseWriter, r *http.Request) {
|
||
id, err := projectID(r)
|
||
if err != nil {
|
||
writeError(w, 400, "主机编号无效")
|
||
return
|
||
}
|
||
user := currentUser(r)
|
||
project, err := s.store.Project(r.Context(), *user.TenantID, id)
|
||
if err != nil {
|
||
writeError(w, 404, "主机不存在")
|
||
return
|
||
}
|
||
root, err := s.projects.OpenFiles(r.Context(), project.Path)
|
||
if err != nil {
|
||
writeError(w, 503, "文件服务不可用")
|
||
return
|
||
}
|
||
defer root.Close()
|
||
if r.Method == "GET" {
|
||
out, e := root.Action(nodefiles.Request{Operation: "trash_list"})
|
||
if e != nil {
|
||
writeError(w, 422, "无法读取回收站")
|
||
return
|
||
}
|
||
writeJSON(w, 200, map[string]any{"entries": out.Trash})
|
||
return
|
||
}
|
||
var input struct {
|
||
Action string `json:"action"`
|
||
Path string `json:"path"`
|
||
To string `json:"to"`
|
||
}
|
||
if decodeJSON(r, &input) != nil {
|
||
writeError(w, 400, "文件操作格式无效")
|
||
return
|
||
}
|
||
if !map[string]bool{"mkdir": true, "move": true, "copy": true, "zip": true, "extract": true, "restore": true}[input.Action] {
|
||
writeError(w, 422, "不支持此操作")
|
||
return
|
||
}
|
||
if input.Action != "restore" {
|
||
source, e := safeProjectName(project.Path, input.Path)
|
||
if e != nil || source == "." {
|
||
writeError(w, 400, "源路径无效")
|
||
return
|
||
}
|
||
input.Path = filepath.ToSlash(source)
|
||
if input.Action=="mkdir"{for _,part:=range strings.Split(input.Path,"/"){if strings.HasPrefix(part,".sea-")||part==".user.ini"{writeError(w,400,"不能创建系统保留目录");return}}}
|
||
}
|
||
if input.Action != "mkdir" {
|
||
dest, e := safeProjectName(project.Path, input.To)
|
||
if e != nil || dest == "." {
|
||
writeError(w, 400, "目标路径无效")
|
||
return
|
||
}
|
||
input.To = filepath.ToSlash(dest)
|
||
}
|
||
if input.Action == "move" {
|
||
sites, e := s.store.ListSites(r.Context(), project.TenantID, id)
|
||
if e != nil {
|
||
writeError(w, 503, "无法核对运行目录")
|
||
return
|
||
}
|
||
for _, site := range sites {
|
||
run := strings.TrimPrefix(site.RunPath, "/")
|
||
if run == input.Path || strings.HasPrefix(run, input.Path+"/") {
|
||
writeError(w, 409, "该目录仍被网站使用,请先修改网站运行目录")
|
||
return
|
||
}
|
||
}
|
||
}
|
||
if err = s.projects.CheckStorageWrite(r.Context(), project.TenantID, 0); err != nil {
|
||
writeFileQuotaError(w, err)
|
||
return
|
||
}
|
||
plan, usage, err := s.store.PlanAndUsage(r.Context(), project.TenantID)
|
||
if err != nil {
|
||
writeError(w, 503, "无法读取主机限额")
|
||
return
|
||
}
|
||
budget := uint64(0)
|
||
if usage.StorageBytes < plan.StorageBytes {
|
||
budget = plan.StorageBytes - usage.StorageBytes
|
||
}
|
||
_, err = root.Action(nodefiles.Request{Operation: input.Action, Path: input.Path, To: input.To, Budget: budget})
|
||
if err != nil {
|
||
writeError(w, 422, "文件操作未完成:目标必须不存在,路径须在主机目录内;复制/解压总量不得超过剩余空间及 256 MiB")
|
||
return
|
||
}
|
||
_ = s.projects.CheckStorageWrite(r.Context(), project.TenantID, 0)
|
||
s.logFile(r, project.TenantID, input.Action, "文件操作 "+input.Action+":"+input.Path)
|
||
w.WriteHeader(204)
|
||
}
|