123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961 |
- package controllers
- import (
- "archive/zip"
- "encoding/json"
- "eta/eta_api/models"
- "eta/eta_api/services"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/http"
- "os"
- "path"
- "strconv"
- "strings"
- "time"
- )
- // CloudDiskController 共享云盘
- type CloudDiskController struct {
- BaseAuthController
- }
- // MenuCreate 创建文件夹
- // @Title 创建文件夹
- // @Description 创建文件夹
- // @Param request body models.CloudDiskMenuCreateReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /menu/create [post]
- func (this *CloudDiskController) MenuCreate() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.CloudDiskMenuCreateReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- req.MenuName = strings.TrimSpace(req.MenuName)
- if req.MenuName == "" {
- br.Msg = "文件夹名称不可为空"
- return
- }
- // 权限校验
- ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
- return
- }
- if !ok {
- br.Msg = "无权操作"
- return
- }
- // 重名校验
- existItem := new(models.CloudDiskMenu)
- existCond := ` AND menu_name = ? AND parent_id = ? `
- existPars := make([]interface{}, 0)
- existPars = append(existPars, req.MenuName, req.ParentId)
- if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "获取重名目录失败, Err: " + e.Error()
- return
- }
- if existItem != nil && existItem.MenuId > 0 {
- br.Msg = "该文件夹已存在"
- return
- }
- nowTime := time.Now().Local()
- newMenu := &models.CloudDiskMenu{
- MenuName: req.MenuName,
- ParentId: req.ParentId,
- AdminId: this.SysUser.AdminId,
- AdminName: this.SysUser.RealName,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- }
- if e := newMenu.Create(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "新增目录失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // MenuRename 重命名文件夹
- // @Title 重命名文件夹
- // @Description 重命名文件夹
- // @Param request body models.CloudDiskMenuRenameReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /menu/rename [post]
- func (this *CloudDiskController) MenuRename() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.CloudDiskMenuRenameReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if req.MenuId <= 0 {
- br.Msg = "参数有误"
- return
- }
- req.MenuName = strings.TrimSpace(req.MenuName)
- if req.MenuName == "" {
- br.Msg = "文件夹名称不可为空"
- return
- }
- // 权限校验
- ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
- return
- }
- if !ok {
- br.Msg = "无权操作"
- return
- }
- // 重名校验
- menuItem := new(models.CloudDiskMenu)
- if e := menuItem.GetItemById(req.MenuId); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取目录信息失败, Err: " + e.Error()
- return
- }
- existItem := new(models.CloudDiskMenu)
- existCond := ` AND menu_name = ? AND parent_id = ? AND menu_id <> ? `
- existPars := make([]interface{}, 0)
- existPars = append(existPars, req.MenuName, menuItem.ParentId, menuItem.MenuId)
- if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "获取重名目录失败, Err: " + e.Error()
- return
- }
- if existItem != nil && existItem.MenuId > 0 {
- br.Msg = "该文件夹已存在"
- return
- }
- menuItem.MenuName = req.MenuName
- menuItem.ModifyTime = time.Now().Local()
- if e := menuItem.Update([]string{"MenuName", "ModifyTime"}); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "目录重命名失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // MenuDelete 删除文件夹
- // @Title 删除文件夹
- // @Description 删除文件夹
- // @Param request body models.CloudDiskMenuDeleteReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /menu/delete [post]
- func (this *CloudDiskController) MenuDelete() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.CloudDiskMenuDeleteReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if req.MenuId <= 0 {
- br.Msg = "参数有误"
- return
- }
- // 权限校验
- ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
- return
- }
- if !ok {
- br.Msg = "无权操作"
- return
- }
- // 获取文件夹子目录IDs
- menuIds := make([]int, 0)
- menuIds = append(menuIds, req.MenuId)
- childIds, e := services.GetCloudDiskMenuIdsByParentId(req.MenuId)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取子目录IDs失败, ErrMsg: " + e.Error()
- return
- }
- menuIds = append(menuIds, childIds...)
- // 删除目录及目录下所有文件
- if e = models.DeleteCloudDiskMenuAndResourcesByMenuIds(menuIds); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "删除目录及文件失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // MenuTree 目录分类树
- // @Title 目录分类树
- // @Description 目录分类树
- // @Success 200 Ret=200 获取成功
- // @router /menu/tree [get]
- func (this *CloudDiskController) MenuTree() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- treeData, e := services.GetCloudDiskMenuTree()
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取目录分类树失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = treeData
- }
- // MenuResources 目录文件
- // @Title 目录文件
- // @Description 获取目录下所有文件
- // @Param MenuId query int true "目录ID"
- // @Success 200 Ret=200 获取成功
- // @router /menu/resources [get]
- func (this *CloudDiskController) MenuResources() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- menuId, _ := this.GetInt("MenuId", 0)
- if menuId <= 0 {
- br.Msg = "参数有误"
- return
- }
- menuItem := new(models.CloudDiskMenu)
- if e := menuItem.GetItemById(menuId); e != nil {
- br.Msg = "目录已被删除, 请刷新页面"
- br.ErrMsg = "获取目录信息失败, Err: " + e.Error()
- return
- }
- // 获取该目录子目录IDs
- menuIds := make([]int, 0)
- menuIds = append(menuIds, menuId)
- childIds, e := services.GetCloudDiskMenuIdsByParentId(menuId)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取子目录IDs失败, ErrMsg: " + e.Error()
- return
- }
- menuIds = append(menuIds, childIds...)
- // 获取目录下所有文件
- resourceOB := new(models.CloudDiskResource)
- resourceCond := ` AND menu_id IN (` + utils.GetOrmInReplace(len(menuIds)) + `) `
- resourcePars := make([]interface{}, 0)
- resourcePars = append(resourcePars, menuIds)
- resourceFieldArr := []string{"resource_id", "resource_name", "resource_suffix", "resource_url", "menu_id"}
- resourceList, e := resourceOB.GetItemsByCondition(resourceCond, resourcePars, resourceFieldArr, "create_time ASC")
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取目录下所有文件失败, Err: " + e.Error()
- return
- }
- // 获取目录列表, 取出parentMap
- listCond := ``
- listPars := make([]interface{}, 0)
- menuList, e := menuItem.GetItemsByCondition(listCond, listPars, []string{}, "")
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取目录列表失败, Err: " + e.Error()
- return
- }
- menuMap := make(map[int]*models.CloudDiskMenu)
- for i := range menuList {
- menuMap[menuList[i].MenuId] = menuList[i]
- }
- type MenuResource struct {
- ResourceId int
- ResourceName string
- ResourceSuffix string
- ResourceUrl string
- OriginName string
- }
- respList := make([]*MenuResource, 0)
- for i := range resourceList {
- n := resourceList[i].ResourceName + resourceList[i].ResourceSuffix
- p := services.FillMenuPath2File(resourceList[i].MenuId, resourceList[i].MenuId, n, menuMap) // 完整的文件路径
- respList = append(respList, &MenuResource{
- ResourceId: resourceList[i].ResourceId,
- ResourceName: p,
- ResourceSuffix: resourceList[i].ResourceSuffix,
- ResourceUrl: resourceList[i].ResourceUrl,
- OriginName: n,
- })
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = respList
- }
- // MenuDownload 目录文件打包下载
- // @Title 目录文件打包下载
- // @Description 目录文件打包下载
- // @Param MenuId query int true "目录ID"
- // @Success 200 Ret=200 操作成功
- // @router /menu/download [get]
- func (this *CloudDiskController) MenuDownload() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- menuId, _ := this.GetInt("MenuId", 0)
- if menuId <= 0 {
- br.Msg = "参数有误"
- return
- }
- menuItem := new(models.CloudDiskMenu)
- if e := menuItem.GetItemById(menuId); e != nil {
- br.Msg = "目录已被删除, 请刷新页面"
- br.ErrMsg = "获取目录信息失败, Err: " + e.Error()
- return
- }
- // 获取该目录子目录IDs
- menuIds := make([]int, 0)
- menuIds = append(menuIds, menuId)
- childIds, e := services.GetCloudDiskMenuIdsByParentId(menuId)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取子目录IDs失败, ErrMsg: " + e.Error()
- return
- }
- menuIds = append(menuIds, childIds...)
- // 获取目录下所有文件
- resourceOB := new(models.CloudDiskResource)
- resourceCond := ` AND menu_id IN (` + utils.GetOrmInReplace(len(menuIds)) + `) `
- resourcePars := make([]interface{}, 0)
- resourcePars = append(resourcePars, menuIds)
- resourceFieldArr := []string{"resource_id", "resource_name", "resource_suffix", "resource_url"}
- resourceList, e := resourceOB.GetItemsByCondition(resourceCond, resourcePars, resourceFieldArr, "create_time ASC")
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取目录下所有文件失败, Err: " + e.Error()
- return
- }
- if len(resourceList) == 0 {
- br.Msg = "无文件可下载"
- return
- }
- // 创建zip
- zipName := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
- savePath := zipName
- zipFile, err := os.Create(zipName)
- if err != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "生成压缩文件失败, Err: " + e.Error()
- return
- }
- zipWriter := zip.NewWriter(zipFile)
- // 生成zip过程中报错关闭
- defer func() {
- if err != nil {
- _ = zipWriter.Close()
- _ = zipFile.Close()
- }
- _ = os.Remove(savePath)
- }()
- // 获取资源, 写入zip
- for i := range resourceList {
- if resourceList[i].ResourceName == "" || resourceList[i].ResourceSuffix == "" || resourceList[i].ResourceUrl == "" {
- continue
- }
- fmt.Printf("开始压缩第%d个文件\n", i+1)
- ioWriter, err := zipWriter.Create(fmt.Sprintf("%s.%s", resourceList[i].ResourceName, resourceList[i].ResourceSuffix))
- if err != nil {
- if os.IsPermission(err) {
- br.Msg = "操作失败"
- br.ErrMsg = "打包权限不足, Err: " + err.Error()
- return
- }
- br.Msg = "操作失败"
- br.ErrMsg = "压缩出错, Err: " + err.Error()
- return
- }
- var content []byte
- content, err = http.Get(resourceList[i].ResourceUrl)
- if err != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "资源获取失败, Err: " + err.Error()
- return
- }
- _, err = ioWriter.Write(content)
- if err != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "压缩文件写入失败, Err: " + err.Error()
- return
- }
- fmt.Printf("第%d个文件写入成功\n", i+1)
- }
- // 生成zip后关闭,否则下载文件会损坏
- _ = zipWriter.Close()
- _ = zipFile.Close()
- this.Ctx.Output.Download(savePath, fmt.Sprintf("%s.zip", menuItem.MenuName))
- br.Ret = 200
- br.Msg = "下载成功"
- br.Success = true
- }
- // CheckResourceRepeat 校验目录下文件重名
- // @Title 校验目录下文件重名
- // @Description 校验目录下文件重名
- // @Param request body models.CloudDiskCheckResourceRepeatReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /resource/check_repeat [post]
- func (this *CloudDiskController) CheckResourceRepeat() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.CloudDiskCheckResourceRepeatReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- // 检测接口允许menuId为0, 上传不允许
- if len(req.FileNames) == 0 {
- br.Msg = "请选择文件"
- return
- }
- // 获取该级目录下的文件
- resourceOB := new(models.CloudDiskResource)
- resourceCond := ` AND menu_id = ? `
- resourcePars := make([]interface{}, 0)
- resourcePars = append(resourcePars, req.MenuId)
- resourceFieldArr := []string{"resource_id", "resource_name", "resource_suffix"}
- resourceList, e := resourceOB.GetItemsByCondition(resourceCond, resourcePars, resourceFieldArr, "create_time ASC")
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取目录下文件失败, Err: " + e.Error()
- return
- }
- resourceLen := len(resourceList)
- respList := make([]*models.CloudDiskCheckResourceRepeatResp, 0)
- for i := range req.FileNames {
- v := new(models.CloudDiskCheckResourceRepeatResp)
- v.OriginName = req.FileNames[i]
- r := false
- a := v.OriginName
- if resourceLen > 0 {
- r, a = services.CheckFileNameFormFiles(v.OriginName, "", resourceList, 0)
- }
- v.IsRepeat = r
- v.AvailableName = a
- respList = append(respList, v)
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- br.Data = respList
- }
- // ResourceUpload 上传文件
- // @Title 上传文件
- // @Description 上传文件
- // @Param MenuId query int true "目录ID"
- // @Param File query file true "文件"
- // @Success 200 Ret=200 操作成功
- // @router /resource/upload [post]
- func (this *CloudDiskController) ResourceUpload() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- menuId, _ := this.GetInt("MenuId", 0)
- if menuId <= 0 {
- br.Msg = "不允许上传文件至根目录"
- return
- }
- originName := this.GetString("OriginName", "")
- if originName == "" {
- br.Msg = "请上传文件名"
- return
- }
- if !strings.Contains(originName, ".") {
- br.Msg = "文件名有误"
- return
- }
- f, h, e := this.GetFile("file")
- if e != nil {
- br.Msg = "获取资源信息失败"
- br.ErrMsg = "获取资源信息失败, Err:" + e.Error()
- return
- }
- defer func() {
- _ = f.Close()
- }()
- extIndex := strings.LastIndex(originName, ".")
- fileName := originName[:extIndex]
- ext := path.Ext(h.Filename)
- // 重名校验
- existItem := new(models.CloudDiskResource)
- existCond := ` AND resource_name = ? AND menu_id = ? `
- existPars := make([]interface{}, 0)
- existPars = append(existPars, fileName, menuId)
- if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "获取重名文件失败, Err: " + e.Error()
- return
- }
- if existItem != nil && existItem.MenuId > 0 {
- br.Msg = fmt.Sprintf("文件名%s已存在", fileName)
- return
- }
- uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
- if e = os.MkdirAll(uploadDir, utils.DIR_MOD); e != nil {
- br.Msg = "存储目录创建失败"
- br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
- return
- }
- ossFileName := utils.GetRandStringNoSpecialChar(28) + ext
- filePath := uploadDir + "/" + ossFileName
- if e = this.SaveToFile("file", filePath); e != nil {
- br.Msg = "文件保存失败"
- br.ErrMsg = "文件保存失败, Err:" + e.Error()
- return
- }
- defer func() {
- _ = os.Remove(filePath)
- }()
- // 上传到阿里云
- ossDir := "static/cloud_disk/"
- resourceUrl := ``
- //上传到阿里云 和 minio
- if utils.ObjectStorageClient == "minio" {
- resourceUrl, e = services.UploadMinIoToDir(ossFileName, filePath, ossDir, "")
- if e != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败, Err:" + e.Error()
- return
- }
- } else {
- resourceUrl, e = services.UploadAliyunToDir(ossFileName, filePath, ossDir, "")
- if e != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败, Err:" + e.Error()
- return
- }
- }
- // 新增云盘资源
- extMap := services.GetCloudDiskResourceFileTypeExtMap()
- resourceIcon := extMap[ext]
- if resourceIcon == "" {
- resourceIcon = models.CloudDiskResourceFileTypeIconUnknown
- }
- resourceItem := &models.CloudDiskResource{
- MenuId: menuId,
- ResourceName: fileName,
- ResourceSuffix: ext,
- ResourceUrl: resourceUrl,
- ResourceIcon: resourceIcon,
- AdminId: this.SysUser.AdminId,
- AdminName: this.SysUser.RealName,
- Size: h.Size,
- CreateTime: time.Now().Local(),
- ModifyTime: time.Now().Local(),
- }
- if e = resourceItem.Create(); e != nil {
- br.Msg = "上传失败"
- br.ErrMsg = "新增云盘资源失败, Err: " + e.Error()
- return
- }
- // 更新目录大小
- go func() {
- if e = services.UpdateCloudDiskMenuSize(menuId); e != nil {
- alarm_msg.SendAlarmMsg("更新云盘目录大小失败, Err: "+e.Error(), 3)
- }
- }()
- br.Msg = "上传成功"
- br.Ret = 200
- br.Success = true
- br.Data = resourceItem
- }
- // ResourceRename 重命名文件
- // @Title 重命名文件
- // @Description 重命名文件
- // @Param request body models.CloudDiskResourceRenameReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /resource/rename [post]
- func (this *CloudDiskController) ResourceRename() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.CloudDiskResourceRenameReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if req.ResourceId <= 0 {
- br.Msg = "参数有误"
- return
- }
- req.ResourceName = strings.TrimSpace(req.ResourceName)
- if req.ResourceName == "" {
- br.Msg = "文件名称不可为空"
- return
- }
- // 权限校验-仅上传人或管理员可操作
- resourceItem := new(models.CloudDiskResource)
- if e := resourceItem.GetItemById(req.ResourceId); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取文件信息失败, Err: " + e.Error()
- return
- }
- if resourceItem.AdminId != this.SysUser.AdminId {
- ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
- return
- }
- if !ok {
- br.Msg = "无权操作"
- return
- }
- }
- // 重名校验
- existItem := new(models.CloudDiskResource)
- existCond := ` AND resource_name = ? AND menu_id = ? AND resource_id <> ? `
- existPars := make([]interface{}, 0)
- existPars = append(existPars, req.ResourceName, resourceItem.MenuId, resourceItem.ResourceId)
- if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "操作失败"
- br.ErrMsg = "获取重名文件失败, Err: " + e.Error()
- return
- }
- if existItem != nil && existItem.MenuId > 0 {
- br.Msg = "该文件名已存在"
- return
- }
- resourceItem.ResourceName = req.ResourceName
- resourceItem.ModifyTime = time.Now().Local()
- if e := resourceItem.Update([]string{"ResourceName", "ModifyTime"}); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "文件重命名失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // ResourceDelete 删除文件
- // @Title 删除文件
- // @Description 删除文件
- // @Param request body models.CloudDiskResourceDeleteReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /resource/delete [post]
- func (this *CloudDiskController) ResourceDelete() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.CloudDiskResourceDeleteReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if req.ResourceId <= 0 {
- br.Msg = "参数有误"
- return
- }
- // 权限校验-仅上传人或管理员可操作
- resourceItem := new(models.CloudDiskResource)
- if e := resourceItem.GetItemById(req.ResourceId); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取文件信息失败, Err: " + e.Error()
- return
- }
- if resourceItem.AdminId != this.SysUser.AdminId {
- ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
- return
- }
- if !ok {
- br.Msg = "无权操作"
- return
- }
- }
- if e := resourceItem.Delete(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "删除文件失败, Err: " + e.Error()
- return
- }
- // 更新目录大小
- go func() {
- if e := services.UpdateCloudDiskMenuSize(resourceItem.MenuId); e != nil {
- alarm_msg.SendAlarmMsg("更新云盘目录大小失败, Err: "+e.Error(), 3)
- }
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // List 云盘列表
- // @Title 云盘列表
- // @Description 云盘列表
- // @Param MenuId query int false "目录ID"
- // @Param Keyword query string false "关键词"
- // @Param SortType query int false "排序方式: 0-默认; 1-创建时间倒序; 2-创建时间正序"
- // @Success 200 Ret=200 获取成功
- // @router /list [get]
- func (this *CloudDiskController) List() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- menuId, _ := this.GetInt("MenuId", 0)
- keyword := this.GetString("Keyword", "")
- sortType, _ := this.GetInt("SortType", 0)
- menuCond := ``
- menuPars := make([]interface{}, 0)
- resourceCond := ``
- resourcePars := make([]interface{}, 0)
- if keyword != "" {
- // 有关键词时全局搜索
- kw := "%" + keyword + "%"
- menuCond += ` AND menu_name LIKE ? `
- menuPars = append(menuPars, kw)
- resourceCond += ` AND resource_name LIKE ? `
- resourcePars = append(resourcePars, kw)
- } else {
- menuCond += ` AND parent_id = ? `
- menuPars = append(menuPars, menuId)
- resourceCond += ` AND menu_id = ? `
- resourcePars = append(resourcePars, menuId)
- }
- orderRule := `create_time DESC`
- if sortType == 2 {
- orderRule = `create_time ASC`
- }
- // 父级目录
- parentItem := new(models.CloudDiskMenu)
- parentCond := ``
- parentPars := make([]interface{}, 0)
- parentList, e := parentItem.GetItemsByCondition(parentCond, parentPars, []string{}, "")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取父级目录列表失败, Err: " + e.Error()
- return
- }
- parentNameMap := make(map[int]string)
- menuMap := make(map[int]*models.CloudDiskMenu)
- for i := range parentList {
- parentNameMap[parentList[i].MenuId] = parentList[i].MenuName
- menuMap[parentList[i].MenuId] = parentList[i]
- }
- list, e := models.GetCloudDiskMenuAndResourceList(menuCond, resourceCond, menuPars, resourcePars, orderRule)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取云盘列表数据失败, Err: " + e.Error()
- return
- }
- // 管理员权限
- ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
- return
- }
- listResp := new(models.CloudDiskListResp)
- listResp.UploadResourceAuth = true
- if ok {
- listResp.CreateMenuAuth = true
- listResp.BatchDelAuth = true
- }
- menuCount := 0
- for i := range list {
- list[i].ParentName = parentNameMap[list[i].ParentId]
- if list[i].ItemType == 1 {
- menuCount += 1
- list[i].ItemIcon = models.CloudDiskResourceFileTypeIconMenu
- }
- list[i].SizeName = utils.FormatFileSize(list[i].Size)
- // 按钮权限
- au := new(models.CloudDiskListButton)
- if ok || (list[i].AdminId == this.SysUser.AdminId) {
- au.RenameAuth = true
- au.DelAuth = true
- }
- list[i].ButtonAuth = au
- }
- // 根目录无子目录时, 关闭上传文件按钮
- if menuId == 0 && menuCount == 0 {
- listResp.UploadResourceAuth = false
- }
- // 面包屑
- pathArr := make([]*models.CloudDiskMenuResourcePath, 0)
- pathArr = services.GetCloudDiskMenuOrResourcePath(menuId, 0, menuMap, menuMap[menuId], pathArr)
- listResp.ListPath = pathArr
- listResp.List = list
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = listResp
- }
- // BatchDelete 批量删除
- // @Title 批量删除
- // @Description 批量删除
- // @Param request body models.CloudDiskBatchDeleteReq true "type json string"
- // @Success 200 Ret=200 操作成功
- // @router /batch_delete [post]
- func (this *CloudDiskController) BatchDelete() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.CloudDiskBatchDeleteReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if req.MenuIds == "" && req.ResourceIds == "" {
- br.Msg = "请选择目录或文件"
- return
- }
- menuIds := make([]int, 0)
- resourceIds := make([]int, 0)
- if req.MenuIds != "" {
- menuIdArr := strings.Split(req.MenuIds, ",")
- for i := range menuIdArr {
- n, e := strconv.Atoi(menuIdArr[i])
- if e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数有误, Err:" + e.Error()
- return
- }
- menuIds = append(menuIds, n)
- }
- }
- if req.ResourceIds != "" {
- resourceIdArr := strings.Split(req.ResourceIds, ",")
- for i := range resourceIdArr {
- n, e := strconv.Atoi(resourceIdArr[i])
- if e != nil {
- br.Msg = "参数有误"
- br.ErrMsg = "参数有误, Err:" + e.Error()
- return
- }
- resourceIds = append(resourceIds, n)
- }
- }
- // 权限校验-仅管理员可操作
- ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
- return
- }
- if !ok {
- br.Msg = "无权操作"
- return
- }
- if e = models.BatchDeleteCloudDiskMenuAndResource(menuIds, resourceIds); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "批量删除目录及文件失败, Err:" + e.Error()
- return
- }
- // 更新目录大小
- if req.MenuId > 0 {
- go func() {
- if e := services.UpdateCloudDiskMenuSize(req.MenuId); e != nil {
- alarm_msg.SendAlarmMsg("更新云盘目录大小失败, Err: "+e.Error(), 3)
- }
- }()
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
|