Skip to content

Commit

Permalink
Merge pull request #13 from pluveto/upload-ext
Browse files Browse the repository at this point in the history
feat: Custom Uploader!
  • Loading branch information
pluveto authored Feb 4, 2022
2 parents ade1711 + dbfe70e commit c72de29
Show file tree
Hide file tree
Showing 13 changed files with 601 additions and 114 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ username = "username"
In follwing way:

```toml
[output-formats]
[output_formats]
"bbcode" = "[img]{url}[/img]"
"html" = '<img src="{url}" />'
"markdown-simple" = "![]({url})"
Expand Down
2 changes: 1 addition & 1 deletion docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ username = "username"
可以通过如下方式自定义输出格式:
```toml
[output-formats]
[output_formats]
"bbcode" = "[img]{url}[/img]"
"html" = '<img src="{url}" />'
"markdown-simple" = "![]({url})"
Expand Down
36 changes: 36 additions & 0 deletions extensions/smms.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"meta": {
"id": "smms",
"name": "SMMS Uploader",
"type": "simple-http-uploader",
"version": "0.0.1",
"repository": ""
},
"http": {
"request": {
"url": "https://sm.ms/api/v2/upload",
"method": "POST",
"headers": {
"Authorization": "#ext_config.token#",
"Content-Type": "multipart/form-data",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36"
},
"body": {
"format": {
"type": "string",
"value": "json"
},
"smfile": {
"type": "file",
"value": "#task.local_path#"
}
}
}
},
"upload": {
"rawUrl": {
"from": "json_response",
"path": "data.url"
}
}
}
97 changes: 25 additions & 72 deletions upload.go → github_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"bytes"
"crypto/md5"

"encoding/base64"
"fmt"
"io/ioutil"
Expand All @@ -16,62 +16,14 @@ type UploadOptions struct {
LocalPath string
}

type UploadStatus string

const (
Ignored UploadStatus = "ignored"
OK = "ok"
)

type UploadRet struct {
Status UploadStatus
TaskId int
LocalPath string
RawUrl string
Url string
Time time.Time
}

type GithubUploader struct {
Config Config
OnUploaded func(result Result[UploadRet])
Config GithubUploaderConfig
OnUploaded func(result Result[*Task])
}

const kRawUrlFmt = "https://raw.githubusercontent.com/{username}/{repo}/{branch}/{path}"
const kApiFmt = "https://api.github.com/repos/{username}/{repo}/contents/{path}"

func (u GithubUploader) Rename(path string, time time.Time) (ret string) {

base := filepath.Base(path)
ext := filepath.Ext(path)
md5HashStr := fmt.Sprintf("%x", md5.Sum([]byte(base)))
r := strings.NewReplacer(
"{year}", time.Format("2006"),
"{month}", time.Format("01"),
"{day}", time.Format("02"),
"{unixts}", fmt.Sprint(time.Unix()),
"{unixtsms}", fmt.Sprint(time.UnixMicro()),
"{ext}", ext,
"{fullname}", base+ext,
"{filename}", base,
"{filenamehash}", md5HashStr,
"{fnamehash}", md5HashStr,
"{fnamehash4}", md5HashStr[:4],
"{fnamehash8}", md5HashStr[:8],
)
ret = r.Replace(u.Config.Rename)
return
}
func (u GithubUploader) ReplaceUrl(path string) (ret string) {
var rules []string
for k, v := range u.Config.Replacements {
rules = append(rules, k, v)
}
r := strings.NewReplacer(rules...)
ret = r.Replace(path)
return
}

func (u GithubUploader) PutFile(message, path, name string) (err error) {
dat, err := ioutil.ReadFile(path)
if err != nil {
Expand Down Expand Up @@ -108,18 +60,18 @@ func (u GithubUploader) PutFile(message, path, name string) (err error) {
return nil
}

func (u GithubUploader) Upload(taskId int, localPath, targetDir string) (ret Result[UploadRet]) {
func (u GithubUploader) Upload(taskId int, localPath, targetDir string) (ret Result[*Task]) {
now := time.Now()
base := filepath.Base(localPath)

// TODO: USE reference
var targetPath string
if len(targetDir) > 0 {
targetPath = targetDir + "/" + base
} else {
targetPath = u.Rename(base, now)
targetPath = Rename(base, now)
}
rawUrl := u.buildUrl(kRawUrlFmt, targetPath)
url := u.ReplaceUrl(rawUrl)
url := ReplaceUrl(rawUrl)
GVerbose.Trace("uploading #TASK_%d %s\n", taskId, localPath)
// var err error
err := u.PutFile("upload "+base+" via upgit client", localPath, targetPath)
Expand All @@ -128,15 +80,15 @@ func (u GithubUploader) Upload(taskId int, localPath, targetDir string) (ret Res
} else {
GVerbose.Trace("failed to upload #TASK_%d %s : %s\n", taskId, localPath, err.Error())
}
ret = Result[UploadRet]{
ret = Result[*Task]{
err: err,
value: UploadRet{
Status: OK,
TaskId: taskId,
LocalPath: localPath,
RawUrl: rawUrl,
Url: url,
Time: now,
value: &Task{
Status: TASK_FINISHED,
TaskId: taskId,
LocalPath: localPath,
RawUrl: rawUrl,
Url: url,
FinishTime: now,
}}
return
}
Expand All @@ -156,17 +108,18 @@ func (u GithubUploader) buildUrl(urlfmt, path string) string {
func (u GithubUploader) UploadAll(localPaths []string, targetDir string) {
for taskId, localPath := range localPaths {

var ret Result[UploadRet]
var ret Result[*Task]
// ignore non-local path
if strings.HasPrefix(localPath, "http") {
ret = Result[UploadRet]{
value: UploadRet{
Status: Ignored,
TaskId: taskId,
LocalPath: localPath,
RawUrl: localPath,
Url: localPath,
Time: time.Now(),
ret = Result[*Task]{
value: &Task{
Ignored: true,
Status: TASK_FINISHED,
TaskId: taskId,
LocalPath: localPath,
RawUrl: localPath,
Url: localPath,
FinishTime: time.Now(),
},
}
} else {
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ require (
gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0
)

require github.com/mitchellh/mapstructure v1.4.3 // direct

require (
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // direct
golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
)
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ github.com/alexflint/go-arg v1.4.2 h1:lDWZAXxpAnZUq4qwb86p/3rIJJ2Li81EoMbTMujhVa
github.com/alexflint/go-arg v1.4.2/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM=
github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70=
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.0.0-beta.6 h1:JFNqj2afbbhCqTiyN16D7Tudc/aaDzE2FBDk+VlBQnE=
github.com/pelletier/go-toml/v2 v2.0.0-beta.6/go.mod h1:ke6xncR3W76Ba8xnVxkrZG0js6Rd2BsQEAYrfgJ6eQA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -18,8 +17,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942 h1:t0lM6y/M5IiUZyvbBTcngso8SZEZICH7is9B6g/obVU=
github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9 h1:k/gmLsJDWwWqbLCur2yWnJzwQEKRcAHXo6seXGuSwWw=
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9/go.mod h1:E1AXubJBdNmFERAOucpDIxNzeGfLzg0mYh+UfMWdChA=
golang.design/x/clipboard v0.6.0 h1:+U/e2KDBdpIjkRdxO8GwlD6dKD3Jx5zlNNzQjxte4A0=
golang.design/x/clipboard v0.6.0/go.mod h1:ep0pB+/4DGJK3ayLxweWJFHhHGGv3npJJHMXAjtLTUM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand All @@ -42,7 +39,6 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
44 changes: 44 additions & 0 deletions lib/xmap/xmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package xmap

import (
"errors"
"strings"
)

func GetDeep[T any](m map[string]interface{}, path string) (ret T, err error) {
if m == nil {
err = errors.New("map is nil")
return
}

if path == "" {
ret = interface{}(m).(T)
return
}

keys := strings.Split(path, ".")
for i := 0; i < len(keys); i++ {
key := keys[i]
if key == "" {
continue
}

if m == nil {
err = errors.New("map is nil")
return
}

if v, ok := m[key]; ok {
switch v.(type) {
case map[string]interface{}:
m = v.(map[string]interface{})
default:
return v.(T), nil
}
} else {
err = errors.New("for path " + path + ", key " + key + " not found")
return
}
}
return interface{}(m).(T), nil
}
Loading

0 comments on commit c72de29

Please sign in to comment.