-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
199 lines (175 loc) · 5.55 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bufio"
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"strings"
"net/http"
"github.com/gin-gonic/gin"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
_ "client_app/docs" // docs is generated by Swag CLI, you have to import it.
)
// gin-swagger middleware
// swagger embed files
func fileToBase64(filename string) (string, int64) {
// Open file on disk.
f, _ := os.Open(filename)
stat, _ := f.Stat()
// Read entire JPG into byte slice.
reader := bufio.NewReader(f)
content, _ := ioutil.ReadAll(reader)
// Encode as base64.
encoded := base64.StdEncoding.EncodeToString(content)
// Print encoded data to console.
// ... The base64 image can be used as a data URI in a browser.
return encoded, stat.Size()
}
func getMinio() (*minio.Client, error) {
endpoint := "127.0.0.1:9000"
accessKeyID := "EafiiIyEVsF2vbn4"
secretAccessKey := "HB3hT1ACjFaWZvzc8sGj70JCWqQLhLkm"
useSSL := false
// Initialize minio client object.
return minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
}
type UploadFileInput struct {
Filename string
Filesize int64
Base64Content string
ContentType string
BucketName string
}
type UploadFileResult struct {
Etag string
Error string
HumanError string
}
// UploadFile godoc
// @Summary get document
// @Description get document by bucket and filename
// @Tags minio
// @Accept json
// @Produce json
//
// @Param filedata body UploadFileInput true "upload file"
//
// @Success 200 {object} UploadFileResult
// @Failure 422 {object} UploadFileResult
// @Failure 500 {object} UploadFileResult
// @Router /minio/file [post]
func UploadFile(c *gin.Context) {
data := UploadFileInput{}
err := c.ShouldBind(&data)
if err != nil {
c.JSON(422, UploadFileResult{Etag: "", Error: err.Error(), HumanError: "Не верные входные данные"})
return
}
client, err := getMinio()
if err != nil {
c.JSON(500, UploadFileResult{Etag: "", Error: err.Error(), HumanError: "Хранилище s3 недоступно"})
return
}
reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(data.Base64Content))
info, err := client.PutObject(context.Background(), data.BucketName, data.Filename, reader, data.Filesize, minio.PutObjectOptions{ContentType: data.ContentType})
c.JSON(200, UploadFileResult{Etag: info.ETag})
}
type DownloadFileInput struct {
Filename string `form:"Filename"`
BucketName string `form:"BucketName"`
}
type DownloadFileResult struct {
Error string
HumanError string
ContentType string
FileSize int64
Base64Content string
}
// DownloadFile godoc
// @Summary get document
// @Description get document by bucket and filename
// @Tags minio
// @Accept json
// @Produce json
// @Param Filename query string true "Filename"
// @Param BucketName query string true "BucketName"
// @Success 200 {object} DownloadFileResult
// @Failure 422 {object} DownloadFileResult
// @Failure 500 {object} DownloadFileResult
// @Router /minio/file [get]
func DownloadFile(c *gin.Context) {
var data DownloadFileInput
err := c.ShouldBind(&data)
if err != nil {
c.JSON(422, DownloadFileResult{Error: err.Error(), HumanError: "Неверные входные данные"})
return
}
fmt.Printf("%+v\n", data)
fmt.Printf("%+v\n", c.Request.URL.Query())
client, err := getMinio()
if err != nil {
c.JSON(500, DownloadFileResult{Error: err.Error(), HumanError: "Хранилище s3 недоступно"})
return
}
obj, err := client.GetObject(context.Background(), data.BucketName, data.Filename, minio.GetObjectOptions{})
if err != nil {
c.JSON(500, DownloadFileResult{Error: err.Error(), HumanError: "Ошибка скачки файла с хранилища"})
return
}
objStat, err := obj.Stat()
if err != nil {
c.JSON(500, DownloadFileResult{Error: err.Error(), HumanError: "Ошибка получения данных о файле"})
return
}
objReader := bufio.NewReader(obj)
objContent, err := ioutil.ReadAll(objReader)
if err != nil {
c.JSON(500, DownloadFileResult{Error: err.Error(), HumanError: "Ошибка чтения тела файла"})
return
}
c.JSON(200, DownloadFileResult{ContentType: objStat.ContentType, FileSize: objStat.Size,
Base64Content: base64.StdEncoding.EncodeToString(objContent)})
}
func Ping(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
}
// @title Swagger Example API
// @version 1.0
// @description This is a sample server celler server.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /api/v1
// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api/
func main() {
r := gin.Default()
v1 := r.Group("/api/v1")
{
pingPong := v1.Group("ping-pong")
{
pingPong.GET("", Ping)
}
minioGroup := v1.Group("minio")
{
minioGroup.GET("file", DownloadFile)
minioGroup.POST("file", UploadFile)
}
}
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}