基础架构变更 增加模块化
This commit is contained in:
91
server/service/example/exa_file_upload_download.go
Normal file
91
server/service/example/exa_file_upload_download.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gin-vue-admin/global"
|
||||
"gin-vue-admin/model/common/request"
|
||||
"gin-vue-admin/model/example"
|
||||
"gin-vue-admin/utils/upload"
|
||||
"mime/multipart"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//@author: [piexlmax](https://github.com/piexlmax)
|
||||
//@function: Upload
|
||||
//@description: 创建文件上传记录
|
||||
//@param: file model.ExaFileUploadAndDownload
|
||||
//@return: error
|
||||
|
||||
func (e *FileUploadAndDownloadService) Upload(file example.ExaFileUploadAndDownload) error {
|
||||
return global.GVA_DB.Create(&file).Error
|
||||
}
|
||||
|
||||
//@author: [piexlmax](https://github.com/piexlmax)
|
||||
//@function: FindFile
|
||||
//@description: 删除文件切片记录
|
||||
//@param: id uint
|
||||
//@return: error, model.ExaFileUploadAndDownload
|
||||
|
||||
func (e *FileUploadAndDownloadService) FindFile(id uint) (error, example.ExaFileUploadAndDownload) {
|
||||
var file example.ExaFileUploadAndDownload
|
||||
err := global.GVA_DB.Where("id = ?", id).First(&file).Error
|
||||
return err, file
|
||||
}
|
||||
|
||||
//@author: [piexlmax](https://github.com/piexlmax)
|
||||
//@function: DeleteFile
|
||||
//@description: 删除文件记录
|
||||
//@param: file model.ExaFileUploadAndDownload
|
||||
//@return: err error
|
||||
|
||||
func (e *FileUploadAndDownloadService) DeleteFile(file example.ExaFileUploadAndDownload) (err error) {
|
||||
var fileFromDb example.ExaFileUploadAndDownload
|
||||
err, fileFromDb = e.FindFile(file.ID)
|
||||
oss := upload.NewOss()
|
||||
if err = oss.DeleteFile(fileFromDb.Key); err != nil {
|
||||
return errors.New("文件删除失败")
|
||||
}
|
||||
err = global.GVA_DB.Where("id = ?", file.ID).Unscoped().Delete(&file).Error
|
||||
return err
|
||||
}
|
||||
|
||||
//@author: [piexlmax](https://github.com/piexlmax)
|
||||
//@function: GetFileRecordInfoList
|
||||
//@description: 分页获取数据
|
||||
//@param: info requset.PageInfo
|
||||
//@return: err error, list interface{}, total int64
|
||||
|
||||
func (e *FileUploadAndDownloadService) GetFileRecordInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
|
||||
limit := info.PageSize
|
||||
offset := info.PageSize * (info.Page - 1)
|
||||
db := global.GVA_DB
|
||||
var fileLists []example.ExaFileUploadAndDownload
|
||||
err = db.Find(&fileLists).Count(&total).Error
|
||||
err = db.Limit(limit).Offset(offset).Order("updated_at desc").Find(&fileLists).Error
|
||||
return err, fileLists, total
|
||||
}
|
||||
|
||||
//@author: [piexlmax](https://github.com/piexlmax)
|
||||
//@function: UploadFile
|
||||
//@description: 根据配置文件判断是文件上传到本地或者七牛云
|
||||
//@param: header *multipart.FileHeader, noSave string
|
||||
//@return: err error, file model.ExaFileUploadAndDownload
|
||||
|
||||
func (e *FileUploadAndDownloadService) UploadFile(header *multipart.FileHeader, noSave string) (err error, file example.ExaFileUploadAndDownload) {
|
||||
oss := upload.NewOss()
|
||||
filePath, key, uploadErr := oss.UploadFile(header)
|
||||
if uploadErr != nil {
|
||||
panic(err)
|
||||
}
|
||||
if noSave == "0" {
|
||||
s := strings.Split(header.Filename, ".")
|
||||
f := example.ExaFileUploadAndDownload{
|
||||
Url: filePath,
|
||||
Name: header.Filename,
|
||||
Tag: s[len(s)-1],
|
||||
Key: key,
|
||||
}
|
||||
return e.Upload(f), f
|
||||
}
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user