mirror of https://gitee.com/answerdev/answer.git
Merge pull request #562 from lzakharov/refactor-close-resources
refactor: close used resources
This commit is contained in:
commit
fdd5cd63a0
|
@ -60,6 +60,7 @@ func ReadConfig(configFilePath string) (c *AllConfig, err error) {
|
|||
func RewriteConfig(configFilePath string, allConfig *AllConfig) error {
|
||||
buf := bytes.Buffer{}
|
||||
enc := yaml.NewEncoder(&buf)
|
||||
defer enc.Close()
|
||||
enc.SetIndent(2)
|
||||
if err := enc.Encode(allConfig); err != nil {
|
||||
return err
|
||||
|
|
|
@ -15,6 +15,7 @@ func DumpAllData(dataConf *data.Database, dumpDataPath string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
if err = db.Ping(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ func CheckDBConnection(dataConf *data.Database) bool {
|
|||
fmt.Printf("connection database failed: %s\n", err)
|
||||
return false
|
||||
}
|
||||
defer db.Close()
|
||||
if err = db.Ping(); err != nil {
|
||||
fmt.Printf("connection ping database failed: %s\n", err)
|
||||
return false
|
||||
|
@ -38,6 +39,7 @@ func CheckDBTableExist(dataConf *data.Database) bool {
|
|||
fmt.Printf("connection database failed: %s\n", err)
|
||||
return false
|
||||
}
|
||||
defer db.Close()
|
||||
if err = db.Ping(); err != nil {
|
||||
fmt.Printf("connection ping database failed: %s\n", err)
|
||||
return false
|
||||
|
|
|
@ -116,6 +116,7 @@ func Migrate(debug bool, dbConf *data.Database, cacheConf *data.CacheConf, upgra
|
|||
fmt.Println("new database failed: ", err.Error())
|
||||
return err
|
||||
}
|
||||
defer engine.Close()
|
||||
|
||||
currentDBVersion, err := GetCurrentDBVersion(engine)
|
||||
if err != nil {
|
||||
|
|
|
@ -91,18 +91,19 @@ func (us *uploaderService) UploadAvatarFile(ctx *gin.Context) (url string, err e
|
|||
|
||||
// max size
|
||||
ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, 5*1024*1024)
|
||||
_, file, err := ctx.Request.FormFile("file")
|
||||
file, fileHeader, err := ctx.Request.FormFile("file")
|
||||
if err != nil {
|
||||
return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
|
||||
}
|
||||
fileExt := strings.ToLower(path.Ext(file.Filename))
|
||||
file.Close()
|
||||
fileExt := strings.ToLower(path.Ext(fileHeader.Filename))
|
||||
if _, ok := FormatExts[fileExt]; !ok {
|
||||
return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
|
||||
}
|
||||
|
||||
newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt)
|
||||
avatarFilePath := path.Join(avatarSubPath, newFilename)
|
||||
return us.uploadFile(ctx, file, avatarFilePath)
|
||||
return us.uploadFile(ctx, fileHeader, avatarFilePath)
|
||||
}
|
||||
|
||||
func (us *uploaderService) AvatarThumbFile(ctx *gin.Context, fileName string, size int) (url string, err error) {
|
||||
|
@ -170,18 +171,19 @@ func (us *uploaderService) UploadPostFile(ctx *gin.Context) (
|
|||
|
||||
// max size
|
||||
ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, 10*1024*1024)
|
||||
_, file, err := ctx.Request.FormFile("file")
|
||||
file, fileHeader, err := ctx.Request.FormFile("file")
|
||||
if err != nil {
|
||||
return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
|
||||
}
|
||||
fileExt := strings.ToLower(path.Ext(file.Filename))
|
||||
defer file.Close()
|
||||
fileExt := strings.ToLower(path.Ext(fileHeader.Filename))
|
||||
if _, ok := FormatExts[fileExt]; !ok {
|
||||
return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
|
||||
}
|
||||
|
||||
newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt)
|
||||
avatarFilePath := path.Join(postSubPath, newFilename)
|
||||
return us.uploadFile(ctx, file, avatarFilePath)
|
||||
return us.uploadFile(ctx, fileHeader, avatarFilePath)
|
||||
}
|
||||
|
||||
func (us *uploaderService) UploadBrandingFile(ctx *gin.Context) (
|
||||
|
@ -196,11 +198,12 @@ func (us *uploaderService) UploadBrandingFile(ctx *gin.Context) (
|
|||
|
||||
// max size
|
||||
ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, 10*1024*1024)
|
||||
_, file, err := ctx.Request.FormFile("file")
|
||||
file, fileHeader, err := ctx.Request.FormFile("file")
|
||||
if err != nil {
|
||||
return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
|
||||
}
|
||||
fileExt := strings.ToLower(path.Ext(file.Filename))
|
||||
file.Close()
|
||||
fileExt := strings.ToLower(path.Ext(fileHeader.Filename))
|
||||
_, ok := FormatExts[fileExt]
|
||||
if !ok && fileExt != ".ico" {
|
||||
return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
|
||||
|
@ -208,7 +211,7 @@ func (us *uploaderService) UploadBrandingFile(ctx *gin.Context) (
|
|||
|
||||
newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt)
|
||||
avatarFilePath := path.Join(brandingSubPath, newFilename)
|
||||
return us.uploadFile(ctx, file, avatarFilePath)
|
||||
return us.uploadFile(ctx, fileHeader, avatarFilePath)
|
||||
}
|
||||
|
||||
func (us *uploaderService) uploadFile(ctx *gin.Context, file *multipart.FileHeader, fileSubPath string) (
|
||||
|
|
Loading…
Reference in New Issue