/
upload.go
179 lines (151 loc) · 3.95 KB
/
upload.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
/*
* Revision History:
* Initial: 2020/1016 Abserari
*/
package controller
import (
"database/sql"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"path"
"strings"
mysql "github.com/abserari/pet/upload/model/mysql"
md "github.com/abserari/pet/utils/file"
"github.com/gin-gonic/gin"
)
var (
errRequest = errors.New("Request is not post method")
erruserID = errors.New("userID invalid")
)
const (
// InvalidUID - userID invalid
InvalidUID = 0
// FileKey - key of the file
FileKey = "file"
// FileUploadDir - the root directory of the upload files
FileUploadDir = "files"
// PictureDir - save pictures file
PictureDir = "picture"
// VideoDir - save videos file
VideoDir = "video"
// OtherDir - files other than video and picture
OtherDir = "other"
)
// UploadController -
type UploadController struct {
db *sql.DB
BaseURL string
getUID func(c *gin.Context) (uint64, error)
}
// New -
func New(db *sql.DB, baseURL string, getUID func(c *gin.Context) (uint64, error)) *UploadController {
return &UploadController{
db: db,
BaseURL: "http://" + baseURL + "/",
getUID: getUID,
}
}
// RegisterRouter -
func (u *UploadController) RegisterRouter(r gin.IRouter) {
if r == nil {
log.Fatal("[InitRouter]: server is nil")
}
err := mysql.CreateTable(u.db)
if err != nil {
log.Fatal(err)
}
err = md.CheckDir(PictureDir, VideoDir, OtherDir)
if err != nil {
log.Fatal(err)
}
r.POST("/upload", u.upload)
r.POST("/delete", u.deleteByID)
}
func (u *UploadController) upload(c *gin.Context) {
if c.Request.Method != "POST" {
c.Error(errRequest)
c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
userID, err := u.getUID(c)
if err != nil {
c.Error(err)
c.JSON(http.StatusUnauthorized, gin.H{"status": http.StatusUnauthorized})
return
}
if userID == InvalidUID {
c.Error(erruserID)
c.JSON(http.StatusForbidden, gin.H{"status": http.StatusForbidden})
return
}
file, header, err := c.Request.FormFile(FileKey)
defer func() {
file.Close()
c.Request.MultipartForm.RemoveAll()
}()
if err != nil {
c.Error(err)
c.JSON(http.StatusNotFound, gin.H{"status": http.StatusNotFound})
return
}
newfile, _ := ioutil.ReadAll(file)
MD5Str, err := md.MD5(newfile)
if err != nil {
c.Error(err)
c.JSON(http.StatusMethodNotAllowed, gin.H{"status": http.StatusMethodNotAllowed})
return
}
filePath, err := mysql.QueryByMD5(u.db, MD5Str)
// if the file exists, return it now.
if err == nil {
fmt.Println("The file already exists:", filePath)
c.JSON(http.StatusNotAcceptable, gin.H{"status": http.StatusNotAcceptable, "URL": u.BaseURL + filePath})
return
}
// check the error if is our expected - NoRows.
if err != mysql.ErrNoRows {
c.Error(err)
c.JSON(http.StatusConflict, gin.H{"status": http.StatusConflict})
return
}
fileSuffix := path.Ext(header.Filename)
filePath = FileUploadDir + "/" + md.ClassifyBySuffix(fileSuffix) + "/" + MD5Str + fileSuffix
err = md.CopyFile(filePath, newfile)
if err != nil {
c.Error(err)
c.JSON(http.StatusPreconditionFailed, gin.H{"status": http.StatusPreconditionFailed})
return
}
err = mysql.Insert(u.db, userID, filePath, MD5Str)
if err != nil {
c.Error(err)
c.JSON(http.StatusUnsupportedMediaType, gin.H{"status": http.StatusUnsupportedMediaType})
return
}
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "URL": u.BaseURL + filePath})
}
func (con *UploadController) deleteByID(c *gin.Context) {
var (
req struct {
Path string `json:"path" binding:"required"`
}
)
err := c.ShouldBind(&req)
if err != nil {
c.Error(err)
c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
req.Path = req.Path[strings.Index(req.Path, "files/"):]
log.Println(req.Path, con.BaseURL)
err = mysql.DeleteByPath(con.db, req.Path)
if err != nil {
c.Error(err)
c.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK})
}