Spaces:
Running
Running
Deploy xiaoju-cloud 1c9eefc
Browse files- internal/api/ingest_plan.go +45 -8
- internal/api/ingest_plan_test.go +21 -0
- internal/api/ingest_test.go +11 -0
internal/api/ingest_plan.go
CHANGED
|
@@ -10,6 +10,7 @@ import (
|
|
| 10 |
"regexp"
|
| 11 |
"strconv"
|
| 12 |
"strings"
|
|
|
|
| 13 |
|
| 14 |
"github.com/langhua98/xiaoju-cloud/internal/meta"
|
| 15 |
)
|
|
@@ -45,7 +46,8 @@ func (s *Server) ingestPlan(w http.ResponseWriter, r *http.Request) {
|
|
| 45 |
return
|
| 46 |
}
|
| 47 |
|
| 48 |
-
stored
|
|
|
|
| 49 |
album, err := s.svc.Store().FindFolder(r.Context(), s.userID, nil, folder)
|
| 50 |
switch {
|
| 51 |
case errors.Is(err, meta.ErrNotFound): // 还没传过任何文件
|
|
@@ -61,14 +63,16 @@ func (s *Server) ingestPlan(w http.ResponseWriter, r *http.Request) {
|
|
| 61 |
for _, f := range files {
|
| 62 |
k := planKey(f.Name, true)
|
| 63 |
stored[k] = append(stored[k], f.Size)
|
|
|
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
| 67 |
-
var todo []string
|
| 68 |
uploaded, unparsed := 0, 0
|
| 69 |
for i, name := range names {
|
| 70 |
match := false
|
| 71 |
-
|
|
|
|
| 72 |
ok, parsed := sizeMatchesAny(sizes[i], candidates)
|
| 73 |
if !parsed {
|
| 74 |
unparsed++
|
|
@@ -76,29 +80,62 @@ func (s *Server) ingestPlan(w http.ResponseWriter, r *http.Request) {
|
|
| 76 |
}
|
| 77 |
match = ok
|
| 78 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
if match {
|
| 80 |
uploaded++
|
| 81 |
} else {
|
| 82 |
todo = append(todo, strconv.Itoa(i+1))
|
|
|
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
}
|
| 85 |
s.log.Info("ingest plan", "folder", folder, "files", len(names), "uploaded", uploaded, "todo", len(todo),
|
| 86 |
-
"unparsed_sizes", unparsed, "client", clientIP(r))
|
| 87 |
writeJSON(w, http.StatusOK, map[string]any{"todo": strings.Join(todo, "\n"), "count": len(todo), "uploaded": uploaded})
|
| 88 |
}
|
| 89 |
|
| 90 |
-
// splitLines 按行拆开(快捷指令的「合并文本」用换行连接);空文本是零行。
|
|
|
|
|
|
|
| 91 |
func splitLines(s string) []string {
|
| 92 |
if s == "" {
|
| 93 |
return nil
|
| 94 |
}
|
| 95 |
-
lines := strings.Split(
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
return lines
|
| 100 |
}
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
// planKey 是比较用的名字:去掉自动加的序号、扩展名(存着的名字带扩展名,快捷指令给的不带),不分大小写。
|
| 103 |
func planKey(name string, hasExt bool) string {
|
| 104 |
name = stripCopySuffix(cleanName(name))
|
|
|
|
| 10 |
"regexp"
|
| 11 |
"strconv"
|
| 12 |
"strings"
|
| 13 |
+
"unicode"
|
| 14 |
|
| 15 |
"github.com/langhua98/xiaoju-cloud/internal/meta"
|
| 16 |
)
|
|
|
|
| 46 |
return
|
| 47 |
}
|
| 48 |
|
| 49 |
+
// stored:比较用的名字 → 大小;loose:再去掉空白、控制字符和“_”的名字 → 大小(兜底,见 looseKey)
|
| 50 |
+
stored, loose := map[string][]int64{}, map[string][]int64{}
|
| 51 |
album, err := s.svc.Store().FindFolder(r.Context(), s.userID, nil, folder)
|
| 52 |
switch {
|
| 53 |
case errors.Is(err, meta.ErrNotFound): // 还没传过任何文件
|
|
|
|
| 63 |
for _, f := range files {
|
| 64 |
k := planKey(f.Name, true)
|
| 65 |
stored[k] = append(stored[k], f.Size)
|
| 66 |
+
loose[looseKey(k)] = append(loose[looseKey(k)], f.Size)
|
| 67 |
}
|
| 68 |
}
|
| 69 |
|
| 70 |
+
var todo, firstTodo []string // firstTodo:前几个要传的文件名原样(带引号)记进日志,对不上时看得出差在哪
|
| 71 |
uploaded, unparsed := 0, 0
|
| 72 |
for i, name := range names {
|
| 73 |
match := false
|
| 74 |
+
key := planKey(name, false)
|
| 75 |
+
if candidates := stored[key]; len(candidates) > 0 {
|
| 76 |
ok, parsed := sizeMatchesAny(sizes[i], candidates)
|
| 77 |
if !parsed {
|
| 78 |
unparsed++
|
|
|
|
| 80 |
}
|
| 81 |
match = ok
|
| 82 |
}
|
| 83 |
+
if !match {
|
| 84 |
+
// 兜底:名字里有制表符这类看不见的字符时,上传时被换成了“_”,这里两边各去掉再比(大小必须对得上)
|
| 85 |
+
if candidates := loose[looseKey(key)]; len(candidates) > 0 {
|
| 86 |
+
if ok, parsed := sizeMatchesAny(sizes[i], candidates); ok && parsed {
|
| 87 |
+
match = true
|
| 88 |
+
s.log.Info("ingest plan: matched after ignoring blanks and underscores", "phone_name", strconv.Quote(name))
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
if match {
|
| 93 |
uploaded++
|
| 94 |
} else {
|
| 95 |
todo = append(todo, strconv.Itoa(i+1))
|
| 96 |
+
if len(firstTodo) < 3 {
|
| 97 |
+
firstTodo = append(firstTodo, strconv.Quote(name)+" "+strings.TrimSpace(sizes[i]))
|
| 98 |
+
}
|
| 99 |
}
|
| 100 |
}
|
| 101 |
s.log.Info("ingest plan", "folder", folder, "files", len(names), "uploaded", uploaded, "todo", len(todo),
|
| 102 |
+
"unparsed_sizes", unparsed, "first_todo", strings.Join(firstTodo, ", "), "client", clientIP(r))
|
| 103 |
writeJSON(w, http.StatusOK, map[string]any{"todo": strings.Join(todo, "\n"), "count": len(todo), "uploaded": uploaded})
|
| 104 |
}
|
| 105 |
|
| 106 |
+
// splitLines 按行拆开(快捷指令的「合并文本」用换行连接);空文本是零行。每一行都以 \r 结尾时(用的是 \r\n)
|
| 107 |
+
// 去掉 \r。别的空白不去掉:文件名里可能就有制表符这类字符(上传时被换成了“_”,比较时 planKey 也会换),
|
| 108 |
+
// 先去掉就对不上了——video_.mov 每次都被当成没传过,就是这样。
|
| 109 |
func splitLines(s string) []string {
|
| 110 |
if s == "" {
|
| 111 |
return nil
|
| 112 |
}
|
| 113 |
+
lines := strings.Split(s, "\n")
|
| 114 |
+
crlf := len(lines) > 1
|
| 115 |
+
for _, l := range lines[:len(lines)-1] {
|
| 116 |
+
if !strings.HasSuffix(l, "\r") {
|
| 117 |
+
crlf = false
|
| 118 |
+
break
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
if crlf {
|
| 122 |
+
for i := range lines {
|
| 123 |
+
lines[i] = strings.TrimSuffix(lines[i], "\r")
|
| 124 |
+
}
|
| 125 |
}
|
| 126 |
return lines
|
| 127 |
}
|
| 128 |
|
| 129 |
+
// looseKey 去掉比较用的名字里所有空白、控制字符、零宽字符和“_”(兜底比较用,大小也必须对得上)。
|
| 130 |
+
func looseKey(key string) string {
|
| 131 |
+
return strings.Map(func(c rune) rune {
|
| 132 |
+
if c == '_' || unicode.IsSpace(c) || unicode.IsControl(c) || unicode.Is(unicode.Cf, c) { // Cf:零宽字符等
|
| 133 |
+
return -1
|
| 134 |
+
}
|
| 135 |
+
return c
|
| 136 |
+
}, key)
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
// planKey 是比较用的名字:去掉自动加的序号、扩展名(存着的名字带扩展名,快捷指令给的不带),不分大小写。
|
| 140 |
func planKey(name string, hasExt bool) string {
|
| 141 |
name = stripCopySuffix(cleanName(name))
|
internal/api/ingest_plan_test.go
CHANGED
|
@@ -43,3 +43,24 @@ func TestPlanKey(t *testing.T) {
|
|
| 43 |
}
|
| 44 |
}
|
| 45 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
}
|
| 45 |
}
|
| 46 |
+
|
| 47 |
+
func TestSplitLines(t *testing.T) {
|
| 48 |
+
for in, want := range map[string][]string{
|
| 49 |
+
"": nil,
|
| 50 |
+
"a": {"a"},
|
| 51 |
+
"a\tb\nc ": {"a\tb", "c "}, // 别的空白原样保留
|
| 52 |
+
"a\r\nb\r\nc": {"a", "b", "c"}, // \r\n 分隔
|
| 53 |
+
"video\r\nb\nc": {"video\r", "b", "c"},
|
| 54 |
+
} {
|
| 55 |
+
got := splitLines(in)
|
| 56 |
+
if len(got) != len(want) {
|
| 57 |
+
t.Errorf("%q: %q, want %q", in, got, want)
|
| 58 |
+
continue
|
| 59 |
+
}
|
| 60 |
+
for i := range got {
|
| 61 |
+
if got[i] != want[i] {
|
| 62 |
+
t.Errorf("%q: %q, want %q", in, got, want)
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
}
|
internal/api/ingest_test.go
CHANGED
|
@@ -312,6 +312,17 @@ func TestIngest(t *testing.T) {
|
|
| 312 |
if code, out := plan(`{"album":"","names":"IMG_0001","sizes":"5 KB"}`); code != 200 || out["todo"] != "1" {
|
| 313 |
t.Fatalf("plan for the other album: %d %v", code, out)
|
| 314 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
if code, _ := plan(`{"album":"hidden","names":"a\nb","sizes":"1 KB"}`); code != 400 {
|
| 316 |
t.Errorf("names and sizes of different lengths must be rejected: %d", code)
|
| 317 |
}
|
|
|
|
| 312 |
if code, out := plan(`{"album":"","names":"IMG_0001","sizes":"5 KB"}`); code != 200 || out["todo"] != "1" {
|
| 313 |
t.Fatalf("plan for the other album: %d %v", code, out)
|
| 314 |
}
|
| 315 |
+
// 名字里有制表符:上传时存成“clip_.mov”;清单里原样带着制表符,或者制表符丢了,都要认出是传过的
|
| 316 |
+
clip := "\x00\x00\x00\x14ftypqt " + strings.Repeat("c", 3000)
|
| 317 |
+
if code, out := call("POST", "/api/ingest", clip, "X-File-Name", "clip\t.mov", "X-Xiaoju-Album", "hidden"); code != 201 || out["name"] != "clip_.mov" {
|
| 318 |
+
t.Fatalf("upload with a tab in the name: %d %v", code, out)
|
| 319 |
+
}
|
| 320 |
+
for _, n := range []string{"clip\t", "clip", "clip_"} {
|
| 321 |
+
body, _ := json.Marshal(map[string]string{"album": "hidden", "names": n + "\nnew one", "sizes": kb(len(clip)) + "\n3 KB"})
|
| 322 |
+
if code, out := plan(string(body)); code != 200 || out["todo"] != "2" {
|
| 323 |
+
t.Errorf("plan for %q: %d %v", n, code, out)
|
| 324 |
+
}
|
| 325 |
+
}
|
| 326 |
if code, _ := plan(`{"album":"hidden","names":"a\nb","sizes":"1 KB"}`); code != 400 {
|
| 327 |
t.Errorf("names and sizes of different lengths must be rejected: %d", code)
|
| 328 |
}
|