cloud_disk.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package services
  2. import (
  3. "eta_gn/eta_api/models"
  4. "eta_gn/eta_api/models/company"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "sort"
  8. "strings"
  9. "time"
  10. )
  11. // CheckCloudDiskManagerAuth 校验云盘管理员权限
  12. func CheckCloudDiskManagerAuth(roleCode string) (ok bool, err error) {
  13. confKey := "cloud_disk_manager_role"
  14. conf, e := company.GetConfigDetailByCode(confKey)
  15. if e != nil {
  16. err = fmt.Errorf("获取管理员权限配置失败, Err: %s", e.Error())
  17. return
  18. }
  19. if conf.ConfigValue == "" {
  20. err = fmt.Errorf("管理员权限配置信息有误")
  21. return
  22. }
  23. authArr := strings.Split(conf.ConfigValue, ",")
  24. if utils.InArrayByStr(authArr, roleCode) {
  25. ok = true
  26. }
  27. return
  28. }
  29. // GetCloudDiskMenuIdsByParentId 云盘目录-通过父级ID获取所有子ID
  30. func GetCloudDiskMenuIdsByParentId(menuId int) (menuIds []int, err error) {
  31. menuOB := new(models.CloudDiskMenu)
  32. menuCond := ``
  33. menuPars := make([]interface{}, 0)
  34. menuFields := []string{"menu_id", "parent_id"}
  35. menus, e := menuOB.GetItemsByCondition(menuCond, menuPars, menuFields, "")
  36. if e != nil {
  37. err = fmt.Errorf("获取目录列表失败, Err: %s", e.Error())
  38. return
  39. }
  40. // 若父级ID为0, 取全部
  41. if menuId == 0 {
  42. for i := range menus {
  43. menuIds = append(menuIds, menus[i].MenuId)
  44. }
  45. return
  46. }
  47. // 父级ID大于0
  48. menuIds = loadChildrenIds(menuId, menus)
  49. return
  50. }
  51. // loadChildrenIds 递归遍历出子分类ID
  52. func loadChildrenIds(parentId int, menus []*models.CloudDiskMenu) []int {
  53. menuIds := make([]int, 0)
  54. for i := range menus {
  55. if parentId == menus[i].ParentId {
  56. childIds := loadChildrenIds(menus[i].MenuId, menus)
  57. menuIds = append(menuIds, menus[i].MenuId)
  58. menuIds = append(menuIds, childIds...)
  59. }
  60. }
  61. return menuIds
  62. }
  63. // GetCloudDiskMenuTree 云盘-获取目录分类树
  64. func GetCloudDiskMenuTree() (list []*models.CloudDiskMenuTree, err error) {
  65. menuOB := new(models.CloudDiskMenu)
  66. menuCond := ``
  67. menuPars := make([]interface{}, 0)
  68. menus, e := menuOB.GetItemsByCondition(menuCond, menuPars, []string{}, "create_time DESC")
  69. if e != nil {
  70. err = fmt.Errorf("获取目录列表失败, Err: %s", e.Error())
  71. return
  72. }
  73. list = make([]*models.CloudDiskMenuTree, 0)
  74. for i := range menus {
  75. if menus[i].ParentId == 0 {
  76. v := new(models.CloudDiskMenuTree)
  77. v.CloudDiskMenu = menus[i]
  78. v.Children = loadMenuTree(menus[i].MenuId, menus, v)
  79. list = append(list, v)
  80. }
  81. }
  82. return
  83. }
  84. // loadMenuTree 递归加载分类树
  85. func loadMenuTree(parentId int, menus []*models.CloudDiskMenu, parentItem *models.CloudDiskMenuTree) (children []*models.CloudDiskMenuTree) {
  86. children = make([]*models.CloudDiskMenuTree, 0)
  87. for i := range menus {
  88. if parentId == menus[i].ParentId {
  89. v := new(models.CloudDiskMenuTree)
  90. v.CloudDiskMenu = menus[i]
  91. v.Children = loadMenuTree(menus[i].MenuId, menus, v)
  92. children = append(children, v)
  93. }
  94. }
  95. parentItem.Children = children
  96. return
  97. }
  98. // CheckFileNameFormFiles 校验文件名并返回可用名
  99. func CheckFileNameFormFiles(originName, fileName string, fileList []*models.CloudDiskResource, num int) (isRepeat bool, availableName string) {
  100. if originName == "" || !strings.Contains(originName, ".") {
  101. return
  102. }
  103. // 从原名称中取出文件名和格式
  104. extIndex := strings.LastIndex(originName, ".")
  105. name := originName[:extIndex]
  106. ext := originName[extIndex:]
  107. if fileName == "" {
  108. fileName = name
  109. }
  110. availableName = fileName + ext
  111. for i := range fileList {
  112. if fileName == fileList[i].ResourceName {
  113. fileName = fmt.Sprintf("%s(%d)", name, num+1)
  114. isRepeat, availableName = CheckFileNameFormFiles(originName, fileName, fileList, num+1)
  115. isRepeat = true
  116. }
  117. }
  118. return
  119. }
  120. // GetCloudDiskResourceFileTypeExtMap 常见文件类型图标
  121. func GetCloudDiskResourceFileTypeExtMap() map[string]string {
  122. return map[string]string{
  123. ".doc": "https://hzstatic.hzinsights.com/static/icon/file_type_docx.png",
  124. ".docx": "https://hzstatic.hzinsights.com/static/icon/file_type_docx.png",
  125. ".pdf": "https://hzstatic.hzinsights.com/static/icon/file_type_pdf.png",
  126. ".ppt": "https://hzstatic.hzinsights.com/static/icon/file_type_ppt.png",
  127. ".pptx": "https://hzstatic.hzinsights.com/static/icon/file_type_ppt.png",
  128. ".xls": "https://hzstatic.hzinsights.com/static/icon/file_type_xlsx.png",
  129. ".xlsx": "https://hzstatic.hzinsights.com/static/icon/file_type_xlsx.png",
  130. ".jpg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  131. ".jpeg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  132. ".png": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  133. ".bmp": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  134. ".svg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  135. ".gif": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  136. ".mp4": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  137. ".wmv": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  138. ".mov": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  139. ".mpeg": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  140. ".avi": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  141. ".flv": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  142. ".rm": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  143. ".rmvb": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  144. ".mp3": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
  145. ".wma": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
  146. }
  147. }
  148. // UpdateCloudDiskMenuSize 更新目录大小
  149. func UpdateCloudDiskMenuSize(menuId int) (err error) {
  150. if menuId <= 0 {
  151. return
  152. }
  153. menuItem := new(models.CloudDiskMenu)
  154. if e := menuItem.GetItemById(menuId); e != nil {
  155. err = fmt.Errorf("获取目录失败, Err: %s", e.Error())
  156. return
  157. }
  158. // 获取目录所属文件总大小
  159. menuIds := make([]int, 0)
  160. menuIds = append(menuIds, menuId)
  161. childrenIds, e := GetCloudDiskMenuIdsByParentId(menuId)
  162. if e != nil {
  163. err = fmt.Errorf("获取目录子目录IDs失败, Err: %s", e.Error())
  164. return
  165. }
  166. menuIds = append(menuIds, childrenIds...)
  167. sizeTotal, e := models.GetSizeTotalByMenuIds(menuIds)
  168. if e != nil {
  169. err = fmt.Errorf("获取目录所属文件大小失败失败, Err: %s", e.Error())
  170. return
  171. }
  172. // 更新
  173. menuItem.Size = sizeTotal
  174. menuItem.ModifyTime = time.Now().Local()
  175. if e = menuItem.Update([]string{"Size", "ModifyTime"}); e != nil {
  176. err = fmt.Errorf("更新目录大小失败, Err: %s", e.Error())
  177. return
  178. }
  179. if menuItem.ParentId > 0 {
  180. err = UpdateCloudDiskMenuSize(menuItem.ParentId)
  181. }
  182. return
  183. }
  184. // GetCloudDiskMenuOrResourcePath 获取目录/文件面包屑
  185. func GetCloudDiskMenuOrResourcePath(menuId, level int, menuMap map[int]*models.CloudDiskMenu, originMenu *models.CloudDiskMenu, pathArr []*models.CloudDiskMenuResourcePath) []*models.CloudDiskMenuResourcePath {
  186. if menuId == 0 || len(menuMap) == 0 || originMenu == nil {
  187. return nil
  188. }
  189. if level == 0 {
  190. level = 99
  191. }
  192. menuItem := menuMap[menuId]
  193. if menuItem.ParentId > 0 {
  194. parentItem := menuMap[menuItem.ParentId]
  195. if parentItem != nil {
  196. level -= 1
  197. pathArr = append(pathArr, &models.CloudDiskMenuResourcePath{
  198. MenuId: parentItem.MenuId,
  199. MenuName: parentItem.MenuName,
  200. ParentId: parentItem.ParentId,
  201. Sort: level,
  202. Selected: false,
  203. })
  204. if parentItem.ParentId > 0 {
  205. pathArr = GetCloudDiskMenuOrResourcePath(parentItem.MenuId, level-1, menuMap, originMenu, pathArr)
  206. } else {
  207. pathArr = append(pathArr, &models.CloudDiskMenuResourcePath{
  208. MenuId: originMenu.MenuId,
  209. MenuName: originMenu.MenuName,
  210. ParentId: originMenu.ParentId,
  211. Sort: 99,
  212. Selected: true,
  213. })
  214. }
  215. }
  216. } else {
  217. pathArr = append(pathArr, &models.CloudDiskMenuResourcePath{
  218. MenuId: originMenu.MenuId,
  219. MenuName: originMenu.MenuName,
  220. ParentId: originMenu.ParentId,
  221. Sort: 99,
  222. Selected: true,
  223. })
  224. }
  225. sort.Slice(pathArr, func(k, j int) bool {
  226. return pathArr[k].Sort < pathArr[j].Sort
  227. })
  228. return pathArr
  229. }
  230. // FillMenuPath2File 完善文件的完整路径
  231. func FillMenuPath2File(originMenuId, menuId int, resourcePath string, menuMap map[int]*models.CloudDiskMenu) string {
  232. if menuId == 0 {
  233. return resourcePath
  234. }
  235. menuItem := menuMap[menuId]
  236. if menuItem == nil {
  237. return resourcePath
  238. }
  239. // 若文件就在当前目录是不需要加目录路径的, 仅多级才需要加
  240. if menuItem.ParentId > 0 {
  241. resourcePath = fmt.Sprintf("%s/%s", menuItem.MenuName, resourcePath)
  242. resourcePath = FillMenuPath2File(originMenuId, menuItem.ParentId, resourcePath, menuMap)
  243. }
  244. return resourcePath
  245. }