add set background and icon of hub functions

This commit is contained in:
2026-05-03 19:30:22 +02:00
parent c0d4483154
commit 22e2d18810
5 changed files with 131 additions and 23 deletions
+4
View File
@@ -18,6 +18,8 @@ var (
MaxRequestWithFileBytes uint32 = 1 << 30 MaxRequestWithFileBytes uint32 = 1 << 30
MaxRequestWithAvatarBytes uint32 = 1 << 20 MaxRequestWithAvatarBytes uint32 = 1 << 20
MaxRequestWithProfileBgBytes uint32 = 4 << 20 MaxRequestWithProfileBgBytes uint32 = 4 << 20
MaxRequestWithHubIconBytes uint32 = 1 << 20
MaxRequestWithHubBackgroundBytes uint32 = 10 << 20
FileProcessingPartBytes uint64 = 12 << 20 FileProcessingPartBytes uint64 = 12 << 20
FileProcessingThreads uint = 3 FileProcessingThreads uint = 3
FileDownloadLinkTtl time.Duration = 24 * time.Hour FileDownloadLinkTtl time.Duration = 24 * time.Hour
@@ -31,6 +33,8 @@ type configFile struct {
MaxRequestWithFileBytes uint32 `toml:"max_request_with_file_bytes"` MaxRequestWithFileBytes uint32 `toml:"max_request_with_file_bytes"`
MaxRequestWithAvatarBytes uint32 `toml:"max_request_with_avatar_bytes"` MaxRequestWithAvatarBytes uint32 `toml:"max_request_with_avatar_bytes"`
MaxRequestWithProfileBgBytes uint32 `toml:"max_request_with_profile_bg_bytes"` MaxRequestWithProfileBgBytes uint32 `toml:"max_request_with_profile_bg_bytes"`
MaxRequestWithHubIconBytes uint32 `toml:"max_request_with_hub_icon_bytes"`
MaxRequestWithHubBackgroundBytes uint32 `toml:"max_request_with_hub_background_bytes"`
FileProcessingPartBytes uint64 `toml:"file_processing_part_bytes"` FileProcessingPartBytes uint64 `toml:"file_processing_part_bytes"`
FileProcessingThreads uint `toml:"file_processing_threads"` FileProcessingThreads uint `toml:"file_processing_threads"`
FileStorageBucketName string `toml:"file_storage_bucket_name"` FileStorageBucketName string `toml:"file_storage_bucket_name"`
+11
View File
@@ -15,6 +15,8 @@ const (
file file
avatar avatar
profileBg profileBg
hubIcon
hubBackground
) )
func validCheckWithResponseOnFail(response http.ResponseWriter, request *http.Request, pt bodyLimit) bool { 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 { switch pt {
case file: case file:
maxSize = int64(config.MaxRequestWithFileBytes) maxSize = int64(config.MaxRequestWithFileBytes)
break
case avatar: case avatar:
maxSize = int64(config.MaxRequestWithAvatarBytes) maxSize = int64(config.MaxRequestWithAvatarBytes)
break
case profileBg: case profileBg:
maxSize = int64(config.MaxRequestWithProfileBgBytes) maxSize = int64(config.MaxRequestWithProfileBgBytes)
break
case hubIcon:
maxSize = int64(config.MaxRequestWithHubIconBytes)
break
case hubBackground:
maxSize = int64(config.MaxRequestWithHubBackgroundBytes)
break
default: default:
maxSize = int64(config.MaxRequestBytes) maxSize = int64(config.MaxRequestBytes)
} }
+85
View File
@@ -10,6 +10,7 @@ import (
"go-socket/packages/convertions" "go-socket/packages/convertions"
"go-socket/packages/cache" "go-socket/packages/cache"
"go-socket/packages/minio"
"go-socket/packages/types" "go-socket/packages/types"
"go-socket/packages/wsServer" "go-socket/packages/wsServer"
@@ -466,6 +467,90 @@ func HandleHubSetColor(response http.ResponseWriter, request *http.Request) {
response.WriteHeader(http.StatusAccepted) 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) { func HandleHubRemove(response http.ResponseWriter, request *http.Request) {
_, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveHub) _, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveHub)
if !ok { if !ok {
+9
View File
@@ -24,6 +24,8 @@ const (
HubChannelFile HubChannelFile
UserAvatar UserAvatar
UserProfileBg UserProfileBg
HubIcon
HubBackground
) )
type DataTypePrefix string type DataTypePrefix string
@@ -33,11 +35,14 @@ const (
HubChannelFilePrefix DataTypePrefix = "hub/" HubChannelFilePrefix DataTypePrefix = "hub/"
UserAvatarPrefix DataTypePrefix = "userAvatar/" UserAvatarPrefix DataTypePrefix = "userAvatar/"
UserProfileBgPrefix DataTypePrefix = "userProfileBg/" UserProfileBgPrefix DataTypePrefix = "userProfileBg/"
HubIconPrefix DataTypePrefix = "hubIcon/"
HubBackgroundPrefix DataTypePrefix = "hubBackground/"
) )
type GetKeyOptions struct { type GetKeyOptions struct {
UserId uuid.UUID UserId uuid.UUID
ConnectionId uuid.UUID ConnectionId uuid.UUID
HubId uuid.UUID
ChannelId uuid.UUID ChannelId uuid.UUID
MimeType string MimeType string
UploadType DataType UploadType DataType
@@ -58,6 +63,10 @@ func GetKey(opts *GetKeyOptions) string {
return string(UserAvatarPrefix) + opts.UserId.String() + key return string(UserAvatarPrefix) + opts.UserId.String() + key
case UserProfileBg: case UserProfileBg:
return string(UserProfileBgPrefix) + opts.UserId.String() + key return string(UserProfileBgPrefix) + opts.UserId.String() + key
case HubIcon:
return string(HubIconPrefix) + opts.HubId.String() + key
case HubBackground:
return string(HubBackgroundPrefix) + opts.HubId.String() + key
default: default:
return string(ConnectionFilePrefix) + opts.ConnectionId.String() + key return string(ConnectionFilePrefix) + opts.ConnectionId.String() + key
} }
-1
View File
@@ -1,6 +1,5 @@
when user not ws connected collect count of unread messages for each conn (add db table in future) when user not ws connected collect count of unread messages for each conn (add db table in future)
add hubs
setting avatar and profilebg setting avatar and profilebg
check when mutex needed check when mutex needed
change api endpoints and body/url/header to follow same case change api endpoints and body/url/header to follow same case