cloud_disk.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. savePath := ossDir + time.Now().Format("200601/20060102/") + ossFileName
  563. ossClient := services.NewOssClient()
  564. if ossClient == nil {
  565. br.Msg = "上传失败"
  566. br.ErrMsg = "初始化OSS服务失败"
  567. return
  568. }
  569. resourceUrl, e = ossClient.UploadFile(ossFileName, filePath, savePath)
  570. if e != nil {
  571. br.Msg = "文件上传失败"
  572. br.ErrMsg = "文件上传失败,Err:" + e.Error()
  573. return
  574. }
  575. // 新增云盘资源
  576. extMap := services.GetCloudDiskResourceFileTypeExtMap()
  577. resourceIcon := extMap[ext]
  578. if resourceIcon == "" {
  579. resourceIcon = models.CloudDiskResourceFileTypeIconUnknown
  580. }
  581. resourceItem := &models.CloudDiskResource{
  582. MenuId: menuId,
  583. ResourceName: fileName,
  584. ResourceSuffix: ext,
  585. ResourceUrl: resourceUrl,
  586. ResourceIcon: resourceIcon,
  587. AdminId: this.SysUser.AdminId,
  588. AdminName: this.SysUser.RealName,
  589. Size: h.Size,
  590. CreateTime: time.Now().Local(),
  591. ModifyTime: time.Now().Local(),
  592. }
  593. if e = resourceItem.Create(); e != nil {
  594. br.Msg = "上传失败"
  595. br.ErrMsg = "新增云盘资源失败, Err: " + e.Error()
  596. return
  597. }
  598. // 更新目录大小
  599. go func() {
  600. if e = services.UpdateCloudDiskMenuSize(menuId); e != nil {
  601. alarm_msg.SendAlarmMsg("更新云盘目录大小失败, Err: "+e.Error(), 3)
  602. }
  603. }()
  604. br.Msg = "上传成功"
  605. br.Ret = 200
  606. br.Success = true
  607. br.Data = resourceItem
  608. }
  609. // ResourceRename 重命名文件
  610. // @Title 重命名文件
  611. // @Description 重命名文件
  612. // @Param request body models.CloudDiskResourceRenameReq true "type json string"
  613. // @Success 200 Ret=200 操作成功
  614. // @router /resource/rename [post]
  615. func (this *CloudDiskController) ResourceRename() {
  616. br := new(models.BaseResponse).Init()
  617. defer func() {
  618. this.Data["json"] = br
  619. this.ServeJSON()
  620. }()
  621. var req models.CloudDiskResourceRenameReq
  622. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  623. br.Msg = "参数解析异常!"
  624. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  625. return
  626. }
  627. if req.ResourceId <= 0 {
  628. br.Msg = "参数有误"
  629. return
  630. }
  631. req.ResourceName = strings.TrimSpace(req.ResourceName)
  632. if req.ResourceName == "" {
  633. br.Msg = "文件名称不可为空"
  634. return
  635. }
  636. // 权限校验-仅上传人或管理员可操作
  637. resourceItem := new(models.CloudDiskResource)
  638. if e := resourceItem.GetItemById(req.ResourceId); e != nil {
  639. br.Msg = "操作失败"
  640. br.ErrMsg = "获取文件信息失败, Err: " + e.Error()
  641. return
  642. }
  643. if resourceItem.AdminId != this.SysUser.AdminId {
  644. ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
  645. if e != nil {
  646. br.Msg = "操作失败"
  647. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  648. return
  649. }
  650. if !ok {
  651. br.Msg = "无权操作"
  652. return
  653. }
  654. }
  655. // 重名校验
  656. existItem := new(models.CloudDiskResource)
  657. existCond := ` AND resource_name = ? AND menu_id = ? AND resource_id <> ? `
  658. existPars := make([]interface{}, 0)
  659. existPars = append(existPars, req.ResourceName, resourceItem.MenuId, resourceItem.ResourceId)
  660. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  661. br.Msg = "操作失败"
  662. br.ErrMsg = "获取重名文件失败, Err: " + e.Error()
  663. return
  664. }
  665. if existItem != nil && existItem.MenuId > 0 {
  666. br.Msg = "该文件名已存在"
  667. return
  668. }
  669. resourceItem.ResourceName = req.ResourceName
  670. resourceItem.ModifyTime = time.Now().Local()
  671. if e := resourceItem.Update([]string{"ResourceName", "ModifyTime"}); e != nil {
  672. br.Msg = "操作失败"
  673. br.ErrMsg = "文件重命名失败, Err: " + e.Error()
  674. return
  675. }
  676. br.Ret = 200
  677. br.Success = true
  678. br.Msg = "操作成功"
  679. }
  680. // ResourceDelete 删除文件
  681. // @Title 删除文件
  682. // @Description 删除文件
  683. // @Param request body models.CloudDiskResourceDeleteReq true "type json string"
  684. // @Success 200 Ret=200 操作成功
  685. // @router /resource/delete [post]
  686. func (this *CloudDiskController) ResourceDelete() {
  687. br := new(models.BaseResponse).Init()
  688. defer func() {
  689. this.Data["json"] = br
  690. this.ServeJSON()
  691. }()
  692. var req models.CloudDiskResourceDeleteReq
  693. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  694. br.Msg = "参数解析异常!"
  695. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  696. return
  697. }
  698. if req.ResourceId <= 0 {
  699. br.Msg = "参数有误"
  700. return
  701. }
  702. // 权限校验-仅上传人或管理员可操作
  703. resourceItem := new(models.CloudDiskResource)
  704. if e := resourceItem.GetItemById(req.ResourceId); e != nil {
  705. br.Msg = "操作失败"
  706. br.ErrMsg = "获取文件信息失败, Err: " + e.Error()
  707. return
  708. }
  709. if resourceItem.AdminId != this.SysUser.AdminId {
  710. ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
  711. if e != nil {
  712. br.Msg = "操作失败"
  713. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  714. return
  715. }
  716. if !ok {
  717. br.Msg = "无权操作"
  718. return
  719. }
  720. }
  721. if e := resourceItem.Delete(); e != nil {
  722. br.Msg = "操作失败"
  723. br.ErrMsg = "删除文件失败, Err: " + e.Error()
  724. return
  725. }
  726. // 更新目录大小
  727. go func() {
  728. if e := services.UpdateCloudDiskMenuSize(resourceItem.MenuId); e != nil {
  729. alarm_msg.SendAlarmMsg("更新云盘目录大小失败, Err: "+e.Error(), 3)
  730. }
  731. }()
  732. br.Ret = 200
  733. br.Success = true
  734. br.Msg = "操作成功"
  735. }
  736. // List 云盘列表
  737. // @Title 云盘列表
  738. // @Description 云盘列表
  739. // @Param MenuId query int false "目录ID"
  740. // @Param Keyword query string false "关键词"
  741. // @Param SortType query int false "排序方式: 0-默认; 1-创建时间倒序; 2-创建时间正序"
  742. // @Success 200 Ret=200 获取成功
  743. // @router /list [get]
  744. func (this *CloudDiskController) List() {
  745. br := new(models.BaseResponse).Init()
  746. defer func() {
  747. this.Data["json"] = br
  748. this.ServeJSON()
  749. }()
  750. menuId, _ := this.GetInt("MenuId", 0)
  751. keyword := this.GetString("Keyword", "")
  752. sortType, _ := this.GetInt("SortType", 0)
  753. menuCond := ``
  754. menuPars := make([]interface{}, 0)
  755. resourceCond := ``
  756. resourcePars := make([]interface{}, 0)
  757. if keyword != "" {
  758. // 有关键词时全局搜索
  759. menuCond += ` AND menu_name LIKE ? `
  760. menuPars = append(menuPars, utils.GetLikeKeyword(keyword))
  761. resourceCond += ` AND resource_name LIKE ? `
  762. resourcePars = append(resourcePars, utils.GetLikeKeyword(keyword))
  763. } else {
  764. menuCond += ` AND parent_id = ? `
  765. menuPars = append(menuPars, menuId)
  766. resourceCond += ` AND menu_id = ? `
  767. resourcePars = append(resourcePars, menuId)
  768. }
  769. orderRule := `create_time DESC`
  770. if sortType == 2 {
  771. orderRule = `create_time ASC`
  772. }
  773. // 父级目录
  774. parentItem := new(models.CloudDiskMenu)
  775. parentCond := ``
  776. parentPars := make([]interface{}, 0)
  777. parentList, e := parentItem.GetItemsByCondition(parentCond, parentPars, []string{}, "")
  778. if e != nil {
  779. br.Msg = "获取失败"
  780. br.ErrMsg = "获取父级目录列表失败, Err: " + e.Error()
  781. return
  782. }
  783. parentNameMap := make(map[int]string)
  784. menuMap := make(map[int]*models.CloudDiskMenu)
  785. for i := range parentList {
  786. parentNameMap[parentList[i].MenuId] = parentList[i].MenuName
  787. menuMap[parentList[i].MenuId] = parentList[i]
  788. }
  789. list, e := models.GetCloudDiskMenuAndResourceList(menuCond, resourceCond, menuPars, resourcePars, orderRule)
  790. if e != nil {
  791. br.Msg = "获取失败"
  792. br.ErrMsg = "获取云盘列表数据失败, Err: " + e.Error()
  793. return
  794. }
  795. // 管理员权限
  796. ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
  797. if e != nil {
  798. br.Msg = "操作失败"
  799. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  800. return
  801. }
  802. listResp := new(models.CloudDiskListResp)
  803. listResp.UploadResourceAuth = true
  804. if ok {
  805. listResp.CreateMenuAuth = true
  806. listResp.BatchDelAuth = true
  807. }
  808. menuCount := 0
  809. for i := range list {
  810. list[i].ParentName = parentNameMap[list[i].ParentId]
  811. if list[i].ItemType == 1 {
  812. menuCount += 1
  813. list[i].ItemIcon = models.CloudDiskResourceFileTypeIconMenu
  814. }
  815. list[i].SizeName = utils.FormatFileSize(list[i].Size)
  816. // 按钮权限
  817. au := new(models.CloudDiskListButton)
  818. if ok || (list[i].AdminId == this.SysUser.AdminId) {
  819. au.RenameAuth = true
  820. au.DelAuth = true
  821. }
  822. list[i].ButtonAuth = au
  823. }
  824. // 根目录无子目录时, 关闭上传文件按钮
  825. if menuId == 0 && menuCount == 0 {
  826. listResp.UploadResourceAuth = false
  827. }
  828. // 面包屑
  829. pathArr := make([]*models.CloudDiskMenuResourcePath, 0)
  830. pathArr = services.GetCloudDiskMenuOrResourcePath(menuId, 0, menuMap, menuMap[menuId], pathArr)
  831. listResp.ListPath = pathArr
  832. listResp.List = list
  833. br.Ret = 200
  834. br.Success = true
  835. br.Msg = "获取成功"
  836. br.Data = listResp
  837. }
  838. // BatchDelete 批量删除
  839. // @Title 批量删除
  840. // @Description 批量删除
  841. // @Param request body models.CloudDiskBatchDeleteReq true "type json string"
  842. // @Success 200 Ret=200 操作成功
  843. // @router /batch_delete [post]
  844. func (this *CloudDiskController) BatchDelete() {
  845. br := new(models.BaseResponse).Init()
  846. defer func() {
  847. this.Data["json"] = br
  848. this.ServeJSON()
  849. }()
  850. var req models.CloudDiskBatchDeleteReq
  851. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  852. br.Msg = "参数解析异常!"
  853. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  854. return
  855. }
  856. if req.MenuIds == "" && req.ResourceIds == "" {
  857. br.Msg = "请选择目录或文件"
  858. return
  859. }
  860. menuIds := make([]int, 0)
  861. resourceIds := make([]int, 0)
  862. if req.MenuIds != "" {
  863. menuIdArr := strings.Split(req.MenuIds, ",")
  864. for i := range menuIdArr {
  865. n, e := strconv.Atoi(menuIdArr[i])
  866. if e != nil {
  867. br.Msg = "参数有误"
  868. br.ErrMsg = "参数有误, Err:" + e.Error()
  869. return
  870. }
  871. menuIds = append(menuIds, n)
  872. }
  873. }
  874. if req.ResourceIds != "" {
  875. resourceIdArr := strings.Split(req.ResourceIds, ",")
  876. for i := range resourceIdArr {
  877. n, e := strconv.Atoi(resourceIdArr[i])
  878. if e != nil {
  879. br.Msg = "参数有误"
  880. br.ErrMsg = "参数有误, Err:" + e.Error()
  881. return
  882. }
  883. resourceIds = append(resourceIds, n)
  884. }
  885. }
  886. // 权限校验-仅管理员可操作
  887. ok, e := services.CheckCloudDiskManagerAuth(this.SysUser.RoleTypeCode)
  888. if e != nil {
  889. br.Msg = "操作失败"
  890. br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
  891. return
  892. }
  893. if !ok {
  894. br.Msg = "无权操作"
  895. return
  896. }
  897. if e = models.BatchDeleteCloudDiskMenuAndResource(menuIds, resourceIds); e != nil {
  898. br.Msg = "操作失败"
  899. br.ErrMsg = "批量删除目录及文件失败, Err:" + e.Error()
  900. return
  901. }
  902. // 更新目录大小
  903. if req.MenuId > 0 {
  904. go func() {
  905. if e := services.UpdateCloudDiskMenuSize(req.MenuId); e != nil {
  906. alarm_msg.SendAlarmMsg("更新云盘目录大小失败, Err: "+e.Error(), 3)
  907. }
  908. }()
  909. }
  910. br.Ret = 200
  911. br.Success = true
  912. br.Msg = "操作成功"
  913. }