add option for avatar/profilebg change
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, true) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postFile) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -56,7 +56,7 @@ func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Requ
|
||||
}
|
||||
|
||||
func HandleAttachmentFileDownload(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
)
|
||||
|
||||
func HandleDm(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ func HandleDm(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleUserGetConnectionMessages(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -158,7 +158,7 @@ func HandleUserGetConnectionMessages(response http.ResponseWriter, request *http
|
||||
}
|
||||
|
||||
func HandleUserNewConnection(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -217,7 +217,7 @@ func HandleUserNewConnection(response http.ResponseWriter, request *http.Request
|
||||
}
|
||||
|
||||
func HandleUserDeleteConnection(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -269,7 +269,7 @@ func HandleUserDeleteConnection(response http.ResponseWriter, request *http.Requ
|
||||
}
|
||||
|
||||
func HandleUserElevateConnection(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -329,7 +329,7 @@ func HandleUserElevateConnection(response http.ResponseWriter, request *http.Req
|
||||
}
|
||||
|
||||
func HandleUserGetConnections(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
|
||||
@@ -1,21 +1,63 @@
|
||||
package httpRequest
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go-socket/packages/globals"
|
||||
)
|
||||
|
||||
func postValidCheckWithResponseOnFail(response *http.ResponseWriter, request *http.Request, withFile bool) bool {
|
||||
type postType uint8
|
||||
|
||||
const (
|
||||
postNormal postType = iota
|
||||
postFile
|
||||
postAvatar
|
||||
postProfileBg
|
||||
)
|
||||
|
||||
func postValidCheckWithResponseOnFail(response *http.ResponseWriter, request *http.Request, pt postType) bool {
|
||||
if request.Method != http.MethodPost {
|
||||
http.Error(*response, "POST only", http.StatusMethodNotAllowed)
|
||||
return false
|
||||
}
|
||||
if withFile && request.ContentLength > int64(globals.MaxPostWithFileBytes) ||
|
||||
!withFile && request.ContentLength > int64(globals.MaxPostBytes) {
|
||||
|
||||
var maxSize int64
|
||||
switch pt {
|
||||
case postFile:
|
||||
maxSize = int64(globals.MaxPostWithFileBytes)
|
||||
case postAvatar:
|
||||
maxSize = int64(globals.MaxPostWithAvatar)
|
||||
case postProfileBg:
|
||||
maxSize = int64(globals.MaxPostWithProfileBg)
|
||||
default:
|
||||
maxSize = int64(globals.MaxPostBytes)
|
||||
}
|
||||
|
||||
if request.ContentLength > maxSize {
|
||||
http.Error(*response, "Request too large", http.StatusRequestEntityTooLarge)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func isImage(r io.Reader) (bool, string, error) {
|
||||
buf := make([]byte, 512)
|
||||
n, err := io.ReadFull(r, buf)
|
||||
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
|
||||
return false, "", err
|
||||
}
|
||||
|
||||
contentType := http.DetectContentType(buf[:n])
|
||||
isImage := strings.HasPrefix(contentType, "image/") && contentType != "image/svg+xml"
|
||||
|
||||
if seeker, ok := r.(io.Seeker); ok {
|
||||
if _, err := seeker.Seek(0, io.SeekStart); err != nil {
|
||||
return isImage, contentType, err
|
||||
}
|
||||
}
|
||||
|
||||
return isImage, contentType, nil
|
||||
}
|
||||
|
||||
@@ -2,11 +2,12 @@ package httpRequest
|
||||
|
||||
import (
|
||||
json2 "encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"go-socket/packages/convertions"
|
||||
"go-socket/packages/globals"
|
||||
"go-socket/packages/minio"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"go-socket/packages/cache"
|
||||
"go-socket/packages/passwords"
|
||||
@@ -18,7 +19,7 @@ import (
|
||||
)
|
||||
|
||||
func HandleUserNewToken(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -77,7 +78,7 @@ func HandleUserNewToken(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleUserNew(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -118,7 +119,7 @@ func HandleUserNew(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleUserDelete(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -140,7 +141,7 @@ func HandleUserDelete(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleUserModProfile(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -190,8 +191,7 @@ func HandleUserModProfile(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleUserModAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
|
||||
if !postValidCheckWithResponseOnFail(&response, request, true) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postAvatar) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -202,7 +202,7 @@ func HandleUserModAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxPostWithFileBytes))
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxPostWithAvatar))
|
||||
|
||||
if err = request.ParseMultipartForm(int64(globals.MaxPostBytes)); err != nil {
|
||||
http.Error(response, "invalid multipart form", http.StatusBadRequest)
|
||||
@@ -221,9 +221,21 @@ func HandleUserModAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
contentType := header.Header.Get("Content-Type")
|
||||
key := minio.GetKey(conn.Id, contentType, minio.File)
|
||||
isImg, contentType, err := isImage(file)
|
||||
if err != nil || !isImg {
|
||||
http.Error(response, "invalid file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if user.Avatar != "" {
|
||||
err = minio.Delete(ctx, string(minio.UserAvatarPrefix)+user.Avatar)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
key := minio.GetKey(conn.Id, contentType, minio.File)
|
||||
if err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
|
||||
"originalName": header.Filename,
|
||||
"uploaderId": user.Id.String(),
|
||||
@@ -231,4 +243,77 @@ func HandleUserModAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
http.Error(response, "upload failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
user.Avatar = key[len(minio.UserAvatarPrefix):]
|
||||
err = postgresql.UserUpdateProfile(ctx, user, types.UserProfileUpdateList{Avatar: true})
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleUserModProfileBg(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postProfileBg) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
|
||||
user, err := getUserByToken(ctx, request.Header.Get("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxPostWithProfileBg))
|
||||
|
||||
if err = request.ParseMultipartForm(int64(globals.MaxPostBytes)); err != nil {
|
||||
http.Error(response, "invalid multipart form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
conn, ok := getConnectionWithResponseOnFail(&response, request, user)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := request.FormFile("file")
|
||||
if err != nil {
|
||||
http.Error(response, "missing file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
isImg, contentType, err := isImage(file)
|
||||
if err != nil || !isImg {
|
||||
http.Error(response, "invalid file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if user.ProfileBg != "" {
|
||||
err = minio.Delete(ctx, string(minio.UserProfileBgPrefix)+user.ProfileBg)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
key := minio.GetKey(conn.Id, contentType, minio.UserProfileBg)
|
||||
if err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
|
||||
"originalName": header.Filename,
|
||||
"uploaderId": user.Id.String(),
|
||||
}); err != nil {
|
||||
http.Error(response, "upload failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
user.ProfileBg = key[len(minio.UserProfileBgPrefix):]
|
||||
err = postgresql.UserUpdateProfile(ctx, user, types.UserProfileUpdateList{ProfileBg: true})
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user