cloud_disk.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. package controllers
  2. import (
  3. "archive/zip"
  4. "encoding/json"
  5. "eta/eta_api/models"
  6. "eta/eta_api/services"
  7. "eta/eta_api/services/alarm_msg"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/http"
  11. "os"
  12. "path"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // CloudDiskController 共享云盘
  18. type CloudDiskController struct {
  19. BaseAuthController
  20. }
  21. // MenuCreate 创建文件夹
  22. // @Title 创建文件夹
  23. // @Description 创建文件夹
  24. // @Param request body models.CloudDiskMenuCreateReq true "type json string"
  25. // @Success 200 Ret=200 操作成功
  26. // @router /menu/create [post]
  27. func (this *CloudDiskController) MenuCreate() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. var req models.CloudDiskMenuCreateReq
  34. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  35. br.Msg = "参数解析异常!"
  36. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  37. return
  38. }
  39. req.MenuName = strings.TrimSpace(req.MenuName)
  40. if req.MenuName == "" {
  41. br.Msg = "文件夹名称不可为空"
  42. return
  43. }
  44. // 权限校验
  45. ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
  46. if e != nil {
  47. br.Msg = "操作失败"
  48. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  49. return
  50. }
  51. if !ok {
  52. br.Msg = "无权操作"
  53. return
  54. }
  55. // 重名校验
  56. existItem := new(models.CloudDiskMenu)
  57. existCond := ` AND menu_name = ? AND parent_id = ? `
  58. existPars := make([]interface{}, 0)
  59. existPars = append(existPars, req.MenuName, req.ParentId)
  60. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  61. br.Msg = "操作失败"
  62. br.ErrMsg = "获取重名目录失败, Err: " + e.Error()
  63. return
  64. }
  65. if existItem != nil && existItem.MenuId > 0 {
  66. br.Msg = "该文件夹已存在"
  67. return
  68. }
  69. nowTime := time.Now().Local()
  70. newMenu := &models.CloudDiskMenu{
  71. MenuName: req.MenuName,
  72. ParentId: req.ParentId,
  73. AdminId: this.SysUser.AdminId,
  74. AdminName: this.SysUser.RealName,
  75. CreateTime: nowTime,
  76. ModifyTime: nowTime,
  77. }
  78. if e := newMenu.Create(); e != nil {
  79. br.Msg = "操作失败"
  80. br.ErrMsg = "新增目录失败, Err: " + e.Error()
  81. return
  82. }
  83. br.Ret = 200
  84. br.Success = true
  85. br.Msg = "操作成功"
  86. }
  87. // MenuRename 重命名文件夹
  88. // @Title 重命名文件夹
  89. // @Description 重命名文件夹
  90. // @Param request body models.CloudDiskMenuRenameReq true "type json string"
  91. // @Success 200 Ret=200 操作成功
  92. // @router /menu/rename [post]
  93. func (this *CloudDiskController) MenuRename() {
  94. br := new(models.BaseResponse).Init()
  95. defer func() {
  96. this.Data["json"] = br
  97. this.ServeJSON()
  98. }()
  99. var req models.CloudDiskMenuRenameReq
  100. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  101. br.Msg = "参数解析异常!"
  102. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  103. return
  104. }
  105. if req.MenuId <= 0 {
  106. br.Msg = "参数有误"
  107. return
  108. }
  109. req.MenuName = strings.TrimSpace(req.MenuName)
  110. if req.MenuName == "" {
  111. br.Msg = "文件夹名称不可为空"
  112. return
  113. }
  114. // 权限校验
  115. ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
  116. if e != nil {
  117. br.Msg = "操作失败"
  118. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  119. return
  120. }
  121. if !ok {
  122. br.Msg = "无权操作"
  123. return
  124. }
  125. // 重名校验
  126. menuItem := new(models.CloudDiskMenu)
  127. if e := menuItem.GetItemById(req.MenuId); e != nil {
  128. br.Msg = "操作失败"
  129. br.ErrMsg = "获取目录信息失败, Err: " + e.Error()
  130. return
  131. }
  132. existItem := new(models.CloudDiskMenu)
  133. existCond := ` AND menu_name = ? AND parent_id = ? AND menu_id <> ? `
  134. existPars := make([]interface{}, 0)
  135. existPars = append(existPars, req.MenuName, menuItem.ParentId, menuItem.MenuId)
  136. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  137. br.Msg = "操作失败"
  138. br.ErrMsg = "获取重名目录失败, Err: " + e.Error()
  139. return
  140. }
  141. if existItem != nil && existItem.MenuId > 0 {
  142. br.Msg = "该文件夹已存在"
  143. return
  144. }
  145. menuItem.MenuName = req.MenuName
  146. menuItem.ModifyTime = time.Now().Local()
  147. if e := menuItem.Update([]string{"MenuName", "ModifyTime"}); e != nil {
  148. br.Msg = "操作失败"
  149. br.ErrMsg = "目录重命名失败, Err: " + e.Error()
  150. return
  151. }
  152. br.Ret = 200
  153. br.Success = true
  154. br.Msg = "操作成功"
  155. }
  156. // MenuDelete 删除文件夹
  157. // @Title 删除文件夹
  158. // @Description 删除文件夹
  159. // @Param request body models.CloudDiskMenuDeleteReq true "type json string"
  160. // @Success 200 Ret=200 操作成功
  161. // @router /menu/delete [post]
  162. func (this *CloudDiskController) MenuDelete() {
  163. br := new(models.BaseResponse).Init()
  164. defer func() {
  165. this.Data["json"] = br
  166. this.ServeJSON()
  167. }()
  168. var req models.CloudDiskMenuDeleteReq
  169. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  170. br.Msg = "参数解析异常!"
  171. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  172. return
  173. }
  174. if req.MenuId <= 0 {
  175. br.Msg = "参数有误"
  176. return
  177. }
  178. // 权限校验
  179. ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
  180. if e != nil {
  181. br.Msg = "操作失败"
  182. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  183. return
  184. }
  185. if !ok {
  186. br.Msg = "无权操作"
  187. return
  188. }
  189. // 获取文件夹子目录IDs
  190. menuIds := make([]int, 0)
  191. menuIds = append(menuIds, req.MenuId)
  192. childIds, e := services.GetCloudDiskMenuIdsByParentId(req.MenuId)
  193. if e != nil {
  194. br.Msg = "操作失败"
  195. br.ErrMsg = "获取子目录IDs失败, ErrMsg: " + e.Error()
  196. return
  197. }
  198. menuIds = append(menuIds, childIds...)
  199. // 删除目录及目录下所有文件
  200. if e = models.DeleteCloudDiskMenuAndResourcesByMenuIds(menuIds); e != nil {
  201. br.Msg = "操作失败"
  202. br.ErrMsg = "删除目录及文件失败, Err: " + e.Error()
  203. return
  204. }
  205. br.Ret = 200
  206. br.Success = true
  207. br.Msg = "操作成功"
  208. }
  209. // MenuTree 目录分类树
  210. // @Title 目录分类树
  211. // @Description 目录分类树
  212. // @Success 200 Ret=200 获取成功
  213. // @router /menu/tree [get]
  214. func (this *CloudDiskController) MenuTree() {
  215. br := new(models.BaseResponse).Init()
  216. defer func() {
  217. this.Data["json"] = br
  218. this.ServeJSON()
  219. }()
  220. treeData, e := services.GetCloudDiskMenuTree()
  221. if e != nil {
  222. br.Msg = "获取失败"
  223. br.ErrMsg = "获取目录分类树失败, Err: " + e.Error()
  224. return
  225. }
  226. br.Ret = 200
  227. br.Success = true
  228. br.Msg = "获取成功"
  229. br.Data = treeData
  230. }
  231. // MenuResources 目录文件
  232. // @Title 目录文件
  233. // @Description 获取目录下所有文件
  234. // @Param MenuId query int true "目录ID"
  235. // @Success 200 Ret=200 获取成功
  236. // @router /menu/resources [get]
  237. func (this *CloudDiskController) MenuResources() {
  238. br := new(models.BaseResponse).Init()
  239. defer func() {
  240. this.Data["json"] = br
  241. this.ServeJSON()
  242. }()
  243. menuId, _ := this.GetInt("MenuId", 0)
  244. if menuId <= 0 {
  245. br.Msg = "参数有误"
  246. return
  247. }
  248. menuItem := new(models.CloudDiskMenu)
  249. if e := menuItem.GetItemById(menuId); e != nil {
  250. br.Msg = "目录已被删除, 请刷新页面"
  251. br.ErrMsg = "获取目录信息失败, Err: " + e.Error()
  252. return
  253. }
  254. // 获取该目录子目录IDs
  255. menuIds := make([]int, 0)
  256. menuIds = append(menuIds, menuId)
  257. childIds, e := services.GetCloudDiskMenuIdsByParentId(menuId)
  258. if e != nil {
  259. br.Msg = "操作失败"
  260. br.ErrMsg = "获取子目录IDs失败, ErrMsg: " + e.Error()
  261. return
  262. }
  263. menuIds = append(menuIds, childIds...)
  264. // 获取目录下所有文件
  265. resourceOB := new(models.CloudDiskResource)
  266. resourceCond := ` AND menu_id IN (` + utils.GetOrmInReplace(len(menuIds)) + `) `
  267. resourcePars := make([]interface{}, 0)
  268. resourcePars = append(resourcePars, menuIds)
  269. resourceFieldArr := []string{"resource_id", "resource_name", "resource_suffix", "resource_url", "menu_id"}
  270. resourceList, e := resourceOB.GetItemsByCondition(resourceCond, resourcePars, resourceFieldArr, "create_time ASC")
  271. if e != nil {
  272. br.Msg = "操作失败"
  273. br.ErrMsg = "获取目录下所有文件失败, Err: " + e.Error()
  274. return
  275. }
  276. // 获取目录列表, 取出parentMap
  277. listCond := ``
  278. listPars := make([]interface{}, 0)
  279. menuList, e := menuItem.GetItemsByCondition(listCond, listPars, []string{}, "")
  280. if e != nil {
  281. br.Msg = "操作失败"
  282. br.ErrMsg = "获取目录列表失败, Err: " + e.Error()
  283. return
  284. }
  285. menuMap := make(map[int]*models.CloudDiskMenu)
  286. for i := range menuList {
  287. menuMap[menuList[i].MenuId] = menuList[i]
  288. }
  289. type MenuResource struct {
  290. ResourceId int
  291. ResourceName string
  292. ResourceSuffix string
  293. ResourceUrl string
  294. OriginName string
  295. }
  296. respList := make([]*MenuResource, 0)
  297. for i := range resourceList {
  298. n := resourceList[i].ResourceName + resourceList[i].ResourceSuffix
  299. p := services.FillMenuPath2File(resourceList[i].MenuId, resourceList[i].MenuId, n, menuMap) // 完整的文件路径
  300. respList = append(respList, &MenuResource{
  301. ResourceId: resourceList[i].ResourceId,
  302. ResourceName: p,
  303. ResourceSuffix: resourceList[i].ResourceSuffix,
  304. ResourceUrl: resourceList[i].ResourceUrl,
  305. OriginName: n,
  306. })
  307. }
  308. br.Ret = 200
  309. br.Success = true
  310. br.Msg = "获取成功"
  311. br.Data = respList
  312. }
  313. // MenuDownload 目录文件打包下载
  314. // @Title 目录文件打包下载
  315. // @Description 目录文件打包下载
  316. // @Param MenuId query int true "目录ID"
  317. // @Success 200 Ret=200 操作成功
  318. // @router /menu/download [get]
  319. func (this *CloudDiskController) MenuDownload() {
  320. br := new(models.BaseResponse).Init()
  321. defer func() {
  322. this.Data["json"] = br
  323. this.ServeJSON()
  324. }()
  325. menuId, _ := this.GetInt("MenuId", 0)
  326. if menuId <= 0 {
  327. br.Msg = "参数有误"
  328. return
  329. }
  330. menuItem := new(models.CloudDiskMenu)
  331. if e := menuItem.GetItemById(menuId); e != nil {
  332. br.Msg = "目录已被删除, 请刷新页面"
  333. br.ErrMsg = "获取目录信息失败, Err: " + e.Error()
  334. return
  335. }
  336. // 获取该目录子目录IDs
  337. menuIds := make([]int, 0)
  338. menuIds = append(menuIds, menuId)
  339. childIds, e := services.GetCloudDiskMenuIdsByParentId(menuId)
  340. if e != nil {
  341. br.Msg = "操作失败"
  342. br.ErrMsg = "获取子目录IDs失败, ErrMsg: " + e.Error()
  343. return
  344. }
  345. menuIds = append(menuIds, childIds...)
  346. // 获取目录下所有文件
  347. resourceOB := new(models.CloudDiskResource)
  348. resourceCond := ` AND menu_id IN (` + utils.GetOrmInReplace(len(menuIds)) + `) `
  349. resourcePars := make([]interface{}, 0)
  350. resourcePars = append(resourcePars, menuIds)
  351. resourceFieldArr := []string{"resource_id", "resource_name", "resource_suffix", "resource_url"}
  352. resourceList, e := resourceOB.GetItemsByCondition(resourceCond, resourcePars, resourceFieldArr, "create_time ASC")
  353. if e != nil {
  354. br.Msg = "操作失败"
  355. br.ErrMsg = "获取目录下所有文件失败, Err: " + e.Error()
  356. return
  357. }
  358. if len(resourceList) == 0 {
  359. br.Msg = "无文件可下载"
  360. return
  361. }
  362. // 创建zip
  363. zipName := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  364. savePath := zipName
  365. zipFile, err := os.Create(zipName)
  366. if err != nil {
  367. br.Msg = "操作失败"
  368. br.ErrMsg = "生成压缩文件失败, Err: " + e.Error()
  369. return
  370. }
  371. zipWriter := zip.NewWriter(zipFile)
  372. // 生成zip过程中报错关闭
  373. defer func() {
  374. if err != nil {
  375. _ = zipWriter.Close()
  376. _ = zipFile.Close()
  377. }
  378. _ = os.Remove(savePath)
  379. }()
  380. // 获取资源, 写入zip
  381. for i := range resourceList {
  382. if resourceList[i].ResourceName == "" || resourceList[i].ResourceSuffix == "" || resourceList[i].ResourceUrl == "" {
  383. continue
  384. }
  385. fmt.Printf("开始压缩第%d个文件\n", i+1)
  386. ioWriter, err := zipWriter.Create(fmt.Sprintf("%s.%s", resourceList[i].ResourceName, resourceList[i].ResourceSuffix))
  387. if err != nil {
  388. if os.IsPermission(err) {
  389. br.Msg = "操作失败"
  390. br.ErrMsg = "打包权限不足, Err: " + err.Error()
  391. return
  392. }
  393. br.Msg = "操作失败"
  394. br.ErrMsg = "压缩出错, Err: " + err.Error()
  395. return
  396. }
  397. var content []byte
  398. content, err = http.Get(resourceList[i].ResourceUrl)
  399. if err != nil {
  400. br.Msg = "操作失败"
  401. br.ErrMsg = "资源获取失败, Err: " + err.Error()
  402. return
  403. }
  404. _, err = ioWriter.Write(content)
  405. if err != nil {
  406. br.Msg = "操作失败"
  407. br.ErrMsg = "压缩文件写入失败, Err: " + err.Error()
  408. return
  409. }
  410. fmt.Printf("第%d个文件写入成功\n", i+1)
  411. }
  412. // 生成zip后关闭,否则下载文件会损坏
  413. _ = zipWriter.Close()
  414. _ = zipFile.Close()
  415. this.Ctx.Output.Download(savePath, fmt.Sprintf("%s.zip", menuItem.MenuName))
  416. br.Ret = 200
  417. br.Msg = "下载成功"
  418. br.Success = true
  419. }
  420. // CheckResourceRepeat 校验目录下文件重名
  421. // @Title 校验目录下文件重名
  422. // @Description 校验目录下文件重名
  423. // @Param request body models.CloudDiskCheckResourceRepeatReq true "type json string"
  424. // @Success 200 Ret=200 操作成功
  425. // @router /resource/check_repeat [post]
  426. func (this *CloudDiskController) CheckResourceRepeat() {
  427. br := new(models.BaseResponse).Init()
  428. defer func() {
  429. this.Data["json"] = br
  430. this.ServeJSON()
  431. }()
  432. var req models.CloudDiskCheckResourceRepeatReq
  433. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  434. br.Msg = "参数解析异常!"
  435. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  436. return
  437. }
  438. // 检测接口允许menuId为0, 上传不允许
  439. if len(req.FileNames) == 0 {
  440. br.Msg = "请选择文件"
  441. return
  442. }
  443. // 获取该级目录下的文件
  444. resourceOB := new(models.CloudDiskResource)
  445. resourceCond := ` AND menu_id = ? `
  446. resourcePars := make([]interface{}, 0)
  447. resourcePars = append(resourcePars, req.MenuId)
  448. resourceFieldArr := []string{"resource_id", "resource_name", "resource_suffix"}
  449. resourceList, e := resourceOB.GetItemsByCondition(resourceCond, resourcePars, resourceFieldArr, "create_time ASC")
  450. if e != nil {
  451. br.Msg = "操作失败"
  452. br.ErrMsg = "获取目录下文件失败, Err: " + e.Error()
  453. return
  454. }
  455. resourceLen := len(resourceList)
  456. respList := make([]*models.CloudDiskCheckResourceRepeatResp, 0)
  457. for i := range req.FileNames {
  458. v := new(models.CloudDiskCheckResourceRepeatResp)
  459. v.OriginName = req.FileNames[i]
  460. r := false
  461. a := v.OriginName
  462. if resourceLen > 0 {
  463. r, a = services.CheckFileNameFormFiles(v.OriginName, "", resourceList, 0)
  464. }
  465. v.IsRepeat = r
  466. v.AvailableName = a
  467. respList = append(respList, v)
  468. }
  469. br.Ret = 200
  470. br.Success = true
  471. br.Msg = "操作成功"
  472. br.Data = respList
  473. }
  474. // ResourceUpload 上传文件
  475. // @Title 上传文件
  476. // @Description 上传文件
  477. // @Param MenuId query int true "目录ID"
  478. // @Param File query file true "文件"
  479. // @Success 200 Ret=200 操作成功
  480. // @router /resource/upload [post]
  481. func (this *CloudDiskController) ResourceUpload() {
  482. br := new(models.BaseResponse).Init()
  483. defer func() {
  484. this.Data["json"] = br
  485. this.ServeJSON()
  486. }()
  487. menuId, _ := this.GetInt("MenuId", 0)
  488. if menuId <= 0 {
  489. br.Msg = "不允许上传文件至根目录"
  490. return
  491. }
  492. originName := this.GetString("OriginName", "")
  493. if originName == "" {
  494. br.Msg = "请上传文件名"
  495. return
  496. }
  497. if !strings.Contains(originName, ".") {
  498. br.Msg = "文件名有误"
  499. return
  500. }
  501. f, h, e := this.GetFile("file")
  502. if e != nil {
  503. br.Msg = "获取资源信息失败"
  504. br.ErrMsg = "获取资源信息失败, Err:" + e.Error()
  505. return
  506. }
  507. defer func() {
  508. _ = f.Close()
  509. }()
  510. extIndex := strings.LastIndex(originName, ".")
  511. fileName := originName[:extIndex]
  512. ext := path.Ext(h.Filename)
  513. // 重名校验
  514. existItem := new(models.CloudDiskResource)
  515. existCond := ` AND resource_name = ? AND menu_id = ? `
  516. existPars := make([]interface{}, 0)
  517. existPars = append(existPars, fileName, menuId)
  518. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  519. br.Msg = "操作失败"
  520. br.ErrMsg = "获取重名文件失败, Err: " + e.Error()
  521. return
  522. }
  523. if existItem != nil && existItem.MenuId > 0 {
  524. br.Msg = fmt.Sprintf("文件名%s已存在", fileName)
  525. return
  526. }
  527. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  528. if e = os.MkdirAll(uploadDir, utils.DIR_MOD); e != nil {
  529. br.Msg = "存储目录创建失败"
  530. br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
  531. return
  532. }
  533. ossFileName := utils.GetRandStringNoSpecialChar(28) + ext
  534. filePath := uploadDir + "/" + ossFileName
  535. if e = this.SaveToFile("file", filePath); e != nil {
  536. br.Msg = "文件保存失败"
  537. br.ErrMsg = "文件保存失败, Err:" + e.Error()
  538. return
  539. }
  540. defer func() {
  541. _ = os.Remove(filePath)
  542. }()
  543. // 上传到阿里云
  544. ossDir := "static/cloud_disk/"
  545. resourceUrl := ``
  546. //上传到阿里云 和 minio
  547. if utils.ObjectStorageClient == "minio" {
  548. resourceUrl, e = services.UploadMinIoToDir(ossFileName, filePath, ossDir, "")
  549. if e != nil {
  550. br.Msg = "文件上传失败"
  551. br.ErrMsg = "文件上传失败, Err:" + e.Error()
  552. return
  553. }
  554. } else {
  555. resourceUrl, e = services.UploadAliyunToDir(ossFileName, filePath, ossDir, "")
  556. if e != nil {
  557. br.Msg = "文件上传失败"
  558. br.ErrMsg = "文件上传失败, Err:" + e.Error()
  559. return
  560. }
  561. }
  562. // 新增云盘资源
  563. extMap := services.GetCloudDiskResourceFileTypeExtMap()
  564. resourceIcon := extMap[ext]
  565. if resourceIcon == "" {
  566. resourceIcon = models.CloudDiskResourceFileTypeIconUnknown
  567. }
  568. resourceItem := &models.CloudDiskResource{
  569. MenuId: menuId,
  570. ResourceName: fileName,
  571. ResourceSuffix: ext,
  572. ResourceUrl: resourceUrl,
  573. ResourceIcon: resourceIcon,
  574. AdminId: this.SysUser.AdminId,
  575. AdminName: this.SysUser.RealName,
  576. Size: h.Size,
  577. CreateTime: time.Now().Local(),
  578. ModifyTime: time.Now().Local(),
  579. }
  580. if e = resourceItem.Create(); e != nil {
  581. br.Msg = "上传失败"
  582. br.ErrMsg = "新增云盘资源失败, Err: " + e.Error()
  583. return
  584. }
  585. // 更新目录大小
  586. go func() {
  587. if e = services.UpdateCloudDiskMenuSize(menuId); e != nil {
  588. alarm_msg.SendAlarmMsg("更新云盘目录大小失败, Err: "+e.Error(), 3)
  589. }
  590. }()
  591. br.Msg = "上传成功"
  592. br.Ret = 200
  593. br.Success = true
  594. br.Data = resourceItem
  595. }
  596. // ResourceRename 重命名文件
  597. // @Title 重命名文件
  598. // @Description 重命名文件
  599. // @Param request body models.CloudDiskResourceRenameReq true "type json string"
  600. // @Success 200 Ret=200 操作成功
  601. // @router /resource/rename [post]
  602. func (this *CloudDiskController) ResourceRename() {
  603. br := new(models.BaseResponse).Init()
  604. defer func() {
  605. this.Data["json"] = br
  606. this.ServeJSON()
  607. }()
  608. var req models.CloudDiskResourceRenameReq
  609. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  610. br.Msg = "参数解析异常!"
  611. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  612. return
  613. }
  614. if req.ResourceId <= 0 {
  615. br.Msg = "参数有误"
  616. return
  617. }
  618. req.ResourceName = strings.TrimSpace(req.ResourceName)
  619. if req.ResourceName == "" {
  620. br.Msg = "文件名称不可为空"
  621. return
  622. }
  623. // 权限校验-仅上传人或管理员可操作
  624. resourceItem := new(models.CloudDiskResource)
  625. if e := resourceItem.GetItemById(req.ResourceId); e != nil {
  626. br.Msg = "操作失败"
  627. br.ErrMsg = "获取文件信息失败, Err: " + e.Error()
  628. return
  629. }
  630. if resourceItem.AdminId != this.SysUser.AdminId {
  631. ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
  632. if e != nil {
  633. br.Msg = "操作失败"
  634. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  635. return
  636. }
  637. if !ok {
  638. br.Msg = "无权操作"
  639. return
  640. }
  641. }
  642. // 重名校验
  643. existItem := new(models.CloudDiskResource)
  644. existCond := ` AND resource_name = ? AND menu_id = ? AND resource_id <> ? `
  645. existPars := make([]interface{}, 0)
  646. existPars = append(existPars, req.ResourceName, resourceItem.MenuId, resourceItem.ResourceId)
  647. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  648. br.Msg = "操作失败"
  649. br.ErrMsg = "获取重名文件失败, Err: " + e.Error()
  650. return
  651. }
  652. if existItem != nil && existItem.MenuId > 0 {
  653. br.Msg = "该文件名已存在"
  654. return
  655. }
  656. resourceItem.ResourceName = req.ResourceName
  657. resourceItem.ModifyTime = time.Now().Local()
  658. if e := resourceItem.Update([]string{"ResourceName", "ModifyTime"}); e != nil {
  659. br.Msg = "操作失败"
  660. br.ErrMsg = "文件重命名失败, Err: " + e.Error()
  661. return
  662. }
  663. br.Ret = 200
  664. br.Success = true
  665. br.Msg = "操作成功"
  666. }
  667. // ResourceDelete 删除文件
  668. // @Title 删除文件
  669. // @Description 删除文件
  670. // @Param request body models.CloudDiskResourceDeleteReq true "type json string"
  671. // @Success 200 Ret=200 操作成功
  672. // @router /resource/delete [post]
  673. func (this *CloudDiskController) ResourceDelete() {
  674. br := new(models.BaseResponse).Init()
  675. defer func() {
  676. this.Data["json"] = br
  677. this.ServeJSON()
  678. }()
  679. var req models.CloudDiskResourceDeleteReq
  680. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  681. br.Msg = "参数解析异常!"
  682. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  683. return
  684. }
  685. if req.ResourceId <= 0 {
  686. br.Msg = "参数有误"
  687. return
  688. }
  689. // 权限校验-仅上传人或管理员可操作
  690. resourceItem := new(models.CloudDiskResource)
  691. if e := resourceItem.GetItemById(req.ResourceId); e != nil {
  692. br.Msg = "操作失败"
  693. br.ErrMsg = "获取文件信息失败, Err: " + e.Error()
  694. return
  695. }
  696. if resourceItem.AdminId != this.SysUser.AdminId {
  697. ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
  698. if e != nil {
  699. br.Msg = "操作失败"
  700. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  701. return
  702. }
  703. if !ok {
  704. br.Msg = "无权操作"
  705. return
  706. }
  707. }
  708. if e := resourceItem.Delete(); e != nil {
  709. br.Msg = "操作失败"
  710. br.ErrMsg = "删除文件失败, Err: " + e.Error()
  711. return
  712. }
  713. // 更新目录大小
  714. go func() {
  715. if e := services.UpdateCloudDiskMenuSize(resourceItem.MenuId); e != nil {
  716. alarm_msg.SendAlarmMsg("更新云盘目录大小失败, Err: "+e.Error(), 3)
  717. }
  718. }()
  719. br.Ret = 200
  720. br.Success = true
  721. br.Msg = "操作成功"
  722. }
  723. // List 云盘列表
  724. // @Title 云盘列表
  725. // @Description 云盘列表
  726. // @Param MenuId query int false "目录ID"
  727. // @Param Keyword query string false "关键词"
  728. // @Param SortType query int false "排序方式: 0-默认; 1-创建时间倒序; 2-创建时间正序"
  729. // @Success 200 Ret=200 获取成功
  730. // @router /list [get]
  731. func (this *CloudDiskController) List() {
  732. br := new(models.BaseResponse).Init()
  733. defer func() {
  734. this.Data["json"] = br
  735. this.ServeJSON()
  736. }()
  737. menuId, _ := this.GetInt("MenuId", 0)
  738. keyword := this.GetString("Keyword", "")
  739. sortType, _ := this.GetInt("SortType", 0)
  740. menuCond := ``
  741. menuPars := make([]interface{}, 0)
  742. resourceCond := ``
  743. resourcePars := make([]interface{}, 0)
  744. if keyword != "" {
  745. // 有关键词时全局搜索
  746. kw := "%" + keyword + "%"
  747. menuCond += ` AND menu_name LIKE ? `
  748. menuPars = append(menuPars, kw)
  749. resourceCond += ` AND resource_name LIKE ? `
  750. resourcePars = append(resourcePars, kw)
  751. } else {
  752. menuCond += ` AND parent_id = ? `
  753. menuPars = append(menuPars, menuId)
  754. resourceCond += ` AND menu_id = ? `
  755. resourcePars = append(resourcePars, menuId)
  756. }
  757. orderRule := `create_time DESC`
  758. if sortType == 2 {
  759. orderRule = `create_time ASC`
  760. }
  761. // 父级目录
  762. parentItem := new(models.CloudDiskMenu)
  763. parentCond := ``
  764. parentPars := make([]interface{}, 0)
  765. parentList, e := parentItem.GetItemsByCondition(parentCond, parentPars, []string{}, "")
  766. if e != nil {
  767. br.Msg = "获取失败"
  768. br.ErrMsg = "获取父级目录列表失败, Err: " + e.Error()
  769. return
  770. }
  771. parentNameMap := make(map[int]string)
  772. menuMap := make(map[int]*models.CloudDiskMenu)
  773. for i := range parentList {
  774. parentNameMap[parentList[i].MenuId] = parentList[i].MenuName
  775. menuMap[parentList[i].MenuId] = parentList[i]
  776. }
  777. list, e := models.GetCloudDiskMenuAndResourceList(menuCond, resourceCond, menuPars, resourcePars, orderRule)
  778. if e != nil {
  779. br.Msg = "获取失败"
  780. br.ErrMsg = "获取云盘列表数据失败, Err: " + e.Error()
  781. return
  782. }
  783. // 管理员权限
  784. ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
  785. if e != nil {
  786. br.Msg = "操作失败"
  787. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  788. return
  789. }
  790. listResp := new(models.CloudDiskListResp)
  791. listResp.UploadResourceAuth = true
  792. if ok {
  793. listResp.CreateMenuAuth = true
  794. listResp.BatchDelAuth = true
  795. }
  796. menuCount := 0
  797. for i := range list {
  798. list[i].ParentName = parentNameMap[list[i].ParentId]
  799. if list[i].ItemType == 1 {
  800. menuCount += 1
  801. list[i].ItemIcon = models.CloudDiskResourceFileTypeIconMenu
  802. }
  803. list[i].SizeName = utils.FormatFileSize(list[i].Size)
  804. // 按钮权限
  805. au := new(models.CloudDiskListButton)
  806. if ok || (list[i].AdminId == this.SysUser.AdminId) {
  807. au.RenameAuth = true
  808. au.DelAuth = true
  809. }
  810. list[i].ButtonAuth = au
  811. }
  812. // 根目录无子目录时, 关闭上传文件按钮
  813. if menuId == 0 && menuCount == 0 {
  814. listResp.UploadResourceAuth = false
  815. }
  816. // 面包屑
  817. pathArr := make([]*models.CloudDiskMenuResourcePath, 0)
  818. pathArr = services.GetCloudDiskMenuOrResourcePath(menuId, 0, menuMap, menuMap[menuId], pathArr)
  819. listResp.ListPath = pathArr
  820. listResp.List = list
  821. br.Ret = 200
  822. br.Success = true
  823. br.Msg = "获取成功"
  824. br.Data = listResp
  825. }
  826. // BatchDelete 批量删除
  827. // @Title 批量删除
  828. // @Description 批量删除
  829. // @Param request body models.CloudDiskBatchDeleteReq true "type json string"
  830. // @Success 200 Ret=200 操作成功
  831. // @router /batch_delete [post]
  832. func (this *CloudDiskController) BatchDelete() {
  833. br := new(models.BaseResponse).Init()
  834. defer func() {
  835. this.Data["json"] = br
  836. this.ServeJSON()
  837. }()
  838. var req models.CloudDiskBatchDeleteReq
  839. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  840. br.Msg = "参数解析异常!"
  841. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  842. return
  843. }
  844. if req.MenuIds == "" && req.ResourceIds == "" {
  845. br.Msg = "请选择目录或文件"
  846. return
  847. }
  848. menuIds := make([]int, 0)
  849. resourceIds := make([]int, 0)
  850. if req.MenuIds != "" {
  851. menuIdArr := strings.Split(req.MenuIds, ",")
  852. for i := range menuIdArr {
  853. n, e := strconv.Atoi(menuIdArr[i])
  854. if e != nil {
  855. br.Msg = "参数有误"
  856. br.ErrMsg = "参数有误, Err:" + e.Error()
  857. return
  858. }
  859. menuIds = append(menuIds, n)
  860. }
  861. }
  862. if req.ResourceIds != "" {
  863. resourceIdArr := strings.Split(req.ResourceIds, ",")
  864. for i := range resourceIdArr {
  865. n, e := strconv.Atoi(resourceIdArr[i])
  866. if e != nil {
  867. br.Msg = "参数有误"
  868. br.ErrMsg = "参数有误, Err:" + e.Error()
  869. return
  870. }
  871. resourceIds = append(resourceIds, n)
  872. }
  873. }
  874. // 权限校验-仅管理员可操作
  875. ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
  876. if e != nil {
  877. br.Msg = "操作失败"
  878. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  879. return
  880. }
  881. if !ok {
  882. br.Msg = "无权操作"
  883. return
  884. }
  885. if e = models.BatchDeleteCloudDiskMenuAndResource(menuIds, resourceIds); e != nil {
  886. br.Msg = "操作失败"
  887. br.ErrMsg = "批量删除目录及文件失败, Err:" + e.Error()
  888. return
  889. }
  890. // 更新目录大小
  891. if req.MenuId > 0 {
  892. go func() {
  893. if e := services.UpdateCloudDiskMenuSize(req.MenuId); e != nil {
  894. alarm_msg.SendAlarmMsg("更新云盘目录大小失败, Err: "+e.Error(), 3)
  895. }
  896. }()
  897. }
  898. br.Ret = 200
  899. br.Success = true
  900. br.Msg = "操作成功"
  901. }