mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
fix(backend): improve error handling in controllers and services by checking errors from Redis operations and adding error comments for deferred function calls
This commit is contained in:
@@ -116,9 +116,12 @@ func VaildateShare(c *echo.Context) error {
|
||||
if latestViewNum < 1 {
|
||||
latestViewNum = -1
|
||||
}
|
||||
models.SetRedisShareInfo(r.ShareId, models.RedisShareInfo{
|
||||
err = models.SetRedisShareInfo(r.ShareId, models.RedisShareInfo{
|
||||
ViewNum: latestViewNum,
|
||||
})
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
|
||||
// 统计分享数
|
||||
currentDate := time.Now().Format("2006-01-02")
|
||||
@@ -132,7 +135,10 @@ func VaildateShare(c *echo.Context) error {
|
||||
}
|
||||
}
|
||||
statData.DownloadNum += 1
|
||||
models.SetRedisStat(currentDate, *statData)
|
||||
err = models.SetRedisStat(currentDate, *statData)
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
|
||||
if shareInfo.Type == models.ShareTypeFile {
|
||||
return utils.HTTPSuccessHandler(c, map[string]any{
|
||||
|
||||
@@ -152,7 +152,7 @@ func UploadFileSlice(c *echo.Context) error {
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
defer file.Close()
|
||||
defer file.Close() //nolint:errcheck
|
||||
|
||||
uploadPath, err := u.GetUploadDirPath()
|
||||
if err != nil {
|
||||
@@ -219,29 +219,26 @@ func FinishUploadTask(c *echo.Context) error {
|
||||
// 计算文件MD5
|
||||
file, err := os.Open(mergeFilePath)
|
||||
if err != nil {
|
||||
file.Close()
|
||||
os.Remove(mergeFilePath)
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
defer file.Close() //nolint:errcheck
|
||||
|
||||
file_hash, err := u.GetFileMd5(file)
|
||||
|
||||
if err != nil {
|
||||
file.Close()
|
||||
os.Remove(mergeFilePath)
|
||||
if err != nil || file_hash != fileInfo.FileHash {
|
||||
defer os.Remove(mergeFilePath) //nolint:errcheck
|
||||
if err == nil {
|
||||
return utils.HTTPErrorHandler(c, ErrFileMD5Mismatch)
|
||||
}
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
|
||||
if file_hash != fileInfo.FileHash {
|
||||
file.Close()
|
||||
os.Remove(mergeFilePath)
|
||||
return utils.HTTPErrorHandler(c, ErrFileMD5Mismatch)
|
||||
}
|
||||
defer file.Close()
|
||||
// 更新文件信息
|
||||
models.SetRedisFileInfo(r.FileId, models.RedisFileInfo{
|
||||
err = models.SetRedisFileInfo(r.FileId, models.RedisFileInfo{
|
||||
FileType: models.FileTypeUpload,
|
||||
})
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
// 统计
|
||||
currentDate := time.Now().Format("2006-01-02")
|
||||
statData, _ := models.GetRedisStat(currentDate)
|
||||
@@ -255,7 +252,10 @@ func FinishUploadTask(c *echo.Context) error {
|
||||
}
|
||||
statData.FileSize += fileInfo.FileSize
|
||||
statData.FileNum += 1
|
||||
models.SetRedisStat(currentDate, *statData)
|
||||
err = models.SetRedisStat(currentDate, *statData)
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
|
||||
return utils.HTTPSuccessHandler(c, map[string]any{
|
||||
"size": fileInfo.FileSize,
|
||||
|
||||
@@ -73,7 +73,7 @@ func CreateShareInfo(c *echo.Context) error {
|
||||
password = hash
|
||||
}
|
||||
|
||||
models.SetRedisShareInfo(id, models.RedisShareInfo{
|
||||
err = models.SetRedisShareInfo(id, models.RedisShareInfo{
|
||||
Data: r.Data,
|
||||
Type: r.Type,
|
||||
CreatedAt: time.Now().Unix(),
|
||||
@@ -84,6 +84,9 @@ func CreateShareInfo(c *echo.Context) error {
|
||||
FileName: r.FileName,
|
||||
ExpireAt: ExpireTime.Unix(),
|
||||
})
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
var pickupCode string
|
||||
if r.Config.HasPickupCode {
|
||||
for {
|
||||
@@ -105,7 +108,10 @@ func CreateShareInfo(c *echo.Context) error {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
shareIDs = append(shareIDs, id)
|
||||
models.SetRedisFileShareRelational(r.Data, shareIDs)
|
||||
err = models.SetRedisFileShareRelational(r.Data, shareIDs)
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
client := u.GetQueueClient()
|
||||
json, err := json.Marshal(map[string]any{"share_id": id, "file_id": r.Data})
|
||||
if err != nil {
|
||||
@@ -132,7 +138,10 @@ func CreateShareInfo(c *echo.Context) error {
|
||||
}
|
||||
}
|
||||
statData.ShareNum += 1
|
||||
models.SetRedisStat(currentDate, *statData)
|
||||
err = models.SetRedisStat(currentDate, *statData)
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
|
||||
return utils.HTTPSuccessHandler(c, map[string]any{
|
||||
"id": id,
|
||||
|
||||
@@ -19,7 +19,7 @@ func CreateFileSlice(fileId string, uploadPath string, fileSlice io.Reader, file
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer dst.Close()
|
||||
defer dst.Close() //nolint:errcheck
|
||||
|
||||
if _, err = io.Copy(dst, fileSlice); err != nil {
|
||||
return "", err
|
||||
@@ -50,13 +50,13 @@ func GetFileSliceList(fileId string, uploadPath string) ([]int, error) {
|
||||
func MergeFileSlices(fileId string, uploadPath string) (string, error) {
|
||||
mergeFilePath := filepath.Join(uploadPath, fileId)
|
||||
slicesPath := filepath.Join(uploadPath, fmt.Sprintf("%s_%s", fileId, "tmp"))
|
||||
defer os.RemoveAll(slicesPath)
|
||||
defer os.RemoveAll(slicesPath) //nolint:errcheck
|
||||
// 创建最终文件
|
||||
destFile, err := os.Create(mergeFilePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("创建合并文件失败: %v", err)
|
||||
}
|
||||
defer destFile.Close()
|
||||
defer destFile.Close() //nolint:errcheck
|
||||
|
||||
fileSliceList, err := GetFileSliceList(fileId, uploadPath)
|
||||
if err != nil {
|
||||
@@ -71,23 +71,19 @@ func MergeFileSlices(fileId string, uploadPath string) (string, error) {
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("打开切片文件失败: %v", err)
|
||||
}
|
||||
|
||||
defer sf.Close() //nolint:errcheck
|
||||
for {
|
||||
n, err := sf.Read(buffer)
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
sf.Close()
|
||||
return "", fmt.Errorf("读取切片文件失败: %v", err)
|
||||
}
|
||||
|
||||
if _, err := destFile.Write(buffer[:n]); err != nil {
|
||||
sf.Close()
|
||||
return "", fmt.Errorf("写入合并文件失败: %v", err)
|
||||
}
|
||||
}
|
||||
sf.Close()
|
||||
}
|
||||
return mergeFilePath, nil
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrPasswordSaltNotSet = errors.New("请配置PASSWORD_SALT")
|
||||
ErrPasswordSaltNotSet = errors.New("PasswordSaltNotSet")
|
||||
)
|
||||
|
||||
func GeneratePasswordHash(password string) (string, error) {
|
||||
|
||||
@@ -16,7 +16,7 @@ func main() {
|
||||
} else {
|
||||
logger, _ = zap.NewDevelopment()
|
||||
}
|
||||
defer logger.Sync()
|
||||
defer logger.Sync() //nolint:errcheck
|
||||
zap.ReplaceGlobals(logger)
|
||||
|
||||
e := echo.New()
|
||||
|
||||
Reference in New Issue
Block a user