feat: 大屏改为纯流式自适应布局,新增KPI带与产线动态拓扑地图
This commit is contained in:
@@ -263,91 +263,4 @@ func AttachmentDiskStatusHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// AttachmentArchiveHandler POST /attachment/archive {dryRun} 归档
|
||||
// 把「早于本地保留年数」的附件移动到 ArchiveDir 并标记 archived=true(保留索引可追溯)。
|
||||
// ArchiveDir 为空时只标记不移动;dryRun=true 时仅返回将被归档的数量。
|
||||
func AttachmentArchiveHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
DryRun bool `json:"dryRun"`
|
||||
}
|
||||
_ = httpx.ParseJSON(r, &req)
|
||||
cfg := svcCtx.Config.Upload
|
||||
years := cfg.LocalRetentionYears
|
||||
if years <= 0 {
|
||||
years = 1
|
||||
}
|
||||
cutYear := time.Now().Year() - years // 早于该年份的附件可归档
|
||||
svc := logic.New(svcCtx)
|
||||
recs, err := svc.ListArchivableAttachments(r.Context())
|
||||
if err != nil {
|
||||
httpx.Fail(w, 3314, err.Error())
|
||||
return
|
||||
}
|
||||
moved, marked, failed := 0, 0, 0
|
||||
for _, rec := range recs {
|
||||
if rec.CreatedAt.Year() > cutYear-1 {
|
||||
continue
|
||||
}
|
||||
if req.DryRun {
|
||||
marked++
|
||||
continue
|
||||
}
|
||||
if cfg.ArchiveDir != "" {
|
||||
src, ok1 := safeAbsPath(cfg.Dir, rec.FilePath)
|
||||
dst, ok2 := safeAbsPath(cfg.ArchiveDir, rec.FilePath)
|
||||
if !ok1 || !ok2 {
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
if err := os.Rename(src, dst); err != nil {
|
||||
if err2 := copyFile(src, dst); err2 != nil {
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
_ = os.Remove(src)
|
||||
}
|
||||
moved++
|
||||
}
|
||||
if err := svc.MarkArchived(r.Context(), rec.ID); err != nil {
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
marked++
|
||||
}
|
||||
if !req.DryRun {
|
||||
svcCtx.EventLog.Write(r.Context(), "attachment.archive", "", operator(r, ""), "attachment", "archive",
|
||||
fmt.Sprintf("附件归档:移动%d 标记%d 失败%d(早于 %d 年)", moved, marked, failed, cutYear),
|
||||
map[string]any{"moved": moved, "marked": marked, "failed": failed})
|
||||
}
|
||||
httpx.Ok(w, map[string]any{
|
||||
"moved": moved, "marked": marked, "failed": failed,
|
||||
"cutYear": cutYear, "archiveDir": cfg.ArchiveDir, "dryRun": req.DryRun,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// copyFile 兜底复制(跨盘符 Rename 失败时用)。
|
||||
func copyFile(src, dst string) error {
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := out.ReadFrom(in); err != nil {
|
||||
_ = out.Close()
|
||||
return err
|
||||
}
|
||||
return out.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user