-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagebed.go
90 lines (77 loc) · 2.03 KB
/
imagebed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"fmt"
"imagebed/config"
"log"
"os"
"os/user"
"path"
"path/filepath"
"time"
"github.com/google/go-github/v55/github"
"golang.org/x/oauth2"
)
func getHomeDir() (dir string, err error) {
usr, err := user.Current()
if err != nil {
return
}
dir = usr.HomeDir
return
}
func main() {
var (
dir string
err error
c *config.Config
conf string
)
if dir, err = getHomeDir(); err != nil {
log.Printf("Cannot get Home Directory: %v", err)
return
}
conf = filepath.Join(dir, ".config/upload-img-github/config.toml")
if c, err = config.ReadConfig(conf); err != nil {
log.Printf("Cannot find Configuration at %v", err)
return
}
if len(os.Args) < 2 {
log.Printf("%v <path to image>", os.Args[0])
return
}
filepath := os.Args[1]
filename := path.Base(filepath)
// filename := time.Now().Format("2006-01-02T15:04:05") + "-" + strings.TrimPrefix(filepath, "/")
dirname := time.Now().Format("2006-01")
content, err := os.ReadFile(filepath)
if err != nil {
panic(err)
}
opts := &github.RepositoryContentFileOptions{
Message: github.String(fmt.Sprintf("[img] %s", filename)),
Content: []byte(content),
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.Token})
client := github.NewClient(oauth2.NewClient(ctx, ts))
_, _, _, err = client.Repositories.GetContents(ctx, c.Owner, c.Repo, dirname, &github.RepositoryContentGetOptions{})
if err != nil {
readmeOpts := &github.RepositoryContentFileOptions{
Message: github.String(fmt.Sprintf("[dir] %v", dirname)),
Content: []byte(fmt.Sprintf("# %v", dirname)),
}
_, _, err = client.Repositories.CreateFile(ctx, c.Owner, c.Repo, dirname+"/README.md", readmeOpts)
if err != nil {
log.Fatalf("%v", err)
return
}
}
// Upload the image to GitHub
_, _, err = client.Repositories.CreateFile(ctx, c.Owner, c.Repo, dirname+"/"+filename, opts)
if err != nil {
panic(err)
}
url := fmt.Sprintf("%s/%s/%s/main/%s/%s", c.BaseURL, c.Owner, c.Repo, dirname, filename)
fmt.Printf("%v", url)
}