chore: sync with gin-template
This commit is contained in:
@@ -1,131 +0,0 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"message-pusher/common"
|
||||
"message-pusher/model"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type FileDeleteRequest struct {
|
||||
Id int
|
||||
Link string
|
||||
//Token string
|
||||
}
|
||||
|
||||
func UploadFile(c *gin.Context) {
|
||||
uploadPath := common.UploadPath
|
||||
//saveToDatabase := true
|
||||
//path := c.PostForm("path")
|
||||
//if path != "" { // Upload to explorer's path
|
||||
// uploadPath = filepath.Join(common.ExplorerRootPath, path)
|
||||
// if !strings.HasPrefix(uploadPath, common.ExplorerRootPath) {
|
||||
// // In this case the given path is not valid, so we reset it to ExplorerRootPath.
|
||||
// uploadPath = common.ExplorerRootPath
|
||||
// }
|
||||
// saveToDatabase = false
|
||||
//}
|
||||
|
||||
description := c.PostForm("description")
|
||||
if description == "" {
|
||||
description = "无描述信息"
|
||||
}
|
||||
uploader := c.GetString("username")
|
||||
if uploader == "" {
|
||||
uploader = "匿名用户"
|
||||
}
|
||||
currentTime := time.Now().Format("2006-01-02 15:04:05")
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
files := form.File["file"]
|
||||
for _, file := range files {
|
||||
// In case someone wants to upload to other folders.
|
||||
filename := filepath.Base(file.Filename)
|
||||
link := filename
|
||||
savePath := filepath.Join(uploadPath, filename)
|
||||
if _, err := os.Stat(savePath); err == nil {
|
||||
// File already existed.
|
||||
t := time.Now()
|
||||
timestamp := t.Format("_2006-01-02_15-04-05")
|
||||
ext := filepath.Ext(filename)
|
||||
if ext == "" {
|
||||
link += timestamp
|
||||
} else {
|
||||
link = filename[:len(filename)-len(ext)] + timestamp + ext
|
||||
}
|
||||
savePath = filepath.Join(uploadPath, link)
|
||||
}
|
||||
if err := c.SaveUploadedFile(file, savePath); err != nil {
|
||||
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
// save to database
|
||||
fileObj := &model.File{
|
||||
Description: description,
|
||||
Uploader: uploader,
|
||||
Time: currentTime,
|
||||
Link: link,
|
||||
Filename: filename,
|
||||
}
|
||||
err = fileObj.Insert()
|
||||
if err != nil {
|
||||
_ = fmt.Errorf(err.Error())
|
||||
}
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, "./")
|
||||
}
|
||||
|
||||
func DeleteFile(c *gin.Context) {
|
||||
var deleteRequest FileDeleteRequest
|
||||
err := json.NewDecoder(c.Request.Body).Decode(&deleteRequest)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"success": false,
|
||||
"message": "无效的参数",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
fileObj := &model.File{
|
||||
Id: deleteRequest.Id,
|
||||
}
|
||||
model.DB.Where("id = ?", deleteRequest.Id).First(&fileObj)
|
||||
err = fileObj.Delete()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": err.Error(),
|
||||
})
|
||||
} else {
|
||||
message := "文件删除成功"
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": message,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func DownloadFile(c *gin.Context) {
|
||||
path := c.Param("file")
|
||||
fullPath := filepath.Join(common.UploadPath, path)
|
||||
if !strings.HasPrefix(fullPath, common.UploadPath) {
|
||||
// We may being attacked!
|
||||
c.Status(403)
|
||||
return
|
||||
}
|
||||
c.File(fullPath)
|
||||
// Update download counter
|
||||
go func() {
|
||||
model.UpdateDownloadCounter(path)
|
||||
}()
|
||||
}
|
||||
+15
-7
@@ -107,16 +107,24 @@ func GitHubOAuth(c *gin.Context) {
|
||||
if model.IsGitHubIdAlreadyTaken(user.GitHubId) {
|
||||
user.FillUserByGitHubId()
|
||||
} else {
|
||||
user.Username = "github_" + strconv.Itoa(model.GetMaxUserId()+1)
|
||||
user.DisplayName = githubUser.Name
|
||||
user.Email = githubUser.Email
|
||||
user.Role = common.RoleCommonUser
|
||||
user.Status = common.UserStatusEnabled
|
||||
if common.RegisterEnabled {
|
||||
user.Username = "github_" + strconv.Itoa(model.GetMaxUserId()+1)
|
||||
user.DisplayName = githubUser.Name
|
||||
user.Email = githubUser.Email
|
||||
user.Role = common.RoleCommonUser
|
||||
user.Status = common.UserStatusEnabled
|
||||
|
||||
if err := user.Insert(); err != nil {
|
||||
if err := user.Insert(); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
"message": err.Error(),
|
||||
"message": "管理员关闭了新用户注册",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ func GetStatus(c *gin.Context) {
|
||||
"wechat_qrcode": common.WeChatAccountQRCodeImageURL,
|
||||
"wechat_login": common.WeChatAuthEnabled,
|
||||
"server_address": common.ServerAddress,
|
||||
"turnstile_check": common.TurnstileCheckEnabled,
|
||||
"turnstile_site_key": common.TurnstileSiteKey,
|
||||
},
|
||||
})
|
||||
return
|
||||
|
||||
@@ -52,6 +52,12 @@ func UpdateOption(c *gin.Context) {
|
||||
"message": "无法启用微信登录,请先填入微信登录相关配置信息!",
|
||||
})
|
||||
return
|
||||
} else if option.Key == "TurnstileCheckEnabled" && option.Value == "true" && common.TurnstileSiteKey == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
"message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
|
||||
})
|
||||
return
|
||||
}
|
||||
err = model.UpdateOption(option.Key, option.Value)
|
||||
if err != nil {
|
||||
|
||||
+27
-13
@@ -100,13 +100,20 @@ func Logout(c *gin.Context) {
|
||||
}
|
||||
|
||||
func Register(c *gin.Context) {
|
||||
if !common.PasswordRegisterEnabled {
|
||||
if !common.RegisterEnabled {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "管理员关闭了新用户注册",
|
||||
"success": false,
|
||||
})
|
||||
return
|
||||
}
|
||||
if !common.PasswordRegisterEnabled {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "管理员关闭了通过密码进行注册,请使用第三方账户验证的形式进行注册",
|
||||
"success": false,
|
||||
})
|
||||
return
|
||||
}
|
||||
var user model.User
|
||||
err := json.NewDecoder(c.Request.Body).Decode(&user)
|
||||
if err != nil {
|
||||
@@ -134,7 +141,7 @@ func Register(c *gin.Context) {
|
||||
if !common.VerifyCodeWithKey(user.Email, user.VerificationCode, common.EmailVerificationPurpose) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
"message": "验证码错误!",
|
||||
"message": "验证码错误或已过期",
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -154,7 +161,6 @@ func Register(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "",
|
||||
@@ -163,7 +169,11 @@ func Register(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetAllUsers(c *gin.Context) {
|
||||
users, err := model.GetAllUsers()
|
||||
p, _ := strconv.Atoi(c.Query("p"))
|
||||
if p < 0 {
|
||||
p = 0
|
||||
}
|
||||
users, err := model.GetAllUsers(p*common.ItemsPerPage, common.ItemsPerPage)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
@@ -372,10 +382,6 @@ func UpdateSelf(c *gin.Context) {
|
||||
Username: user.Username,
|
||||
Password: user.Password,
|
||||
DisplayName: user.DisplayName,
|
||||
Token: user.Token,
|
||||
}
|
||||
if cleanUser.Token == "" {
|
||||
cleanUser.Token = " " // this is because gorm will ignore zero value
|
||||
}
|
||||
if user.Password == "$I_LOVE_U" {
|
||||
user.Password = "" // rollback to what it should be
|
||||
@@ -449,7 +455,6 @@ func DeleteSelf(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// CreateUser Only admin user can call this, so we can trust it
|
||||
func CreateUser(c *gin.Context) {
|
||||
var user model.User
|
||||
err := json.NewDecoder(c.Request.Body).Decode(&user)
|
||||
@@ -471,8 +476,13 @@ func CreateUser(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err := user.Insert(); err != nil {
|
||||
// Even for admin users, we cannot fully trust them!
|
||||
cleanUser := model.User{
|
||||
Username: user.Username,
|
||||
Password: user.Password,
|
||||
DisplayName: user.DisplayName,
|
||||
}
|
||||
if err := cleanUser.Insert(); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
"message": err.Error(),
|
||||
@@ -557,10 +567,14 @@ func ManageUser(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
clearUser := model.User{
|
||||
Role: user.Role,
|
||||
Status: user.Status,
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "",
|
||||
"data": clearUser,
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -571,7 +585,7 @@ func EmailBind(c *gin.Context) {
|
||||
if !common.VerifyCodeWithKey(email, code, common.EmailVerificationPurpose) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
"message": "验证码错误!",
|
||||
"message": "验证码错误或已过期",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
+14
-6
@@ -72,15 +72,23 @@ func WeChatAuth(c *gin.Context) {
|
||||
if model.IsWeChatIdAlreadyTaken(wechatId) {
|
||||
user.FillUserByWeChatId()
|
||||
} else {
|
||||
user.Username = "wechat_" + strconv.Itoa(model.GetMaxUserId()+1)
|
||||
user.DisplayName = "WeChat User"
|
||||
user.Role = common.RoleCommonUser
|
||||
user.Status = common.UserStatusEnabled
|
||||
if common.RegisterEnabled {
|
||||
user.Username = "wechat_" + strconv.Itoa(model.GetMaxUserId()+1)
|
||||
user.DisplayName = "WeChat User"
|
||||
user.Role = common.RoleCommonUser
|
||||
user.Status = common.UserStatusEnabled
|
||||
|
||||
if err := user.Insert(); err != nil {
|
||||
if err := user.Insert(); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
"message": err.Error(),
|
||||
"message": "管理员关闭了新用户注册",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user