cloud_disk.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package services
  2. import (
  3. "eta/eta_api/models"
  4. "eta/eta_api/models/company"
  5. "eta/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. func BuildCloudDiskMenuTree() (root *models.MenuNode, menuNameMap map[int]string) {
  99. menuOB := new(models.CloudDiskMenu)
  100. menuCond := ``
  101. menuPars := make([]interface{}, 0)
  102. menuList, err := menuOB.GetItemsByCondition(menuCond, menuPars, []string{}, "create_time DESC")
  103. if err != nil {
  104. utils.FileLog.Error("获取目录列表失败, Err: %s", err.Error())
  105. return nil, nil
  106. }
  107. if len(menuList) == 0 {
  108. return
  109. }
  110. // 使用 map 存储每个节点的子节点列表
  111. subMenuMap := make(map[int][]*models.CloudDiskMenu)
  112. menuNameMap = make(map[int]string)
  113. for _, menu := range menuList {
  114. subMenuMap[menu.ParentId] = append(subMenuMap[menu.ParentId], menu)
  115. }
  116. root = new(models.MenuNode)
  117. root.MenuName = "虚拟根目录"
  118. buildMenuTree(10, 0, subMenuMap, root, menuNameMap)
  119. return
  120. }
  121. func buildMenuTree(depth, current int, subMenuMap map[int][]*models.CloudDiskMenu, node *models.MenuNode, menuNameMap map[int]string) {
  122. if current > depth {
  123. return
  124. }
  125. children, exists := subMenuMap[node.MenuId]
  126. if !exists {
  127. return
  128. }
  129. for _, childMenu := range children {
  130. var menuName string
  131. if depth == 0 {
  132. menuName = childMenu.MenuName
  133. } else {
  134. menuName = fmt.Sprintf("%s/%s", node.MenuName, childMenu.MenuName)
  135. }
  136. childNode := &models.MenuNode{
  137. MenuId: childMenu.MenuId,
  138. MenuName: menuName,
  139. ParentId: childMenu.ParentId,
  140. Level: node.Level + 1,
  141. }
  142. menuNameMap[childMenu.MenuId] = menuName
  143. node.Children = append(node.Children, childNode)
  144. buildMenuTree(depth, current+1, subMenuMap, childNode, menuNameMap)
  145. }
  146. }
  147. // CheckFileNameFormFiles 校验文件名并返回可用名
  148. func CheckFileNameFormFiles(originName, fileName string, fileList []*models.CloudDiskResource, num int) (isRepeat bool, availableName string) {
  149. if originName == "" || !strings.Contains(originName, ".") {
  150. return
  151. }
  152. // 从原名称中取出文件名和格式
  153. extIndex := strings.LastIndex(originName, ".")
  154. name := originName[:extIndex]
  155. ext := originName[extIndex:]
  156. if fileName == "" {
  157. fileName = name
  158. }
  159. availableName = fileName + ext
  160. for i := range fileList {
  161. if fileName == fileList[i].ResourceName {
  162. fileName = fmt.Sprintf("%s(%d)", name, num+1)
  163. isRepeat, availableName = CheckFileNameFormFiles(originName, fileName, fileList, num+1)
  164. isRepeat = true
  165. }
  166. }
  167. return
  168. }
  169. // GetCloudDiskResourceFileTypeExtMap 常见文件类型图标
  170. func GetCloudDiskResourceFileTypeExtMap() map[string]string {
  171. return map[string]string{
  172. ".doc": "https://hzstatic.hzinsights.com/static/icon/file_type_docx.png",
  173. ".docx": "https://hzstatic.hzinsights.com/static/icon/file_type_docx.png",
  174. ".pdf": "https://hzstatic.hzinsights.com/static/icon/file_type_pdf.png",
  175. ".ppt": "https://hzstatic.hzinsights.com/static/icon/file_type_ppt.png",
  176. ".pptx": "https://hzstatic.hzinsights.com/static/icon/file_type_ppt.png",
  177. ".xls": "https://hzstatic.hzinsights.com/static/icon/file_type_xlsx.png",
  178. ".xlsx": "https://hzstatic.hzinsights.com/static/icon/file_type_xlsx.png",
  179. ".jpg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  180. ".jpeg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  181. ".png": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  182. ".bmp": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  183. ".svg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  184. ".gif": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  185. ".mp4": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  186. ".wmv": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  187. ".mov": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  188. ".mpeg": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  189. ".avi": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  190. ".flv": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  191. ".rm": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  192. ".rmvb": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  193. ".mp3": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
  194. ".wma": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
  195. }
  196. }
  197. // UpdateCloudDiskMenuSize 更新目录大小
  198. func UpdateCloudDiskMenuSize(menuId int) (err error) {
  199. if menuId <= 0 {
  200. return
  201. }
  202. menuItem := new(models.CloudDiskMenu)
  203. if e := menuItem.GetItemById(menuId); e != nil {
  204. err = fmt.Errorf("获取目录失败, Err: %s", e.Error())
  205. return
  206. }
  207. // 获取目录所属文件总大小
  208. menuIds := make([]int, 0)
  209. menuIds = append(menuIds, menuId)
  210. childrenIds, e := GetCloudDiskMenuIdsByParentId(menuId)
  211. if e != nil {
  212. err = fmt.Errorf("获取目录子目录IDs失败, Err: %s", e.Error())
  213. return
  214. }
  215. menuIds = append(menuIds, childrenIds...)
  216. sizeTotal, e := models.GetSizeTotalByMenuIds(menuIds)
  217. if e != nil {
  218. err = fmt.Errorf("获取目录所属文件大小失败失败, Err: %s", e.Error())
  219. return
  220. }
  221. // 更新
  222. menuItem.Size = sizeTotal
  223. menuItem.ModifyTime = time.Now().Local()
  224. if e = menuItem.Update([]string{"Size", "ModifyTime"}); e != nil {
  225. err = fmt.Errorf("更新目录大小失败, Err: %s", e.Error())
  226. return
  227. }
  228. if menuItem.ParentId > 0 {
  229. err = UpdateCloudDiskMenuSize(menuItem.ParentId)
  230. }
  231. return
  232. }
  233. // GetCloudDiskMenuOrResourcePath 获取目录/文件面包屑
  234. func GetCloudDiskMenuOrResourcePath(menuId, level int, menuMap map[int]*models.CloudDiskMenu, originMenu *models.CloudDiskMenu, pathArr []*models.CloudDiskMenuResourcePath) []*models.CloudDiskMenuResourcePath {
  235. if menuId == 0 || len(menuMap) == 0 || originMenu == nil {
  236. return nil
  237. }
  238. if level == 0 {
  239. level = 99
  240. }
  241. menuItem := menuMap[menuId]
  242. if menuItem.ParentId > 0 {
  243. parentItem := menuMap[menuItem.ParentId]
  244. if parentItem != nil {
  245. level -= 1
  246. pathArr = append(pathArr, &models.CloudDiskMenuResourcePath{
  247. MenuId: parentItem.MenuId,
  248. MenuName: parentItem.MenuName,
  249. ParentId: parentItem.ParentId,
  250. Sort: level,
  251. Selected: false,
  252. })
  253. if parentItem.ParentId > 0 {
  254. pathArr = GetCloudDiskMenuOrResourcePath(parentItem.MenuId, level-1, menuMap, originMenu, pathArr)
  255. } else {
  256. pathArr = append(pathArr, &models.CloudDiskMenuResourcePath{
  257. MenuId: originMenu.MenuId,
  258. MenuName: originMenu.MenuName,
  259. ParentId: originMenu.ParentId,
  260. Sort: 99,
  261. Selected: true,
  262. })
  263. }
  264. }
  265. } else {
  266. pathArr = append(pathArr, &models.CloudDiskMenuResourcePath{
  267. MenuId: originMenu.MenuId,
  268. MenuName: originMenu.MenuName,
  269. ParentId: originMenu.ParentId,
  270. Sort: 99,
  271. Selected: true,
  272. })
  273. }
  274. sort.Slice(pathArr, func(k, j int) bool {
  275. return pathArr[k].Sort < pathArr[j].Sort
  276. })
  277. return pathArr
  278. }
  279. // FillMenuPath2File 完善文件的完整路径
  280. func FillMenuPath2File(originMenuId, menuId int, resourcePath string, menuMap map[int]*models.CloudDiskMenu) string {
  281. if menuId == 0 {
  282. return resourcePath
  283. }
  284. menuItem := menuMap[menuId]
  285. if menuItem == nil {
  286. return resourcePath
  287. }
  288. // 若文件就在当前目录是不需要加目录路径的, 仅多级才需要加
  289. if menuItem.ParentId > 0 {
  290. resourcePath = fmt.Sprintf("%s/%s", menuItem.MenuName, resourcePath)
  291. resourcePath = FillMenuPath2File(originMenuId, menuItem.ParentId, resourcePath, menuMap)
  292. }
  293. return resourcePath
  294. }
  295. func TraceTreeNode(menuId int, node []*models.MenuNode) (menuIds []int) {
  296. for i := 0; i < len(node); i++ {
  297. if node[i].MenuId == menuId {
  298. menuIds = append(menuIds, collectChildMenuIds(node[i].Children)...)
  299. }
  300. }
  301. return
  302. }
  303. // 辅助函数,用于递归收集子节点的ID
  304. func collectChildMenuIds(node []*models.MenuNode) (menuIds []int) {
  305. if node == nil || len(node) == 0 {
  306. return
  307. }
  308. for _, n := range node {
  309. menuIds = append(menuIds, n.MenuId)
  310. if len(n.Children) > 0 {
  311. menuIds = append(menuIds, collectChildMenuIds(n.Children)...)
  312. }
  313. }
  314. return
  315. }