feat✨ oss 文件上传

This commit is contained in:
zhangwenjian committed 2020-11-12 20:18:53 +08:00
1 parent 2399824d35
commit 52be3f6153
25 files changed
+1411 -175

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
package dto
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"go-admin/app/admin/models"
"go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/tools"
)
type SysFileDirSearch struct {
dto.Pagination `search:"-"`
ID uint `form:"ID" search:"type:exact;column:id;table:sys_file_dir" comment:"标识"`
Label string `form:"label" search:"type:exact;column:label;table:sys_file_dir" comment:"目录名称"`
PId string `form:"pId" search:"type:exact;column:p_id;table:sys_file_dir" comment:"上级目录"`
Sort string `form:"sort" search:"type:exact;column:sort;table:sys_file_dir" comment:"排序"`
Path string `form:"path" search:"type:exact;column:path;table:sys_file_dir" comment:"路径"`
}
func (m *SysFileDirSearch) GetNeedSearch() interface{} {
return *m
}
func (m *SysFileDirSearch) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBind(m)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %s", msgID, err.Error())
}
return err
}
func (m *SysFileDirSearch) Generate() dto.Index {
o := *m
return &o
}
type SysFileDirControl struct {
ID uint `uri:"ID" comment:"标识"` // 标识
Label string `json:"label" comment:"目录名称"`
PId uint `json:"pId" comment:"上级目录"`
Sort string `json:"sort" comment:"排序"`
Path string `json:"path" comment:"路径"`
}
func (s *SysFileDirControl) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBindUri(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error())
return err
}
err = ctx.ShouldBind(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error())
}
return err
}
func (s *SysFileDirControl) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysFileDirControl) GenerateM() (common.ActiveRecord, error) {
return &models.SysFileDir{
Model: gorm.Model{ID: s.ID},
Label: s.Label,
PId: s.PId,
Sort: s.Sort,
Path: s.Path,
}, nil
}
func (s *SysFileDirControl) GetId() interface{} {
return s.ID
}
type SysFileDirById struct {
dto.ObjectById
}
func (s *SysFileDirById) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysFileDirById) GenerateM() (common.ActiveRecord, error) {
return &models.SysFileDir{}, nil
}
+104
View File
@@ -0,0 +1,104 @@
package dto
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"go-admin/app/admin/models"
"go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/tools"
)
type SysFileInfoSearch struct {
dto.Pagination `search:"-"`
ID uint `form:"ID" search:"type:exact;column:id;table:sys_file_info" comment:"标识"`
Type string `form:"type" search:"type:exact;column:type;table:sys_file_info" comment:"类型"`
Name string `form:"name" search:"type:exact;column:name;table:sys_file_info" comment:"名称"`
PId string `form:"pId" search:"type:exact;column:p_id;table:sys_file_info" comment:"目录"`
Source string `form:"source" search:"type:exact;column:source;table:sys_file_info" comment:"来源"`
}
func (m *SysFileInfoSearch) GetNeedSearch() interface{} {
return *m
}
func (m *SysFileInfoSearch) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBind(m)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %s", msgID, err.Error())
}
return err
}
func (m *SysFileInfoSearch) Generate() dto.Index {
o := *m
return &o
}
type SysFileInfoControl struct {
ID uint `uri:"ID" comment:"标识"` // 标识
Type string `json:"type" comment:"类型"`
Name string `json:"name" comment:"名称"`
Size string `json:"size" comment:"大小"`
PId uint `json:"pId" comment:"目录"`
Source string `json:"source" comment:"来源"`
Url string `json:"url" comment:"地址"`
FullUrl string `json:"fullUrl" comment:"全地址"`
}
func (s *SysFileInfoControl) Bind(ctx *gin.Context) error {
msgID := tools.GenerateMsgIDFromContext(ctx)
err := ctx.ShouldBindUri(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error())
return err
}
err = ctx.ShouldBind(s)
if err != nil {
log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error())
}
return err
}
func (s *SysFileInfoControl) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysFileInfoControl) GenerateM() (common.ActiveRecord, error) {
return &models.SysFileInfo{
Model: gorm.Model{ID: s.ID},
Type: s.Type,
Name: s.Name,
Size: s.Size,
PId: s.PId,
Source: s.Source,
Url: s.Url,
FullUrl: s.FullUrl,
}, nil
}
func (s *SysFileInfoControl) GetId() interface{} {
return s.ID
}
type SysFileInfoById struct {
dto.ObjectById
}
func (s *SysFileInfoById) Generate() dto.Control {
cp := *s
return &cp
}
func (s *SysFileInfoById) GenerateM() (common.ActiveRecord, error) {
return &models.SysFileInfo{}, nil
}
+171
View File
@@ -0,0 +1,171 @@
package service
import (
"errors"
"go-admin/app/admin/models"
"go-admin/common/actions"
cDto "go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/common/service"
"go-admin/tools"
"gorm.io/gorm"
)
type SysFileDir struct {
service.Service
}
// GetSysFileDirPage 获取SysFileDir列表
func (e *SysFileDir) GetSysFileDirPage(c cDto.Index, list *[]models.SysFileDirL) error {
var err error
var data models.SysFileDir
msgID := e.MsgID
err = e.Orm.Model(&data).
Scopes(
cDto.MakeCondition(c.GetNeedSearch()),
//cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
).
Find(list). //Limit(-1).Offset(-1).
Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// GetSysFileDir 获取SysFileDir对象
func (e *SysFileDir) GetSysFileDir(d cDto.Control, model *models.SysFileDir) error {
var err error
var data models.SysFileDir
msgID := e.MsgID
db := e.Orm.Model(&data).
First(model, d.GetId())
err = db.Error
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
err = errors.New("查看对象不存在或无权查看")
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// InsertSysFileDir 创建SysFileDir对象
func (e *SysFileDir) InsertSysFileDir(model common.ActiveRecord) error {
var err error
var data models.SysFileDir
msgID := e.MsgID
db := e.Orm.Model(&data).
Create(model)
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
id := model.GetId()
path := "/" + tools.UIntToString(id.(uint))
db = e.Orm.Model(&data).
First(&data, model.GetId())
err = db.Error
if data.PId != 0 {
var dept models.SysFileDir
e.Orm.Model(&data).Where("id = ?", data.PId).First(&dept)
path = dept.Path + path
} else {
path = "/0" + path
}
//var mp = map[string]string{}
//mp["path"] = path
if err := e.Orm.Model(&data).Where("id = ?", data.ID).Update("path",path).Error; err != nil {
return err
}
return nil
}
// UpdateSysFileDir 修改SysFileDir对象
func (e *SysFileDir) UpdateSysFileDir(c common.ActiveRecord, p *actions.DataPermission) error {
var err error
var data models.SysFileDir
msgID := e.MsgID
db := e.Orm.Model(&data).
Scopes(
actions.Permission(data.TableName(), p),
).Where(c.GetId()).Updates(c)
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.RowsAffected == 0 {
return errors.New("无权更新该数据")
}
return nil
}
// RemoveSysFileDir 删除SysFileDir
func (e *SysFileDir) RemoveSysFileDir(d cDto.Control, c common.ActiveRecord, p *actions.DataPermission) error {
var err error
var data models.SysFileDir
msgID := e.MsgID
db := e.Orm.Model(&data).
Scopes(
actions.Permission(data.TableName(), p),
).Where(d.GetId()).Delete(c)
if db.Error != nil {
err = db.Error
log.Errorf("MsgID[%s] Delete error: %s", msgID, err)
return err
}
if db.RowsAffected == 0 {
err = errors.New("无权删除该数据")
return err
}
return nil
}
func (e *SysFileDir) SetSysFileDir(c cDto.Index) (*[]models.SysFileDirL, error) {
var list []models.SysFileDirL
err := e.GetSysFileDirPage(c, &list)
m := make([]models.SysFileDirL, 0)
for i := 0; i < len(list); i++ {
if list[i].PId != 0 {
continue
}
info := SysFileDirCall(&list, list[i])
m = append(m, info)
}
return &m, err
}
func SysFileDirCall(list *[]models.SysFileDirL, m models.SysFileDirL) models.SysFileDirL {
listGroup := *list
min := make([]models.SysFileDirL, 0)
for j := 0; j < len(listGroup); j++ {
if m.ID != listGroup[j].PId {
continue
}
mi := models.SysFileDirL{}
mi.ID = listGroup[j].ID
mi.PId = listGroup[j].PId
mi.Label = listGroup[j].Label
mi.Sort = listGroup[j].Sort
mi.CreatedAt = listGroup[j].CreatedAt
mi.UpdatedAt = listGroup[j].UpdatedAt
mi.Children = []models.SysFileDirL{}
ms := SysFileDirCall(list, mi)
min = append(min, ms)
}
m.Children = min
return m
}
+119
View File
@@ -0,0 +1,119 @@
package service
import (
"errors"
"go-admin/app/admin/models"
"go-admin/common/actions"
cDto "go-admin/common/dto"
"go-admin/common/log"
common "go-admin/common/models"
"go-admin/common/service"
"gorm.io/gorm"
)
type SysFileInfo struct {
service.Service
}
// GetSysFileInfoPage 获取SysFileInfo列表
func (e *SysFileInfo) GetSysFileInfoPage(c cDto.Index, p *actions.DataPermission, list *[]models.SysFileInfo, count *int64) error {
var err error
var data models.SysFileInfo
msgID := e.MsgID
err = e.Orm.Model(&data).
Scopes(
cDto.MakeCondition(c.GetNeedSearch()),
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
actions.Permission(data.TableName(), p),
).
Find(list).Limit(-1).Offset(-1).
Count(count).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// GetSysFileInfo 获取SysFileInfo对象
func (e *SysFileInfo) GetSysFileInfo(d cDto.Control, p *actions.DataPermission, model *models.SysFileInfo) error {
var err error
var data models.SysFileInfo
msgID := e.MsgID
db := e.Orm.Model(&data).
Scopes(
actions.Permission(data.TableName(), p),
).
First(model, d.GetId())
err = db.Error
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
err = errors.New("查看对象不存在或无权查看")
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// InsertSysFileInfo 创建SysFileInfo对象
func (e *SysFileInfo) InsertSysFileInfo(model common.ActiveRecord) error {
var err error
var data models.SysFileInfo
msgID := e.MsgID
err = e.Orm.Model(&data).
Create(model).Error
if err != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
return nil
}
// UpdateSysFileInfo 修改SysFileInfo对象
func (e *SysFileInfo) UpdateSysFileInfo(c common.ActiveRecord, p *actions.DataPermission) error {
var err error
var data models.SysFileInfo
msgID := e.MsgID
db := e.Orm.Model(&data).
Scopes(
actions.Permission(data.TableName(), p),
).Where(c.GetId()).Updates(c)
if db.Error != nil {
log.Errorf("msgID[%s] db error:%s", msgID, err)
return err
}
if db.RowsAffected == 0 {
return errors.New("无权更新该数据")
}
return nil
}
// RemoveSysFileInfo 删除SysFileInfo
func (e *SysFileInfo) RemoveSysFileInfo(d cDto.Control, c common.ActiveRecord, p *actions.DataPermission) error {
var err error
var data models.SysFileInfo
msgID := e.MsgID
db := e.Orm.Model(&data).
Scopes(
actions.Permission(data.TableName(), p),
).Where(d.GetId()).Delete(c)
if db.Error != nil {
err = db.Error
log.Errorf("MsgID[%s] Delete error: %s", msgID, err)
return err
}
if db.RowsAffected == 0 {
err = errors.New("无权删除该数据")
return err
}
return nil
}