Skip to content

Commit

Permalink
push修改代码
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegit committed Mar 21, 2022
1 parent badf8b3 commit bcd81bb
Show file tree
Hide file tree
Showing 34 changed files with 764 additions and 572 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ require (
github.com/swaggo/gin-swagger v1.3.0
github.com/swaggo/swag v1.7.0
github.com/wenzhenxi/gorsa v0.0.0-20210524035706-528c7050d703
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gorm.io/driver/mysql v1.1.1
gorm.io/driver/postgres v1.1.0
gorm.io/driver/postgres v1.3.1
gorm.io/driver/sqlite v1.1.4
gorm.io/driver/sqlserver v1.0.7
gorm.io/gorm v1.21.11
gorm.io/plugin/soft_delete v1.0.1
gorm.io/gorm v1.23.2
gorm.io/plugin/soft_delete v1.1.0
)
375 changes: 170 additions & 205 deletions go.sum

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions nice/file/upload/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
package upload

import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/lifegit/go-gulu/v2/nice/crypto"
"github.com/lifegit/go-gulu/v2/nice/file"
"github.com/lifegit/go-gulu/v2/nice/rand"
"mime/multipart"
"net/url"
"os"
"path"
"strconv"
Expand All @@ -23,14 +26,15 @@ var (
)

type FileUpload interface {
Upload(c *gin.Context, attribute FileAttribute) File
Upload(c *gin.Context, attribute FileAttribute) (File, error)
Remove(path string) error
URL(domain, filePath string) (*url.URL, error)
}

type File struct {
Error error
Url string
Save string
Name string
Size int64
Save string
}

type FileAttribute struct {
Expand All @@ -40,6 +44,19 @@ type FileAttribute struct {
MaxByte int64
}

func (a FileAttribute) CheckFile(fileHeader *multipart.FileHeader) (err error) {
if strings.Join(a.Exts, "") != strings.Join(AllowAnyExts, "") {
if !CheckFileExt(fileHeader.Filename, &a.Exts) {
return errors.New("不支持此文件类型")
}
}
if fileHeader.Size > a.MaxByte {
return errors.New(fmt.Sprintf("文件大小最大允许%dM", a.MaxByte/1024/1024))
}

return
}

// 使用md5随机生成一个文件名
func RandFileName(name string) string {
ext := path.Ext(name)
Expand Down
78 changes: 29 additions & 49 deletions nice/file/upload/local.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @Author: TheLife
* @Date: 2020-2-25 9:00 下午
*/
package upload

import (
Expand All @@ -10,81 +6,65 @@ import (
"github.com/gin-gonic/gin"
"github.com/lifegit/go-gulu/v2/nice/file"
"io"
"net/url"
"os"
"path"
)

type Local struct {
baseDir string
domain string
}

func NewLocal(baseDir, domain string) *Local {
func NewLocal(baseDir string) *Local {
return &Local{
baseDir: baseDir,
domain: domain,
}
}

// r.StaticFS("/upload/images", http.Dir(upload.GetImageFullPath()))
func (l Local) Upload(c *gin.Context, attribute FileAttribute) File {
u := File{}
func (l *Local) URL(domain, filePath string) (*url.URL, error) {
return url.Parse(fmt.Sprintf("%s/%s", domain, filePath))
}

f, image, err := c.Request.FormFile(attribute.Key)
// r.StaticFS("/upload/resources", http.Dir(upload.GetresourceFullPath()))
func (l *Local) Upload(c *gin.Context, attr FileAttribute) (f File, e error) {
// init http file
rFile, rFileHeader, err := c.Request.FormFile(attr.Key)
if err != nil {
u.Error = errors.New("未上传文件")
return u
}
defer f.Close()
if !CheckFileExt(image.Filename, &attribute.Exts) {
u.Error = errors.New("不支持此文件类型")
return u
}
if image.Size > attribute.MaxByte {
u.Error = errors.New(fmt.Sprintf("文件大小最大允许%dM", attribute.MaxByte/1024/1024))
return u
return f, errors.New("未上传文件")
}
defer rFile.Close()

if err = ReadyDir(path.Join(l.baseDir, attribute.DirPath)); err != nil {
u.Error = errors.New("系统无权限保存文件")
return u
// check file
if e = attr.CheckFile(rFileHeader); e != nil {
return f, e
}

run := 0
finalDirFileName, simpleDirFileName := "", ""
// make file information
for {
imageName := RandFileName(image.Filename)
simpleDirFileName = path.Join(attribute.DirPath, imageName)
finalDirFileName = path.Join(l.baseDir, simpleDirFileName)
if !file.IsExist(finalDirFileName) {
f.Name = RandFileName(rFileHeader.Filename)
f.Save = path.Join(l.baseDir, attr.DirPath, f.Name)
if !file.IsExist(f.Save) {
break
}

run++
if run >= 5 {
u.Error = errors.New("文件名无法构造")
return u
}
}
f.Size = rFileHeader.Size

out, err := os.Create(finalDirFileName)
// create file
if err := ReadyDir(path.Join(l.baseDir, attr.DirPath)); err != nil {
return f, errors.New("系统无权限保存文件")
}
out, err := os.Create(f.Save)
if err != nil {
u.Error = errors.New("文件IO错误")
return u
return f, errors.New("文件IO错误")
}
defer out.Close()

if _, err = io.Copy(out, f); err != nil {
u.Error = errors.New("无法保存文件")
return u
if _, err = io.Copy(out, rFile); err != nil {
return f, errors.New("无法保存文件")
}

u.Url = path.Join(l.domain, simpleDirFileName)
u.Save = finalDirFileName

return u
return
}

func (l Local) Remove(filename string) error {
func (l *Local) Remove(filename string) error {
return os.Remove(filename)
}
72 changes: 27 additions & 45 deletions nice/file/upload/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
package upload

import (
"bytes"
"errors"
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/gin-gonic/gin"
"net/url"
"path"
)

Expand All @@ -18,7 +18,7 @@ type Oss struct {
domain string
}

func NewOss(endpoint, accessKeyID, accessKeySecret, bucketName string, haveDomain string) (*Oss, error) {
func NewOss(endpoint, accessKeyID, accessKeySecret, bucketName string) (*Oss, error) {
client, err := oss.New(endpoint, accessKeyID, accessKeySecret)
if err != nil {
return nil, errors.New("oss-new 初始化失败")
Expand All @@ -27,68 +27,50 @@ func NewOss(endpoint, accessKeyID, accessKeySecret, bucketName string, haveDomai
if err != nil {
return nil, errors.New("oss-bucket 初始化失败")
}
//-internal
if haveDomain == "" {
haveDomain = fmt.Sprintf("https://%s.%s/", bucketName, endpoint)
}

return &Oss{
bucket: bucket,
domain: haveDomain,
}, nil
}

func (oss *Oss) Upload(c *gin.Context, attribute FileAttribute) File {
u := File{}
func (oss *Oss) URL(domain, filePath string) (*url.URL, error) {
if domain == "" {
domain = fmt.Sprintf("https://%s.%s/", oss.bucket.BucketName, oss.bucket.Client.Config.Endpoint)
}

return url.Parse(fmt.Sprintf("%s/%s", domain, filePath))
}

f, image, err := c.Request.FormFile(attribute.Key)
func (oss *Oss) Upload(c *gin.Context, attr FileAttribute) (f File, e error) {
// init http file
rFile, rFileHeader, err := c.Request.FormFile(attr.Key)
if err != nil {
u.Error = errors.New("未上传文件")
return u
}
defer f.Close()
if !CheckFileExt(image.Filename, &attribute.Exts) {
u.Error = errors.New("不支持此文件类型")
return u
return f, errors.New("未上传文件")
}
defer rFile.Close()

if image.Size > attribute.MaxByte {
u.Error = errors.New(fmt.Sprintf("文件大小最大允许%dM", attribute.MaxByte/1024/1024))
return u
// check file
if e = attr.CheckFile(rFileHeader); e != nil {
return f, e
}

run := 0
finalDirFileName := ""
// make file information
for {
imageName := RandFileName(image.Filename)
finalDirFileName = path.Join(attribute.DirPath, imageName)
if isExist, _ := oss.bucket.IsObjectExist(finalDirFileName); !isExist {
f.Name = RandFileName(rFileHeader.Filename)
f.Save = path.Join(attr.DirPath, f.Name)
if isExist, _ := oss.bucket.IsObjectExist(f.Save); !isExist {
break
}

run++
if run >= 5 {
u.Error = errors.New("文件名无法构造")
return u
}
}

buf := make([]byte, image.Size)
_, err = f.ReadAt(buf, 0)
if err != nil {
u.Error = errors.New("文件IO错误")
return u
}
f.Size = rFileHeader.Size

err = oss.bucket.PutObject(finalDirFileName, bytes.NewReader(buf))
// create file
err = oss.bucket.PutObject(f.Save, rFile)
if err != nil {
u.Error = errors.New("无法保存文件")
return u
return f, errors.New("无法保存文件")
}

u.Url = path.Join(oss.domain, finalDirFileName)
u.Save = finalDirFileName

return u
return
}

func (oss *Oss) Remove(filename string) error {
Expand Down
Loading

0 comments on commit bcd81bb

Please sign in to comment.