-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add virtual path for file uploads * Update launch.json and api_v1.go * Remove unused import and fix directory creation bug * Add clean target to Makefile * feat: add attachment upload api
- Loading branch information
Showing
14 changed files
with
248 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package mygin | ||
|
||
import ( | ||
"fmt" | ||
"go-gin/pkg/utils" | ||
"go-gin/pkg/utils/file" | ||
"go-gin/pkg/utils/image" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type AttchmentUpload struct { | ||
BaseURL string // BaseURL is the base url for the uploaded file | ||
MaxSize int64 // MaxSize is the max file size, default is 2MB | ||
AllowTypes []string // AllowTypes is the allowed file types | ||
FormName string // FormName is the form name for the file, default is "file" | ||
StoreDir string // StoreDir is the directory to store the uploaded file | ||
CreateDateDir bool // CreateDateDir is the flag to create date directory | ||
KeepOriginalName bool // KeepOriginalName is the flag to keep the original file name | ||
} | ||
|
||
type AttchmentUploadResult struct { | ||
Url string `json:"url"` | ||
Name string `json:"name"` | ||
OriginalName string `json:"original_name"` | ||
Size int64 `json:"size"` | ||
MiMe string `json:"mime"` | ||
With int `json:"width"` | ||
Hei int `json:"height"` | ||
Ext string `json:"ext"` | ||
MD5 string `json:"md5"` | ||
SavePath string `json:"save_path"` | ||
} | ||
|
||
func (a *AttchmentUpload) Upload(c *gin.Context) (*AttchmentUploadResult, error) { | ||
result := &AttchmentUploadResult{} | ||
form_file, err := c.FormFile(a.FormName) | ||
if err != nil { | ||
return result, err | ||
} | ||
if form_file.Size > a.MaxSize { | ||
return result, fmt.Errorf("file size too large") | ||
} | ||
form_file_ext := strings.ToLower(filepath.Ext(form_file.Filename)) // eg: .jpg | ||
form_file_fileilename := form_file.Filename | ||
form_file_fileize := form_file.Size | ||
form_file_mime := form_file.Header.Get("Content-Type") | ||
|
||
if len(a.AllowTypes) > 0 && !utils.InArrayString(form_file_mime, a.AllowTypes) { | ||
return result, fmt.Errorf("file type not allowed") | ||
} | ||
|
||
now := time.Now() | ||
year := now.Format("2006") | ||
month := now.Format("01") | ||
day := now.Format("02") | ||
|
||
saveName := utils.GenHexStr(32) + form_file_ext | ||
if a.KeepOriginalName { | ||
saveName = form_file_fileilename | ||
} | ||
|
||
savePath := a.StoreDir | ||
url := fmt.Sprintf("%s/%s", a.BaseURL, saveName) | ||
if a.CreateDateDir { | ||
savePath = path.Join(a.StoreDir, year, month, day) | ||
url = fmt.Sprintf("%s/%s/%s/%s/%s", a.BaseURL, year, month, day, saveName) | ||
} | ||
if err := file.MkdirAllIfNotExists(savePath, os.ModePerm); err != nil { | ||
return result, err | ||
} | ||
|
||
if err := c.SaveUploadedFile(form_file, path.Join(savePath, saveName)); err != nil { | ||
return result, err | ||
} | ||
|
||
md5, _ := file.FileMD5(path.Join(savePath, saveName)) | ||
w, h, _ := image.GetImageSize(path.Join(savePath, saveName)) | ||
|
||
result.Url = url | ||
result.Name = saveName | ||
result.OriginalName = form_file_fileilename | ||
result.Size = form_file_fileize | ||
result.MiMe = form_file_mime | ||
result.Ext = form_file_ext | ||
result.MD5 = md5 | ||
result.With = w | ||
result.Hei = h | ||
result.SavePath = savePath | ||
return result, nil | ||
} | ||
|
||
func NewAttchmentUpload() *AttchmentUpload { | ||
return &AttchmentUpload{ | ||
BaseURL: "/upload", | ||
MaxSize: 1024 * 1024 * 2, | ||
AllowTypes: []string{"image/jpeg", "image/png", "image/gif", "image/jpg"}, | ||
FormName: "file", | ||
StoreDir: "./upload", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package image | ||
|
||
import ( | ||
"bytes" | ||
"image" | ||
_ "image/gif" | ||
_ "image/jpeg" | ||
_ "image/png" | ||
|
||
"os" | ||
) | ||
|
||
func GetImageSize(imgpath string) (int, int, error) { | ||
file, err := os.Open(imgpath) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
defer file.Close() | ||
img, _, err := image.DecodeConfig(file) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
return img.Width, img.Height, nil | ||
} | ||
|
||
func IsImage(data []byte) bool { | ||
_, _, err := image.Decode(bytes.NewReader(data)) | ||
return err == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package singleton | ||
|
||
import ( | ||
"go-gin/pkg/mygin" | ||
) | ||
|
||
var ( | ||
AttchmentUpload *mygin.AttchmentUpload | ||
) | ||
|
||
func LoadUpload() { | ||
AttchmentUpload = &mygin.AttchmentUpload{ | ||
BaseURL: Conf.Site.BaseURL + Conf.Upload.VirtualPath, | ||
MaxSize: Conf.Upload.MaxSize, | ||
AllowTypes: Conf.Upload.AllowTypes, | ||
FormName: "file", | ||
StoreDir: Conf.Upload.Dir, | ||
CreateDateDir: Conf.Upload.CreateDateDir, | ||
KeepOriginalName: Conf.Upload.KeepOriginalName, | ||
} | ||
} |