-
Notifications
You must be signed in to change notification settings - Fork 1
/
savefile.go
125 lines (103 loc) · 3.31 KB
/
savefile.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package modul
import (
"crypto/rand"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/google/go-github/v56/github"
"golang.org/x/oauth2"
)
func SaveFileToGithub(usernameGhp, emailGhp, repoGhp, path string, r *http.Request) (string, error) {
file, handler, err := r.FormFile("file")
if err != nil {
return "", fmt.Errorf("error 1: %s", err)
}
defer file.Close()
// Generate a random filename
randomFileName, err := generateRandomFileName(handler.Filename)
if err != nil {
return "", fmt.Errorf("error 2: %s", err)
}
// Read the content of the file into a byte slice
fileContent, err := io.ReadAll(file)
if err != nil {
return "", fmt.Errorf("error 5: %s", err)
}
access_token := os.Getenv("GITHUB_ACCESS_TOKEN")
if access_token == "" {
return "", fmt.Errorf("error access token: %s", err)
}
// Initialize GitHub client
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: access_token},
)
tc := oauth2.NewClient(r.Context(), ts)
client := github.NewClient(tc)
// Create a new repository file
_, _, err = client.Repositories.CreateFile(r.Context(), usernameGhp, repoGhp, path+"/"+randomFileName, &github.RepositoryContentFileOptions{
Message: github.String("Add new file"),
Content: fileContent,
Committer: &github.CommitAuthor{Name: github.String(usernameGhp), Email: github.String(emailGhp)},
})
if err != nil {
return "", fmt.Errorf("error 6: %s", err)
}
fileUrl := "https://" + usernameGhp + ".github.io/" + repoGhp + "/" + path + "/" + randomFileName
return fileUrl, nil
}
func generateRandomFileName(originalFilename string) (string, error) {
randomBytes := make([]byte, 16)
_, err := rand.Read(randomBytes)
if err != nil {
return "", err
}
randomFileName := fmt.Sprintf("%x%s", randomBytes, filepath.Ext(originalFilename))
return randomFileName, nil
}
// func SaveFileToGridFS(db *mongo.Database, col string, r *http.Request) (model.GridFSFile, error) {
// file, handler, err := r.FormFile("file")
// if err != nil {
// return model.GridFSFile{}, fmt.Errorf("error 1: %s", err)
// }
// defer file.Close()
// data, err := io.ReadAll(file)
// if err != nil {
// return model.GridFSFile{}, fmt.Errorf("error 2: %s", err)
// }
// fileSize := len(data)
// // Generate a random filename
// randomFileName, err := generateRandomFileName(handler.Filename)
// if err != nil {
// return model.GridFSFile{}, fmt.Errorf("error 3: %s", err)
// }
// // Create a new file in GridFS
// bucket, err := gridfs.NewBucket(
// db,
// options.GridFSBucket().SetName(col),
// )
// if err != nil {
// return model.GridFSFile{}, fmt.Errorf("error 4: %s", err)
// }
// uploadStream, err := bucket.OpenUploadStream(randomFileName)
// if err != nil {
// return model.GridFSFile{}, fmt.Errorf("error 5: %s", err)
// }
// defer uploadStream.Close()
// _, err = uploadStream.Write(data)
// if err != nil {
// return model.GridFSFile{}, fmt.Errorf("error 6: %s", err)
// }
// // Content Type based on file extension (modify as needed)
// contentType := mime.TypeByExtension(filepath.Ext(handler.Filename))
// if contentType == "" {
// contentType = "application/octet-stream"
// }
// return model.GridFSFile{
// ID: primitive.NewObjectID(),
// FileName: randomFileName,
// FileSize: int64(fileSize),
// ContentType: contentType,
// }, nil
// }