Fetch the repository succeeded.
package service
import (
"errors"
"fmt"
"mime/multipart"
"os"
"gitee.com/one_love/blog-service/global"
"gitee.com/one_love/blog-service/pkg/upload"
)
type FileInfo struct {
Name string
AccessUrl string
}
func (svc *Service) UploadFile(fileType upload.FileType, file multipart.File, fileHeader *multipart.FileHeader) (*FileInfo, error) {
fileName := upload.GetFileName(fileHeader.Filename)
uploadSavePath := upload.GetSavePath()
dst := uploadSavePath + "/" + fileName
if !upload.CheckContaionExt(fileType, fileName) {
return nil, errors.New("file suffix is not supported")
}
if upload.CheckSavePath(uploadSavePath) {
if err := upload.CreateSavePath(uploadSavePath, os.ModePerm); err != nil {
return nil, errors.New("filed to create save directory")
}
}
if upload.CheckMaxSize(fileType, file) {
return nil, errors.New("exceeded maxium file limit")
}
if upload.CheckPermission(uploadSavePath) {
return nil, errors.New("insufficient file permissions")
}
if err := upload.SaveFile(fileHeader, dst); err != nil {
fmt.Println(dst)
return nil, err
}
accessUrl := global.AppSetting.UploadServerUrl + "/" + fileName
return &FileInfo{Name: fileName, AccessUrl: accessUrl}, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。