diff --git a/dao/file.go b/dao/file.go index c44c1f0..8133f31 100644 --- a/dao/file.go +++ b/dao/file.go @@ -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 } diff --git a/handler/tool.go b/handler/tool.go index 42a387c..b1de959 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -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