mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-09-08 00:56:38 +08:00
feat: add NormalizePathname function and update task handling for parsed items
This commit is contained in:
@@ -3,6 +3,7 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/celestix/gotgproto/dispatcher"
|
"github.com/celestix/gotgproto/dispatcher"
|
||||||
@@ -11,6 +12,7 @@ import (
|
|||||||
"github.com/gotd/td/tg"
|
"github.com/gotd/td/tg"
|
||||||
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
|
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
|
||||||
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/shortcut"
|
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/shortcut"
|
||||||
|
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
|
||||||
"github.com/krau/SaveAny-Bot/database"
|
"github.com/krau/SaveAny-Bot/database"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
|
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
|
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
|
||||||
@@ -74,6 +76,9 @@ func handleAddCallback(ctx *ext.Context, update *ext.Update) error {
|
|||||||
case tasktype.TaskTypeTphpics:
|
case tasktype.TaskTypeTphpics:
|
||||||
return shortcut.CreateAndAddtelegraphWithEdit(ctx, userID, data.TphPageNode, data.TphDirPath, data.TphPics, selectedStorage, msgID)
|
return shortcut.CreateAndAddtelegraphWithEdit(ctx, userID, data.TphPageNode, data.TphDirPath, data.TphPics, selectedStorage, msgID)
|
||||||
case tasktype.TaskTypeParseditem:
|
case tasktype.TaskTypeParseditem:
|
||||||
|
if len(data.ParsedItem.Resources) > 1 {
|
||||||
|
dirPath = path.Join(dirPath, fsutil.NormalizePathname(data.ParsedItem.Title))
|
||||||
|
}
|
||||||
shortcut.CreateAndAddParsedTaskWithEdit(ctx, selectedStorage, dirPath, data.ParsedItem, msgID, userID)
|
shortcut.CreateAndAddParsedTaskWithEdit(ctx, selectedStorage, dirPath, data.ParsedItem, msgID, userID)
|
||||||
default:
|
default:
|
||||||
log.FromContext(ctx).Errorf("Unsupported task type: %s", data.TaskType)
|
log.FromContext(ctx).Errorf("Unsupported task type: %s", data.TaskType)
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/gotd/td/tg"
|
"github.com/gotd/td/tg"
|
||||||
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
|
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
|
||||||
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/shortcut"
|
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/shortcut"
|
||||||
|
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
|
||||||
"github.com/krau/SaveAny-Bot/parsers"
|
"github.com/krau/SaveAny-Bot/parsers"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
|
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
|
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
|
||||||
@@ -106,5 +107,9 @@ func handleSilentSaveText(ctx *ext.Context, u *ext.Update) error {
|
|||||||
logger.Errorf("Failed to send message: %s", err)
|
logger.Errorf("Failed to send message: %s", err)
|
||||||
return dispatcher.EndGroups
|
return dispatcher.EndGroups
|
||||||
}
|
}
|
||||||
return shortcut.CreateAndAddParsedTaskWithEdit(ctx, stor, "", item, msg.ID, userID)
|
dirPath := ""
|
||||||
|
if len(item.Resources) > 1 {
|
||||||
|
dirPath = fsutil.NormalizePathname(item.Title)
|
||||||
|
}
|
||||||
|
return shortcut.CreateAndAddParsedTaskWithEdit(ctx, stor, dirPath, item, msg.ID, userID)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package fsutil
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/gabriel-vasile/mimetype"
|
"github.com/gabriel-vasile/mimetype"
|
||||||
)
|
)
|
||||||
@@ -55,3 +57,21 @@ func CreateFile(fp string) (*File, error) {
|
|||||||
}
|
}
|
||||||
return &File{File: file}, nil
|
return &File{File: file}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NormalizePathname(s string) string {
|
||||||
|
specials := `\/:*?"<>|` + "\n\r\t"
|
||||||
|
var builder strings.Builder
|
||||||
|
for _, ch := range s {
|
||||||
|
if strings.ContainsRune(specials, ch) || unicode.IsControl(ch) {
|
||||||
|
builder.WriteRune('_')
|
||||||
|
} else {
|
||||||
|
builder.WriteRune(ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result := strings.TrimRightFunc(builder.String(), func(r rune) bool {
|
||||||
|
return r == '.' || r == '_' || unicode.IsSpace(r)
|
||||||
|
})
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package fsutil_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNormalizePathname(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: "hello/world?.txt ",
|
||||||
|
expected: "hello_world_.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "bad|name:\nfile\r.",
|
||||||
|
expected: "bad_name__file",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "normal.txt",
|
||||||
|
expected: "normal.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "test.... ",
|
||||||
|
expected: "test",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "abc<>def",
|
||||||
|
expected: "abc__def",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "with\tcontrol",
|
||||||
|
expected: "with_control",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
got := fsutil.NormalizePathname(tc.input)
|
||||||
|
if got != tc.expected {
|
||||||
|
t.Errorf("NormalizePathname(%q) = %q; want %q", tc.input, got, tc.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/celestix/gotgproto/ext"
|
"github.com/celestix/gotgproto/ext"
|
||||||
"github.com/duke-git/lancet/v2/maputil"
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
@@ -61,16 +62,12 @@ func GenFileNameFromMessage(message tg.Message) string {
|
|||||||
return fmt.Sprintf("%s_%s", tagStr, strconv.Itoa(message.GetID()))
|
return fmt.Sprintf("%s_%s", tagStr, strconv.Itoa(message.GetID()))
|
||||||
}
|
}
|
||||||
text = lcstrutil.Substring(strings.Map(func(r rune) rune {
|
text = lcstrutil.Substring(strings.Map(func(r rune) rune {
|
||||||
if r < 0x20 || r == 0x7F {
|
|
||||||
return '_'
|
|
||||||
}
|
|
||||||
switch r {
|
switch r {
|
||||||
// invalid characters
|
|
||||||
case '/', '\\',
|
case '/', '\\',
|
||||||
':', '*', '?', '"', '<', '>', '|':
|
':', '*', '?', '"', '<', '>', '|':
|
||||||
return '_'
|
return '_'
|
||||||
// empty
|
}
|
||||||
case ' ', '\t', '\r', '\n':
|
if unicode.IsControl(r) || unicode.IsSpace(r) {
|
||||||
return '_'
|
return '_'
|
||||||
}
|
}
|
||||||
if validator.IsPrintable(string(r)) {
|
if validator.IsPrintable(string(r)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user