Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

refactor: close used resources

Lev Zakharov5ce518742288
diff --git a/internal/base/conf/conf.go b/internal/base/conf/conf.go
index b0401e1e..b441c471 100644
--- a/internal/base/conf/conf.go
+++ b/internal/base/conf/conf.go
@@ -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
diff --git a/internal/cli/dump.go b/internal/cli/dump.go
index 7b84d862..3696a432 100644
--- a/internal/cli/dump.go
+++ b/internal/cli/dump.go
@@ -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
}
diff --git a/internal/cli/install_check.go b/internal/cli/install_check.go
index 6cb25987..a8926060 100644
--- a/internal/cli/install_check.go
+++ b/internal/cli/install_check.go
@@ -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
diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go
index a4ebeba8..de28c0c5 100644
--- a/internal/migrations/migrations.go
+++ b/internal/migrations/migrations.go
@@ -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 {
diff --git a/internal/service/uploader/upload.go b/internal/service/uploader/upload.go
index 19bf6322..01abec43 100644
--- a/internal/service/uploader/upload.go
+++ b/internal/service/uploader/upload.go
@@ -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) (