feat: file name staregy

This commit is contained in:
krau
2025-08-23 17:16:51 +08:00
parent 7300e54c40
commit 68e5a51300
10 changed files with 265 additions and 20 deletions

View File

@@ -0,0 +1,14 @@
package fnamest
//go:generate go-enum --values --names --noprefix --flag --nocase
// FnameST
/* ENUM(
default, message
) */
type FnameST string
var FnameSTDisplay = map[FnameST]string{
Default: "默认",
Message: "优先从消息生成",
}

View File

@@ -0,0 +1,87 @@
// Code generated by go-enum DO NOT EDIT.
// Version: 0.6.1
// Revision: a6f63bddde05aca4221df9c8e9e6d7d9674b1cb4
// Build Date: 2025-03-18T23:42:14Z
// Built By: goreleaser
package fnamest
import (
"fmt"
"strings"
)
const (
// Default is a FnameST of type default.
Default FnameST = "default"
// Message is a FnameST of type message.
Message FnameST = "message"
)
var ErrInvalidFnameST = fmt.Errorf("not a valid FnameST, try [%s]", strings.Join(_FnameSTNames, ", "))
var _FnameSTNames = []string{
string(Default),
string(Message),
}
// FnameSTNames returns a list of possible string values of FnameST.
func FnameSTNames() []string {
tmp := make([]string, len(_FnameSTNames))
copy(tmp, _FnameSTNames)
return tmp
}
// FnameSTValues returns a list of the values for FnameST
func FnameSTValues() []FnameST {
return []FnameST{
Default,
Message,
}
}
// String implements the Stringer interface.
func (x FnameST) String() string {
return string(x)
}
// IsValid provides a quick way to determine if the typed value is
// part of the allowed enumerated values
func (x FnameST) IsValid() bool {
_, err := ParseFnameST(string(x))
return err == nil
}
var _FnameSTValue = map[string]FnameST{
"default": Default,
"message": Message,
}
// ParseFnameST attempts to convert a string to a FnameST.
func ParseFnameST(name string) (FnameST, error) {
if x, ok := _FnameSTValue[name]; ok {
return x, nil
}
// Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to.
if x, ok := _FnameSTValue[strings.ToLower(name)]; ok {
return x, nil
}
return FnameST(""), fmt.Errorf("%s is %w", name, ErrInvalidFnameST)
}
// Set implements the Golang flag.Value interface func.
func (x *FnameST) Set(val string) error {
v, err := ParseFnameST(val)
*x = v
return err
}
// Get implements the Golang flag.Getter interface func.
func (x *FnameST) Get() interface{} {
return *x
}
// Type implements the github.com/spf13/pFlag Value interface.
func (x *FnameST) Type() string {
return "FnameST"
}

View File

@@ -10,6 +10,8 @@ import (
const (
TypeAdd = "add"
TypeSetDefault = "setdefault"
TypeConfig = "config"
TypeCancel = "cancel"
)
// type TaskDataTGFiles struct {

View File

@@ -2,20 +2,21 @@ package tfile
import "github.com/gotd/td/tg"
type TGFileOptions func(*tgFile)
type TGFileOption func(*tgFile)
func WithMessage(msg *tg.Message) TGFileOptions {
func WithMessage(msg *tg.Message) TGFileOption {
return func(f *tgFile) {
f.message = msg
}
}
func WithName(name string) TGFileOptions {
func WithName(name string) TGFileOption {
return func(f *tgFile) {
f.name = name
}
}
func WithNameIfEmpty(name string) TGFileOptions {
func WithNameIfEmpty(name string) TGFileOption {
return func(f *tgFile) {
if f.name == "" {
f.name = name
@@ -23,13 +24,13 @@ func WithNameIfEmpty(name string) TGFileOptions {
}
}
func WithSize(size int64) TGFileOptions {
func WithSize(size int64) TGFileOption {
return func(f *tgFile) {
f.size = size
}
}
func WithSizeIfZero(size int64) TGFileOptions {
func WithSizeIfZero(size int64) TGFileOption {
return func(f *tgFile) {
if f.size == 0 {
f.size = size

View File

@@ -54,7 +54,7 @@ func NewTGFile(
dler downloader.Client,
size int64,
name string,
opts ...TGFileOptions,
opts ...TGFileOption,
) TGFile {
f := &tgFile{
location: location,
@@ -68,7 +68,7 @@ func NewTGFile(
return f
}
func FromMedia(media tg.MessageMediaClass, client downloader.Client, opts ...TGFileOptions) (TGFile, error) {
func FromMedia(media tg.MessageMediaClass, client downloader.Client, opts ...TGFileOption) (TGFile, error) {
switch m := media.(type) {
case *tg.MessageMediaDocument:
document, ok := m.Document.AsNotEmpty()
@@ -125,7 +125,7 @@ func FromMedia(media tg.MessageMediaClass, client downloader.Client, opts ...TGF
return nil, fmt.Errorf("unsupported media type: %T", media)
}
func FromMediaMessage(media tg.MessageMediaClass, client downloader.Client, msg *tg.Message, opts ...TGFileOptions) (TGFileMessage, error) {
func FromMediaMessage(media tg.MessageMediaClass, client downloader.Client, msg *tg.Message, opts ...TGFileOption) (TGFileMessage, error) {
file, err := FromMedia(media, client, opts...)
if err != nil {
return nil, err