1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000 |
- 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/h2non/filetype"
- "github.com/rdlucklib/rdluck_tools/http"
- "io/ioutil"
- "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() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- 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]
- // 不依赖于文件扩展名检查文件格式
- fileData, e := ioutil.ReadAll(f)
- if e != nil {
- br.Msg = "上传失败"
- br.ErrMsg = "读取文件失败, Err: " + e.Error()
- return
- }
- // 允许上传图片/音视频/文档/压缩包
- if !filetype.IsImage(fileData) && !filetype.IsVideo(fileData) && !filetype.IsAudio(fileData) && !filetype.IsDocument(fileData) && !filetype.IsArchive(fileData) {
- br.Msg = "不允许上传该格式文件"
- br.ErrMsg = "文件格式有误"
- return
- }
- // 重名校验
- 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
- }
- ext := path.Ext(h.Filename)
- 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 := utils.RESOURCE_DIR + "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
- // }
- //}
- savePath := ossDir + time.Now().Format("200601/20060102/") + ossFileName
- ossClient := services.NewOssClient()
- if ossClient == nil {
- br.Msg = "上传失败"
- br.ErrMsg = "初始化OSS服务失败"
- return
- }
- resourceUrl, e = ossClient.UploadFile(ossFileName, filePath, savePath)
- 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 != "" {
- // 有关键词时全局搜索
- menuCond += ` AND menu_name LIKE ? `
- menuPars = append(menuPars, utils.GetLikeKeyword(keyword))
- resourceCond += ` AND resource_name LIKE ? `
- resourcePars = append(resourcePars, utils.GetLikeKeyword(keyword))
- } 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 = "操作成功"
- }
|