Compare commits

..

2 Commits

Author SHA1 Message Date
junleea 720322bfd6 Merge branch 'refs/heads/feat-file' 2024-12-27 20:36:08 +08:00
junleea fc915a0f7c 修复文件上传文件去重 2024-12-27 20:36:04 +08:00
2 changed files with 13 additions and 10 deletions

View File

@ -18,11 +18,11 @@ type File struct {
Md5 string `gorm:"column:md5;type:varchar(255);uniqueIndex:idx_file_name"`
}
func CreateFile(fileStoreName, fileName, fileType, filePath string, fileSize, authID int, NeedAuth bool) File {
file := File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize, AuthID: authID, NeedAuth: NeedAuth}
func CreateFile(fileStoreName, fileName, fileType, filePath, md5Str string, fileSize, authID int, NeedAuth bool) File {
file := File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize, AuthID: authID, NeedAuth: NeedAuth, Md5: md5Str}
result := DB.Create(&file)
if result.Error != nil {
return 0
return File{}
}
return file
}

View File

@ -126,6 +126,7 @@ func UploadFile(c *gin.Context) {
//从请求头获取upload_type
uploadType := c.PostForm("upload_type")
authType := c.PostForm("auth_type")
md5_ := c.PostForm("md5")
if uploadType == "" {
c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"})
return
@ -143,14 +144,16 @@ func UploadFile(c *gin.Context) {
return
}
//计算文件md5值
file_, _ := file.Open()
md5Value := service.CalculateFileMd5(file_)
if md5Value == "" {
c.JSON(http.StatusOK, gin.H{"error": "计算文件MD5值失败", "code": proto.UploadFileFailed, "message": "failed"})
return
if md5_ == "" {
file_, _ := file.Open()
md5_ = service.CalculateFileMd5(file_)
if md5_ == "" {
c.JSON(http.StatusOK, gin.H{"error": "计算文件MD5值失败", "code": proto.UploadFileFailed, "message": "failed"})
return
}
}
//查询文件是否已存在
fileExist := dao.FindFileByMd5(md5Value)
fileExist := dao.FindFileByMd5(md5_)
if fileExist.ID != 0 {
fileExist.FilePath = ""
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": fileExist})
@ -173,7 +176,7 @@ func UploadFile(c *gin.Context) {
} else if authType == "private" {
auth_type_ = true
}
file_record := dao.CreateFile(fileStoreName, fileName, fileType, filePath, fileSize, id1, auth_type_)
file_record := dao.CreateFile(fileStoreName, fileName, fileType, filePath, md5_, fileSize, id1, auth_type_)
if file_record.ID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
return