Skip to content

Commit

Permalink
feat(dl): add filenamify to tplfunc
Browse files Browse the repository at this point in the history
related #676, #704, #733, #804
  • Loading branch information
iyear committed Nov 17, 2024
1 parent bdd84cc commit 5a690c5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
10 changes: 1 addition & 9 deletions cmd/dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"context"
"fmt"
"strings"

"github.com/gotd/td/telegram"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -48,14 +47,7 @@ func NewDownload() *cobra.Command {
cmd.Flags().StringSliceVarP(&opts.URLs, "url", "u", []string{}, "telegram message links")
cmd.Flags().StringSliceVarP(&opts.Files, file, "f", []string{}, "official client exported files")

// generate default replacer
builder := strings.Builder{}
chars := []string{`/`, `\`, `:`, `*`, `?`, `<`, `>`, `|`, ` `}
for _, c := range chars {
builder.WriteString(fmt.Sprintf("`%s` `_` ", c))
}
t := fmt.Sprintf(`{{ .DialogID }}_{{ .MessageID }}_{{ replace .FileName %s }}`, builder.String())
cmd.Flags().String(consts.FlagDlTemplate, t, "download file name template")
cmd.Flags().String(consts.FlagDlTemplate, `{{ .DialogID }}_{{ .MessageID }}_{{ filenamify .FileName }}`, "download file name template")

cmd.Flags().StringSliceVarP(&opts.Include, include, "i", []string{}, "include the specified file extensions, and only judge by file name, not file MIME. Example: -i mp4,mp3")
cmd.Flags().StringSliceVarP(&opts.Exclude, exclude, "e", []string{}, "exclude the specified file extensions, and only judge by file name, not file MIME. Example: -e png,jpg")
Expand Down
27 changes: 14 additions & 13 deletions docs/content/en/guide/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@ Template syntax is based on [Go's text/template](https://golang.org/pkg/text/tem

### Functions (beta)

| Func | Desc | Usage | Example |
|:------------:|:------------------------------------------------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------:|:-------------------------------------------------------------------------------------:|
| `repeat` | Repeat `STRING` `N` times | `repeat STRING N` | `{{ repeat "test" 3 }}` |
| `replace` | Perform replacement on `STRING` with `PAIRS` | `replace STRING PAIRS...` | `{{ replace "Test" "t" "T" "e" "E" }}` |
| `upper` | Convert `STRING` to uppercase | `upper STRING` | `{{ upper "Test" }}` |
| `lower` | Convert `STRING` to lowercase | `lower STRING` | `{{ lower "Test" }}` |
| `snakecase` | Convert `STRING` to snake_case | `snakecase STRING` | `{{ snakecase "Test" }}` |
| `camelcase` | Convert `STRING` to camelCase | `camelcase STRING` | `{{ camelcase "Test" }}` |
| `kebabcase` | Convert `STRING` to kebab-case | `kebabcase STRING` | `{{ kebabcase "Test" }}` |
| `rand` | Generate random number in range `MIN` to `MAX` | `rand MIN MAX` | `{{ rand 1 10 }}` |
| `now` | Get current timestamp | `now` | `{{ now }}` |
| Func | Desc | Usage | Example |
|:------------:|:------------------------------------------------------------------------------------------------------------------------:|:------------------------------------------------------------:|:-------------------------------------------------------------------------------------:|
| `repeat` | Repeat `STRING` `N` times | `repeat STRING N` | `{{ repeat "test" 3 }}` |
| `replace` | Perform replacement on `STRING` with `PAIRS` | `replace STRING PAIRS...` | `{{ replace "Test" "t" "T" "e" "E" }}` |
| `upper` | Convert `STRING` to uppercase | `upper STRING` | `{{ upper "Test" }}` |
| `lower` | Convert `STRING` to lowercase | `lower STRING` | `{{ lower "Test" }}` |
| `snakecase` | Convert `STRING` to snake_case | `snakecase STRING` | `{{ snakecase "Test" }}` |
| `camelcase` | Convert `STRING` to camelCase | `camelcase STRING` | `{{ camelcase "Test" }}` |
| `kebabcase` | Convert `STRING` to kebab-case | `kebabcase STRING` | `{{ kebabcase "Test" }}` |
| `rand` | Generate random number in range `MIN` to `MAX` | `rand MIN MAX` | `{{ rand 1 10 }}` |
| `now` | Get current timestamp | `now` | `{{ now }}` |
| `formatDate` | Format `TIMESTAMP` with [format](https://golang.cafe/blog/golang-time-format-example.html)<br/>Default: `20060102150405` | `formatDate TIMESTAMP` <br/> `formatDate TIMESTAMP "format"` | `{{ formatDate 1600000000 }}`<br/> `{{ formatDate 1600000000 "2006-01-02-15-04-05"}}` |
| `filenamify` | Convert `STRING` to a valid filename with the best effort. | `filenamify STRING` | `{{ filenamify .FileName }}` |

### Examples:

```gotemplate
{{ .DialogID }}_{{ .MessageID }}_{{ replace .FileName `/` `_` `\` `_` `:` `_` `*` `_` `?` `_` `<` `_` `>` `_` `|` `_` ` ` `_` }}
{{ .DialogID }}_{{ .MessageID }}_{{ replace .FileCaption `/` `_` `\` `_` `:` `_` }}
{{ .FileName }}_{{ formatDate .DownloadDate }}_{{ .FileSize }}
Expand All @@ -56,5 +57,5 @@ Template syntax is based on [Go's text/template](https://golang.org/pkg/text/tem
### Default:

```gotemplate
{{ .DialogID }}_{{ .MessageID }}_{{ replace .FileName `/` `_` `\` `_` `:` `_` `*` `_` `?` `_` `<` `_` `>` `_` `|` `_` ` ` `_` }}
{{ .DialogID }}_{{ .MessageID }}_{{ filenamify .FileName }}
```
5 changes: 3 additions & 2 deletions docs/content/zh/guide/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ bookToC: false
| `rand` | 在范围 `MIN``MAX` 生成随机数 | `rand MIN MAX` | `{{ rand 1 10 }}` |
| `now` | 获取当前时间戳 | `now` | `{{ now }}` |
| `formatDate` | [格式化](https://zhuanlan.zhihu.com/p/145009400) `TIMESTAMP` 时间戳<br/>(默认格式: `20060102150405`) | `formatDate TIMESTAMP` <br/> `formatDate TIMESTAMP "format"` | `{{ formatDate 1600000000 }}`<br/> `{{ formatDate 1600000000 "2006-01-02-15-04-05"}}` |
| `filenamify` | 尽可能将 `STRING` 转换为合法文件名 | `filenamify STRING` | `{{ filenamify .FileName }}` |

### 示例:

```gotemplate
{{ .DialogID }}_{{ .MessageID }}_{{ replace .FileName `/` `_` `\` `_` `:` `_` `*` `_` `?` `_` `<` `_` `>` `_` `|` `_` ` ` `_` }}
{{ .DialogID }}_{{ .MessageID }}_{{ replace .FileCaption `/` `_` `\` `_` `:` `_` }}
{{ .FileName }}_{{ formatDate .DownloadDate }}_{{ .FileSize }}
Expand All @@ -56,5 +57,5 @@ bookToC: false
### 默认:

```gotemplate
{{ .DialogID }}_{{ .MessageID }}_{{ replace .FileName `/` `_` `\` `_` `:` `_` `*` `_` `?` `_` `<` `_` `>` `_` `|` `_` ` ` `_` }}
{{ .DialogID }}_{{ .MessageID }}_{{ filenamify .FileName }}
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/beevik/ntp v1.4.3
github.com/expr-lang/expr v1.16.9
github.com/fatih/color v1.18.0
github.com/flytam/filenamify v1.2.0
github.com/gabriel-vasile/mimetype v1.4.6
github.com/go-faster/errors v0.7.1
github.com/go-faster/jx v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/expr-lang/expr v1.16.9/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/flytam/filenamify v1.2.0 h1:7RiSqXYR4cJftDQ5NuvljKMfd/ubKnW/j9C6iekChgI=
github.com/flytam/filenamify v1.2.0/go.mod h1:Dzf9kVycwcsBlr2ATg6uxjqiFgKGH+5SKFuhdeP5zu8=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
Expand Down
14 changes: 14 additions & 0 deletions pkg/tplfunc/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"strings"
"text/template"

"github.com/flytam/filenamify"
"github.com/iancoleman/strcase"
)

var String = []Func{
Repeat(), Replace(),
ToUpper(), ToLower(),
SnakeCase(), CamelCase(), KebabCase(),
Filenamify(),
}

func Repeat() Func {
Expand Down Expand Up @@ -64,3 +66,15 @@ func KebabCase() Func {
}
}
}

func Filenamify() Func {
return func(funcMap template.FuncMap) {
funcMap["filenamify"] = func(s string) string {
res, err := filenamify.FilenamifyV2(s)
if err != nil || res == "" {
return "invalid-filename"
}
return res
}
}
}

0 comments on commit 5a690c5

Please sign in to comment.