Merge pull request #562 from lzakharov/refactor-close-resources

refactor: close used resources
This commit is contained in:
LinkinStars 2023-10-08 11:16:33 +08:00 committed by GitHub
commit fdd5cd63a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 9 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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 {

View File

@ -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) (