feat(suffix): 支持根目录重命名、列宽保存、锚定值与文件发布
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -42,6 +43,8 @@ func registerSuffixRoutes(secured *gin.RouterGroup, a *BusinessAPI) {
|
||||
s.PATCH("/columns/:id", a.updateSuffixColumn)
|
||||
s.DELETE("/columns/:id", a.deleteSuffixColumn)
|
||||
s.PUT("/cells", a.saveSuffixCell)
|
||||
s.GET("/col-widths", a.suffixColWidths)
|
||||
s.PUT("/col-widths", a.saveSuffixColWidth)
|
||||
s.GET("/overview", a.suffixOverview)
|
||||
s.POST("/groups", a.createSuffixGroup)
|
||||
s.PUT("/groups/:id", a.renameSuffixGroup)
|
||||
@@ -54,6 +57,7 @@ func registerSuffixRoutes(secured *gin.RouterGroup, a *BusinessAPI) {
|
||||
s.POST("/suffixes/:id/files", a.uploadSuffixFile)
|
||||
s.DELETE("/files/:id", a.deleteSuffixFile)
|
||||
s.GET("/files/:id/download", a.downloadSuffixFile)
|
||||
s.POST("/files/:id/publish", a.publishSuffixFile)
|
||||
}
|
||||
|
||||
func suffixTrimName(raw string, max int) (string, bool) {
|
||||
@@ -61,8 +65,6 @@ func suffixTrimName(raw string, max int) (string, bool) {
|
||||
return name, name != "" && utf8.RuneCountInString(name) <= max
|
||||
}
|
||||
|
||||
const suffixRootNameValue = "按信息价的目录"
|
||||
|
||||
// ---- 目录树 ----
|
||||
|
||||
type suffixDirRow struct {
|
||||
@@ -101,9 +103,6 @@ func (a *BusinessAPI) suffixTree(c *gin.Context) {
|
||||
dirs[p].Children++
|
||||
}
|
||||
}
|
||||
if len(dirs) > 0 {
|
||||
dirs[0].Name = suffixRootNameValue
|
||||
}
|
||||
c.JSON(200, gin.H{"data": gin.H{"dirs": dirs}})
|
||||
}
|
||||
|
||||
@@ -127,7 +126,7 @@ func (a *BusinessAPI) createSuffixDir(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if in.ParentID == 0 {
|
||||
apiError(c, 403, "forbidden", "只能建在「按信息价的目录」之内")
|
||||
apiError(c, 403, "forbidden", "只能建在根目录之内")
|
||||
return
|
||||
}
|
||||
if !a.dirExists(c, in.ParentID) {
|
||||
@@ -158,10 +157,6 @@ func (a *BusinessAPI) dirIsRoot(c *gin.Context, id uint64) bool {
|
||||
|
||||
func (a *BusinessAPI) renameSuffixDir(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if a.dirIsRoot(c, id) {
|
||||
apiError(c, 403, "forbidden", "根目录名称固定,不能改名")
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
@@ -250,15 +245,15 @@ func (a *BusinessAPI) suffixDirPage(c *gin.Context) {
|
||||
colRows.Close()
|
||||
|
||||
type suffixLine struct {
|
||||
ID uint64 `json:"id"`
|
||||
GroupID uint64 `json:"group_id"`
|
||||
GroupName string `json:"group_name"`
|
||||
Name string `json:"name"`
|
||||
Anchor bool `json:"anchor"`
|
||||
ID uint64 `json:"id"`
|
||||
GroupID uint64 `json:"group_id"`
|
||||
GroupName string `json:"group_name"`
|
||||
Name string `json:"name"`
|
||||
AnchorValue string `json:"anchor_value"`
|
||||
}
|
||||
suffixes := []suffixLine{}
|
||||
sfxRows, err := a.db.QueryContext(c,
|
||||
"SELECT s.id,s.group_id,g.name,s.name, EXISTS(SELECT 1 FROM suffix_dir_anchors x WHERE x.dir_id=? AND x.suffix_id=s.id)"+
|
||||
"SELECT s.id,s.group_id,g.name,s.name, COALESCE((SELECT x.value FROM suffix_dir_anchors x WHERE x.dir_id=? AND x.suffix_id=s.id),'')"+
|
||||
" FROM suffixes s JOIN suffix_groups g ON g.id=s.group_id"+
|
||||
" ORDER BY g.sort_order ASC,g.id ASC,s.sort_order ASC,s.id ASC", dirID)
|
||||
if err != nil {
|
||||
@@ -267,7 +262,7 @@ func (a *BusinessAPI) suffixDirPage(c *gin.Context) {
|
||||
}
|
||||
for sfxRows.Next() {
|
||||
var r suffixLine
|
||||
if err := sfxRows.Scan(&r.ID, &r.GroupID, &r.GroupName, &r.Name, &r.Anchor); err != nil {
|
||||
if err := sfxRows.Scan(&r.ID, &r.GroupID, &r.GroupName, &r.Name, &r.AnchorValue); err != nil {
|
||||
sfxRows.Close()
|
||||
apiError(c, 500, "internal_error", "后缀加载失败")
|
||||
return
|
||||
@@ -422,30 +417,73 @@ func (a *BusinessAPI) reorderSuffixColumns(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"data": gin.H{"ok": true, "ordered": len(final)}})
|
||||
}
|
||||
|
||||
// 列更新:name 与 width 至少传一个;重命名在同一目录内查重。
|
||||
func (a *BusinessAPI) updateSuffixColumn(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
var in struct {
|
||||
Width *int `json:"width"`
|
||||
Name *string `json:"name"`
|
||||
Width *int `json:"width"`
|
||||
}
|
||||
if c.ShouldBindJSON(&in) != nil || in.Width == nil {
|
||||
apiError(c, 400, "invalid_request", "width 必填")
|
||||
if c.ShouldBindJSON(&in) != nil || (in.Name == nil && in.Width == nil) {
|
||||
apiError(c, 400, "invalid_request", "name 或 width 至少传一个")
|
||||
return
|
||||
}
|
||||
w := *in.Width
|
||||
if w < 0 || w > 2000 {
|
||||
apiError(c, 400, "invalid_request", "列宽须为 0–2000(0=自适应)")
|
||||
return
|
||||
if in.Name != nil {
|
||||
name, ok := suffixTrimName(*in.Name, 190)
|
||||
if !ok {
|
||||
apiError(c, 400, "invalid_request", "列名须为 1–190 字")
|
||||
return
|
||||
}
|
||||
var dirID uint64
|
||||
if err := a.db.QueryRowContext(c,
|
||||
"SELECT dir_id FROM suffix_columns WHERE id=?", id).Scan(&dirID); err != nil {
|
||||
apiError(c, 404, "not_found", "列不存在")
|
||||
return
|
||||
}
|
||||
var dup int
|
||||
if a.db.QueryRowContext(c,
|
||||
"SELECT 1 FROM suffix_columns WHERE dir_id=? AND name=? AND id<>?", dirID, name, id).Scan(&dup) == nil {
|
||||
apiError(c, 409, "duplicate_name", "这个目录里已有同名列")
|
||||
return
|
||||
}
|
||||
if _, err := a.db.ExecContext(c, "UPDATE suffix_columns SET name=? WHERE id=?", name, id); err != nil {
|
||||
apiError(c, 500, "internal_error", "列重命名失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
res, err := a.db.ExecContext(c, "UPDATE suffix_columns SET width=? WHERE id=?", w, id)
|
||||
if err != nil || rowsAffected(res) == 0 {
|
||||
if in.Width != nil {
|
||||
w := *in.Width
|
||||
if w < 0 || w > 2000 {
|
||||
apiError(c, 400, "invalid_request", "列宽须为 0–2000(0=自适应)")
|
||||
return
|
||||
}
|
||||
if _, err := a.db.ExecContext(c, "UPDATE suffix_columns SET width=? WHERE id=?", w, id); err != nil {
|
||||
apiError(c, 500, "internal_error", "列宽保存失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
var out suffixColumnRow
|
||||
if err := a.db.QueryRowContext(c,
|
||||
"SELECT id,name,is_value,width FROM suffix_columns WHERE id=?", id).
|
||||
Scan(&out.ID, &out.Name, &out.IsValue, &out.Width); err != nil {
|
||||
apiError(c, 404, "not_found", "列不存在")
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{"data": gin.H{"id": id, "width": w}})
|
||||
c.JSON(200, gin.H{"data": out})
|
||||
}
|
||||
|
||||
func (a *BusinessAPI) deleteSuffixColumn(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
var filled int
|
||||
if err := a.db.QueryRowContext(c,
|
||||
"SELECT COUNT(*) FROM suffix_cells WHERE column_id=? AND value<>''", id).Scan(&filled); err != nil {
|
||||
apiError(c, 500, "internal_error", "删除失败")
|
||||
return
|
||||
}
|
||||
if filled > 0 {
|
||||
apiError(c, 409, "column_has_data", "该列已有数据,不能删除")
|
||||
return
|
||||
}
|
||||
res, err := a.db.ExecContext(c, "DELETE FROM suffix_columns WHERE id=?", id)
|
||||
if err != nil || rowsAffected(res) == 0 {
|
||||
apiError(c, 404, "not_found", "列不存在")
|
||||
@@ -494,20 +532,27 @@ func (a *BusinessAPI) saveSuffixCell(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"data": gin.H{"ok": true}})
|
||||
}
|
||||
|
||||
// 锚定行存值:value 非空 upsert,空串删除(未锚定)。
|
||||
func (a *BusinessAPI) setSuffixAnchor(c *gin.Context) {
|
||||
dirID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
var in struct {
|
||||
SuffixID uint64 `json:"suffix_id"`
|
||||
On bool `json:"on"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
if c.ShouldBindJSON(&in) != nil {
|
||||
apiError(c, 400, "invalid_request", "参数无效")
|
||||
return
|
||||
}
|
||||
value := strings.TrimSpace(in.Value)
|
||||
if utf8.RuneCountInString(value) > 190 {
|
||||
apiError(c, 400, "invalid_request", "锚定值最多 190 字")
|
||||
return
|
||||
}
|
||||
var exec error
|
||||
if in.On {
|
||||
if value != "" {
|
||||
_, exec = a.db.ExecContext(c,
|
||||
"INSERT IGNORE INTO suffix_dir_anchors(dir_id,suffix_id) VALUES(?,?)", dirID, in.SuffixID)
|
||||
"INSERT INTO suffix_dir_anchors(dir_id,suffix_id,value) VALUES(?,?,?)"+
|
||||
" ON DUPLICATE KEY UPDATE value=VALUES(value)", dirID, in.SuffixID, value)
|
||||
} else {
|
||||
_, exec = a.db.ExecContext(c,
|
||||
"DELETE FROM suffix_dir_anchors WHERE dir_id=? AND suffix_id=?", dirID, in.SuffixID)
|
||||
@@ -519,6 +564,79 @@ func (a *BusinessAPI) setSuffixAnchor(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"data": gin.H{"ok": true}})
|
||||
}
|
||||
|
||||
// ---- 固定列列宽(序号/后缀/操作等,基本设置与目录工作区各一套,全部可自由设置) ----
|
||||
|
||||
// 默认值与前端 FIXED_COL_DEFAULTS 保持一致;0=自适应(导入文件为 0 时占满剩余宽度)。
|
||||
var suffixColWidthDefaults = map[string]map[string]int{
|
||||
"basic": {"idx": 50, "suffix": 50, "files": 0, "ops": 150},
|
||||
"workspace": {"idx": 50, "suffix": 50, "ops": 80, "anchor": 300},
|
||||
}
|
||||
|
||||
func (a *BusinessAPI) suffixColWidths(c *gin.Context) {
|
||||
out := map[string]map[string]int{}
|
||||
for scope, keys := range suffixColWidthDefaults {
|
||||
out[scope] = map[string]int{}
|
||||
for k, def := range keys {
|
||||
out[scope][k] = def
|
||||
}
|
||||
}
|
||||
rows, err := a.db.QueryContext(c, "SELECT scope,col_key,width FROM suffix_col_widths")
|
||||
if err != nil {
|
||||
apiError(c, 500, "internal_error", "列宽加载失败")
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var scope, key string
|
||||
var w int
|
||||
if rows.Scan(&scope, &key, &w) == nil {
|
||||
if keys, ok := out[scope]; ok {
|
||||
if _, known := keys[key]; known && w >= 0 {
|
||||
keys[key] = w
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
apiError(c, 500, "internal_error", "列宽加载失败")
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{"data": gin.H{"widths": out}})
|
||||
}
|
||||
|
||||
func (a *BusinessAPI) saveSuffixColWidth(c *gin.Context) {
|
||||
var in struct {
|
||||
Scope string `json:"scope"`
|
||||
Key string `json:"key"`
|
||||
Width *int `json:"width"`
|
||||
}
|
||||
if c.ShouldBindJSON(&in) != nil || in.Width == nil {
|
||||
apiError(c, 400, "invalid_request", "参数无效")
|
||||
return
|
||||
}
|
||||
keys, ok := suffixColWidthDefaults[in.Scope]
|
||||
if !ok {
|
||||
apiError(c, 400, "invalid_request", "scope 只能是 basic 或 workspace")
|
||||
return
|
||||
}
|
||||
if _, known := keys[in.Key]; !known {
|
||||
apiError(c, 400, "invalid_request", "未知的固定列 key")
|
||||
return
|
||||
}
|
||||
w := *in.Width
|
||||
if w < 0 || w > 2000 {
|
||||
apiError(c, 400, "invalid_request", "列宽须为 0–2000(0=自适应)")
|
||||
return
|
||||
}
|
||||
if _, err := a.db.ExecContext(c,
|
||||
"INSERT INTO suffix_col_widths(scope,col_key,width) VALUES(?,?,?)"+
|
||||
" ON DUPLICATE KEY UPDATE width=VALUES(width)", in.Scope, in.Key, w); err != nil {
|
||||
apiError(c, 500, "internal_error", "列宽保存失败")
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{"data": gin.H{"scope": in.Scope, "key": in.Key, "width": w}})
|
||||
}
|
||||
|
||||
// ---- 基本设置:分组 + 后缀 + 模板文件 ----
|
||||
|
||||
type suffixFileRow struct {
|
||||
@@ -526,6 +644,7 @@ type suffixFileRow struct {
|
||||
FileName string `json:"file_name"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Published bool `json:"published"`
|
||||
}
|
||||
|
||||
func (a *BusinessAPI) suffixOverview(c *gin.Context) {
|
||||
@@ -583,7 +702,7 @@ func (a *BusinessAPI) suffixOverview(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
fRows, err := a.db.QueryContext(c,
|
||||
"SELECT id,suffix_id,file_name,file_size,created_at FROM suffix_files ORDER BY id ASC")
|
||||
"SELECT id,suffix_id,file_name,file_size,created_at,published FROM suffix_files ORDER BY id ASC")
|
||||
if err != nil {
|
||||
apiError(c, 500, "internal_error", "文件加载失败")
|
||||
return
|
||||
@@ -592,7 +711,7 @@ func (a *BusinessAPI) suffixOverview(c *gin.Context) {
|
||||
for fRows.Next() {
|
||||
var f suffixFileRow
|
||||
var sid uint64
|
||||
if err := fRows.Scan(&f.ID, &sid, &f.FileName, &f.FileSize, &f.CreatedAt); err != nil {
|
||||
if err := fRows.Scan(&f.ID, &sid, &f.FileName, &f.FileSize, &f.CreatedAt, &f.Published); err != nil {
|
||||
apiError(c, 500, "internal_error", "文件加载失败")
|
||||
return
|
||||
}
|
||||
@@ -904,12 +1023,13 @@ type suffixTemplateRootError struct{}
|
||||
|
||||
func (suffixTemplateRootError) Error() string { return "模板存储目录未配置" }
|
||||
|
||||
// 附件统一落盘在 PACKAGE_STORAGE_ROOT/attachments/<后缀id>/ 下(新软件,无旧目录兼容负担)。
|
||||
func suffixTemplateRoot() (string, error) {
|
||||
base := strings.TrimSpace(os.Getenv("PACKAGE_STORAGE_ROOT"))
|
||||
if base == "" {
|
||||
return "", suffixTemplateRootError{}
|
||||
}
|
||||
return filepath.Join(base, "suffix-templates"), nil
|
||||
return filepath.Join(base, "attachments"), nil
|
||||
}
|
||||
|
||||
func suffixUniqueName(dir string, name string) string {
|
||||
@@ -928,6 +1048,8 @@ func suffixUniqueName(dir string, name string) string {
|
||||
|
||||
func (a *BusinessAPI) uploadSuffixFile(c *gin.Context) {
|
||||
suffixID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
// base64 上传体放宽到 128MB + 少量 JSON 包装开销(该路由已在全局 64KiB JSON 上限中豁免)。
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, suffixUploadMax+(64<<10))
|
||||
var in struct {
|
||||
FileName string `json:"file_name"`
|
||||
Size int64 `json:"size"`
|
||||
@@ -1009,7 +1131,7 @@ func (a *BusinessAPI) uploadSuffixFile(c *gin.Context) {
|
||||
}})
|
||||
}
|
||||
|
||||
// 只认模板根目录以内的路径,越界一律不删。
|
||||
// 只认模板根目录以内的路径,越界一律不删;删完后顺手回收空的后缀目录。
|
||||
func (a *BusinessAPI) removeDiskFiles(c *gin.Context, relPaths []string) {
|
||||
root, err := suffixTemplateRoot()
|
||||
if err != nil {
|
||||
@@ -1019,11 +1141,34 @@ func (a *BusinessAPI) removeDiskFiles(c *gin.Context, relPaths []string) {
|
||||
abs := filepath.Join(root, filepath.FromSlash(rel))
|
||||
clean := filepath.Clean(abs)
|
||||
if strings.HasPrefix(clean, filepath.Clean(root)+string(filepath.Separator)) {
|
||||
_ = os.Remove(clean)
|
||||
if os.Remove(clean) == nil {
|
||||
_ = os.Remove(filepath.Dir(clean))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *BusinessAPI) publishSuffixFile(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
var in struct {
|
||||
On bool `json:"on"`
|
||||
}
|
||||
if c.ShouldBindJSON(&in) != nil {
|
||||
apiError(c, 400, "invalid_request", "参数无效")
|
||||
return
|
||||
}
|
||||
on := 0
|
||||
if in.On {
|
||||
on = 1
|
||||
}
|
||||
res, err := a.db.ExecContext(c, "UPDATE suffix_files SET published=? WHERE id=?", on, id)
|
||||
if err != nil || rowsAffected(res) == 0 {
|
||||
apiError(c, 404, "not_found", "文件不存在")
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{"data": gin.H{"id": id, "published": in.On}})
|
||||
}
|
||||
|
||||
func (a *BusinessAPI) deleteSuffixFile(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
var rel string
|
||||
|
||||
Reference in New Issue
Block a user