新增游戏图标,用户头像上传功能

This commit is contained in:
chy
2025-06-12 10:19:27 +08:00
parent f7cffcae21
commit cc8646156a
8 changed files with 117 additions and 5 deletions

View File

@ -55,3 +55,65 @@ func (s *sUpload) Upload(ctx context.Context, in *model.UploadIn) (res *model.Up
Url: uploadRes.Url,
}, nil
}
func (s *sUpload) UploadGameImg(ctx context.Context, in *model.UploadIn) (res *model.UploadOut, err error) {
// 判断文件格式
fileType := in.File.Header.Get("Content-Type")
if !strings.HasPrefix(fileType, "image/") {
return nil, gerror.New("图片格式错误")
}
//判断图片大小
if in.File.Size > 10*1024*1024 {
return nil, gerror.New("图片大小最多10M")
}
// 获取图片后缀
exit := strings.Split(fileType, "/")[1]
// 文件名称
fileName := strings.ToLower("gameImg/" + grand.Letters(32) + "." + exit)
// 开始上传图片
client, ok := oss.GetClient("aliyun")
if !ok {
return nil, ecode.Fail.Sub("未配置OSS")
}
uploadRes, err := client.UploadFile(ctx, &model.OssUploadFileInput{
Filename: fileName,
File: in.File,
})
if err != nil {
return nil, err
}
return &model.UploadOut{
Url: uploadRes.Url,
}, nil
}
func (s *sUpload) UploadUserImg(ctx context.Context, in *model.UploadIn) (res *model.UploadOut, err error) {
// 判断文件格式
fileType := in.File.Header.Get("Content-Type")
if !strings.HasPrefix(fileType, "image/") {
return nil, gerror.New("图片格式错误")
}
//判断图片大小
if in.File.Size > 10*1024*1024 {
return nil, gerror.New("图片大小最多10M")
}
// 获取图片后缀
exit := strings.Split(fileType, "/")[1]
// 文件名称
fileName := strings.ToLower("userImg/" + grand.Letters(32) + "." + exit)
// 开始上传图片
client, ok := oss.GetClient("aliyun")
if !ok {
return nil, ecode.Fail.Sub("未配置OSS")
}
uploadRes, err := client.UploadFile(ctx, &model.OssUploadFileInput{
Filename: fileName,
File: in.File,
})
if err != nil {
return nil, err
}
return &model.UploadOut{
Url: uploadRes.Url,
}, nil
}