add set background and icon of hub functions
This commit is contained in:
@@ -15,6 +15,8 @@ const (
|
||||
file
|
||||
avatar
|
||||
profileBg
|
||||
hubIcon
|
||||
hubBackground
|
||||
)
|
||||
|
||||
func validCheckWithResponseOnFail(response http.ResponseWriter, request *http.Request, pt bodyLimit) bool {
|
||||
@@ -22,10 +24,19 @@ func validCheckWithResponseOnFail(response http.ResponseWriter, request *http.Re
|
||||
switch pt {
|
||||
case file:
|
||||
maxSize = int64(config.MaxRequestWithFileBytes)
|
||||
break
|
||||
case avatar:
|
||||
maxSize = int64(config.MaxRequestWithAvatarBytes)
|
||||
break
|
||||
case profileBg:
|
||||
maxSize = int64(config.MaxRequestWithProfileBgBytes)
|
||||
break
|
||||
case hubIcon:
|
||||
maxSize = int64(config.MaxRequestWithHubIconBytes)
|
||||
break
|
||||
case hubBackground:
|
||||
maxSize = int64(config.MaxRequestWithHubBackgroundBytes)
|
||||
break
|
||||
default:
|
||||
maxSize = int64(config.MaxRequestBytes)
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"go-socket/packages/convertions"
|
||||
|
||||
"go-socket/packages/cache"
|
||||
"go-socket/packages/minio"
|
||||
"go-socket/packages/types"
|
||||
"go-socket/packages/wsServer"
|
||||
|
||||
@@ -466,6 +467,90 @@ func HandleHubSetColor(response http.ResponseWriter, request *http.Request) {
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleHubSetIcon(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, ctx, _, ok := hubPermissionContext(response, request, hubIcon, types.PermissionSetHubIcon)
|
||||
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
|
||||
}
|
||||
|
||||
key := minio.GetKey(&minio.GetKeyOptions{
|
||||
MimeType: contentType,
|
||||
UploadType: minio.HubIcon,
|
||||
HubId: hub.Id,
|
||||
})
|
||||
if err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
|
||||
"originalName": header.Filename,
|
||||
}); err != nil {
|
||||
http.Error(response, "upload failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if hub.IconUrl != "" {
|
||||
if err = minio.Delete(ctx, hub.IconUrl); err != nil {
|
||||
minio.Delete(ctx, key)
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
hub.IconUrl = key
|
||||
response.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
func HandleHubSetBg(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, ctx, _, ok := hubPermissionContext(response, request, hubBackground, types.PermissionSetHubBg)
|
||||
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
|
||||
}
|
||||
|
||||
key := minio.GetKey(&minio.GetKeyOptions{
|
||||
MimeType: contentType,
|
||||
UploadType: minio.HubBackground,
|
||||
HubId: hub.Id,
|
||||
})
|
||||
if err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
|
||||
"originalName": header.Filename,
|
||||
}); err != nil {
|
||||
http.Error(response, "upload failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if hub.BgUrl != "" {
|
||||
if err = minio.Delete(ctx, hub.BgUrl); err != nil {
|
||||
minio.Delete(ctx, key)
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
hub.BgUrl = key
|
||||
response.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
func HandleHubRemove(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveHub)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user