archive.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package http
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "strconv"
  6. "time"
  7. "go-common/app/service/main/videoup/model/archive"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/sync/errgroup"
  12. "go-common/library/xstr"
  13. )
  14. func simpleArchive(c *bm.Context) {
  15. params := c.Request.Form
  16. aidStr := params.Get("aid")
  17. modeStr := params.Get("mode")
  18. // check params
  19. mode, _ := strconv.Atoi(modeStr)
  20. if mode <= 0 || mode > 2 {
  21. mode = 0 // 0 novideo 1 open video 2 all video
  22. }
  23. aid, err := strconv.ParseInt(aidStr, 10, 64)
  24. if err != nil {
  25. log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
  26. c.JSON(nil, ecode.RequestErr)
  27. return
  28. }
  29. av, err := vdpSvc.SimpleArchive(c, aid, mode)
  30. if err != nil {
  31. log.Error(" vdpSvc.SimpleArchive(%d) error(%v)", aid, err)
  32. c.JSON(nil, err)
  33. return
  34. }
  35. c.JSON(av, nil)
  36. }
  37. func simpleVideos(c *bm.Context) {
  38. params := c.Request.Form
  39. aidStr := params.Get("aid")
  40. // check params
  41. aid, err := strconv.ParseInt(aidStr, 10, 64)
  42. if err != nil {
  43. log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
  44. c.JSON(nil, ecode.RequestErr)
  45. return
  46. }
  47. vs, err := vdpSvc.SimpleVideos(c, aid)
  48. if err != nil {
  49. log.Error(" vdpSvc.SimpleVideos(%d) error(%v)", aid, err)
  50. c.JSON(nil, ecode.RequestErr)
  51. return
  52. }
  53. c.JSON(vs, nil)
  54. }
  55. // viewArchive get archive info.
  56. func viewArchive(c *bm.Context) {
  57. params := c.Request.Form
  58. aidStr := params.Get("aid")
  59. withPOIStr := params.Get("need_poi")
  60. withVoteStr := params.Get("need_vote")
  61. // check params
  62. aid, err := strconv.ParseInt(aidStr, 10, 64)
  63. if err != nil {
  64. log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
  65. c.JSON(nil, ecode.RequestErr)
  66. return
  67. }
  68. av, err := vdpSvc.Archive(c, aid)
  69. if err != nil {
  70. log.Error(" vdpSvc.Archive(%d) error(%v)", aid, err)
  71. c.JSON(nil, ecode.RequestErr)
  72. return
  73. }
  74. //详情页展示LBS
  75. var poi *archive.PoiObj
  76. eg, errCtx := errgroup.WithContext(c)
  77. if withPOIStr == "1" {
  78. eg.Go(func() (err error) {
  79. poi, _ = vdpSvc.ArchivePOI(errCtx, aid)
  80. return nil
  81. })
  82. }
  83. var vote *archive.Vote
  84. if withVoteStr == "1" {
  85. eg.Go(func() (err error) {
  86. vote, _ = vdpSvc.ArchiveVote(errCtx, aid)
  87. return nil
  88. })
  89. }
  90. eg.Wait()
  91. if poi != nil {
  92. av.Archive.POI = poi
  93. }
  94. if vote != nil {
  95. av.Archive.Vote = vote
  96. }
  97. //详情页展示staff
  98. var staffs []*archive.StaffApply
  99. if staffs, err = vdpSvc.ApplysByAID(c, aid); err != nil || staffs == nil || len(staffs) == 0 {
  100. log.Error(" vdpSvc.ApplysByAID(%d) error(%v)", aid, err)
  101. } else {
  102. av.Archive.Staffs = staffs
  103. }
  104. c.JSON(av, nil)
  105. }
  106. // viewArchives get archive info.
  107. func viewArchives(c *bm.Context) {
  108. params := c.Request.Form
  109. aidsStr := params.Get("aids")
  110. // check params
  111. aids, err := xstr.SplitInts(aidsStr)
  112. if err != nil {
  113. log.Error("strconv.ParseInt(%s) error(%v)", aidsStr, err)
  114. c.JSON(nil, ecode.RequestErr)
  115. return
  116. }
  117. if len(aids) > 50 {
  118. log.Error("viewArchives aids(%s) too long, appkey(%s)", aidsStr, params.Get("appkey"))
  119. c.JSON(nil, ecode.RequestErr)
  120. return
  121. }
  122. avm, err := vdpSvc.Archives(c, aids)
  123. if err != nil {
  124. log.Error(" vdpSvc.Archive(%d) error(%v)", aids, err)
  125. c.JSON(nil, ecode.RequestErr)
  126. return
  127. }
  128. c.JSON(avm, nil)
  129. }
  130. // archivesByMid get archive list by mid.
  131. func archivesByMid(c *bm.Context) {
  132. params := c.Request.Form
  133. // check params
  134. midStr := params.Get("mid")
  135. pnStr := params.Get("pn")
  136. psStr := params.Get("ps")
  137. groupStr := params.Get("group")
  138. mid, _ := strconv.ParseInt(midStr, 10, 64)
  139. if mid <= 0 {
  140. log.Error("http.archivesByMid mid(%d) <=0 ", mid)
  141. c.JSON(nil, ecode.RequestErr)
  142. return
  143. }
  144. pn, _ := strconv.Atoi(pnStr)
  145. if pn <= 0 {
  146. pn = 1
  147. }
  148. ps, _ := strconv.Atoi(psStr)
  149. if ps <= 0 || ps > 100 {
  150. ps = 10
  151. }
  152. group, _ := strconv.Atoi(groupStr)
  153. gp := int8(group)
  154. if gp < 0 || gp > 2 {
  155. gp = 0
  156. }
  157. uav, err := vdpSvc.UpArchives(c, mid, pn, ps, gp)
  158. if err != nil {
  159. log.Error(" vdpSvc.Archive(%d,%d,%d,%d) error(%v)", mid, pn, ps, gp, err)
  160. c.JSON(nil, ecode.RequestErr)
  161. return
  162. }
  163. c.JSON(uav, nil)
  164. }
  165. // upArchiveTag add archive.
  166. func upArchiveTag(c *bm.Context) {
  167. params := c.Request.Form
  168. aidStr := params.Get("aid")
  169. tag := params.Get("tag")
  170. // check params
  171. aid, err := strconv.ParseInt(aidStr, 10, 64)
  172. if err != nil {
  173. log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
  174. return
  175. }
  176. c.JSON(nil, vdpSvc.UpTag(c, aid, tag))
  177. }
  178. // delArchive del archive.
  179. func delArchive(c *bm.Context) {
  180. req := c.Request
  181. bs, err := ioutil.ReadAll(req.Body)
  182. if err != nil {
  183. log.Error("ioutil.ReadAll() error(%v)", err)
  184. c.JSON(nil, ecode.RequestErr)
  185. return
  186. }
  187. req.Body.Close()
  188. // params
  189. var ap = &archive.ArcParam{}
  190. if err = json.Unmarshal(bs, ap); err != nil {
  191. log.Error("http delArchive() json.Unmarshal(%s) error(%v)", string(bs), err)
  192. c.JSON(nil, ecode.RequestErr)
  193. return
  194. }
  195. if ap.Aid == 0 {
  196. c.JSON(nil, ecode.RequestErr)
  197. return
  198. }
  199. c.JSON(nil, vdpSvc.DelArchive(c, ap.Aid, ap.Mid))
  200. }
  201. // arcHistory get archive edit history.
  202. func arcHistory(c *bm.Context) {
  203. params := c.Request.Form
  204. hidStr := params.Get("hid")
  205. // check params
  206. hid, err := strconv.ParseInt(hidStr, 10, 64)
  207. if err != nil {
  208. log.Error("strconv.ParseInt(hid(%s)) error(%v)", hidStr, err)
  209. c.JSON(nil, ecode.RequestErr)
  210. return
  211. }
  212. c.JSON(vdpSvc.ArcHistory(c, hid), nil)
  213. }
  214. // arcHistorys get archive edit history.
  215. func arcHistorys(c *bm.Context) {
  216. params := c.Request.Form
  217. aidStr := params.Get("aid")
  218. // check params
  219. aid, err := strconv.ParseInt(aidStr, 10, 64)
  220. if err != nil {
  221. log.Error("strconv.ParseInt(aid(%s)) error(%v)", aidStr, err)
  222. c.JSON(nil, ecode.RequestErr)
  223. return
  224. }
  225. c.JSON(vdpSvc.ArcHistorys(c, aid), nil)
  226. }
  227. // types get all types info
  228. func types(c *bm.Context) {
  229. c.JSON(vdpSvc.Types(c), nil)
  230. }
  231. // videoBycid get video bid cid
  232. func videoBycid(c *bm.Context) {
  233. params := c.Request.Form
  234. cidStr := params.Get("cid")
  235. // check params
  236. cid, err := strconv.ParseInt(cidStr, 10, 64)
  237. if err != nil {
  238. log.Error("strconv.ParseInt(cid(%s)) error(%v)", cidStr, err)
  239. c.JSON(nil, ecode.RequestErr)
  240. return
  241. }
  242. v, err := vdpSvc.VideoBycid(c, cid)
  243. if err != nil {
  244. c.JSON(nil, err)
  245. return
  246. }
  247. c.JSON(v, nil)
  248. }
  249. // archivesByCids get archives by cids
  250. func archivesByCids(c *bm.Context) {
  251. params := c.Request.Form
  252. cidsStr := params.Get("cids")
  253. appkey := params.Get("appkey")
  254. // check params
  255. if appkey != config.DmVerifyKey {
  256. log.Warn("appkey(%s) invalid", appkey)
  257. c.JSON(nil, ecode.AppKeyInvalid)
  258. return
  259. }
  260. cids, err := xstr.SplitInts(cidsStr)
  261. if err != nil {
  262. log.Error("strconv.ParseInt(%s) error(%v)", cidsStr, err)
  263. c.JSON(nil, ecode.RequestErr)
  264. return
  265. }
  266. if len(cids) > 100 {
  267. log.Error("cids(%d) number gt 100", len(cids))
  268. c.JSON(nil, ecode.RequestErr)
  269. return
  270. }
  271. c.JSON(vdpSvc.ArchivesByCids(c, cids), nil)
  272. }
  273. // flows get flow list
  274. func flows(c *bm.Context) {
  275. c.JSON(vdpSvc.Flows(c), nil)
  276. }
  277. // flows get specialUps list
  278. func specialUps(c *bm.Context) {
  279. params := c.Request.Form
  280. groupStr := params.Get("group_id")
  281. var err error
  282. // check params
  283. //default all groups
  284. groupID := int64(0)
  285. if groupStr != "" {
  286. groupID, err = strconv.ParseInt(groupStr, 10, 64)
  287. if err != nil {
  288. log.Error("strconv.ParseInt(groupStr(%s)) error(%v)", groupStr, err)
  289. c.JSON(nil, ecode.RequestErr)
  290. return
  291. }
  292. }
  293. c.JSON(vdpSvc.UpsByGroup(c, groupID), nil)
  294. }
  295. // arcReasonTag .
  296. func arcReasonTag(c *bm.Context) {
  297. params := c.Request.Form
  298. aidStr := params.Get("aid")
  299. var (
  300. aid, tagID int64
  301. err error
  302. )
  303. aid, err = strconv.ParseInt(aidStr, 10, 64)
  304. if err != nil {
  305. log.Error("strconv.ParseInt(aidStr(%s)) error(%v)", aidStr, err)
  306. c.JSON(nil, ecode.RequestErr)
  307. return
  308. }
  309. tagID, err = vdpSvc.ArcTag(c, aid)
  310. if err != nil {
  311. log.Error("vdpSvc.ArcTag error(%v)", err)
  312. c.JSON(nil, ecode.RequestErr)
  313. return
  314. }
  315. c.JSON(map[string]interface{}{
  316. "tag_id": tagID,
  317. }, nil)
  318. }
  319. //flowForbid
  320. func flowForbid(c *bm.Context) {
  321. c.JSON(vdpSvc.UpsForbid(c), nil)
  322. }
  323. func appFeedAids(c *bm.Context) {
  324. aids, err := vdpSvc.AppFeedAids(c)
  325. if err != nil {
  326. log.Error("vdpSvc.AppFeedAids() error(%v)", err)
  327. c.JSON(nil, err)
  328. return
  329. }
  330. c.JSON(aids, nil)
  331. }
  332. func descFormats(c *bm.Context) {
  333. dfs, err := vdpSvc.DescFormats(c)
  334. if err != nil {
  335. log.Error("vdpSvc.DescFormats() error(%v)", err)
  336. c.JSON(nil, err)
  337. return
  338. }
  339. c.JSON(dfs, nil)
  340. }
  341. // videoJam video check traffic jam time
  342. func videoJam(c *bm.Context) {
  343. level, _ := vdpSvc.VideoJamLevel(c)
  344. c.JSON(map[string]interface{}{
  345. "level": level,
  346. }, nil)
  347. }
  348. // archiveAddit get archive addit
  349. func archiveAddit(c *bm.Context) {
  350. var (
  351. err error
  352. addit *archive.Addit
  353. )
  354. params := c.Request.Form
  355. aidStr := params.Get("aid")
  356. aid, err := strconv.ParseInt(aidStr, 10, 64)
  357. if err != nil {
  358. log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
  359. c.JSON(nil, ecode.RequestErr)
  360. return
  361. }
  362. if addit, err = vdpSvc.ArchiveAddit(c, aid); err != nil {
  363. log.Error("vdpSvc.archiveAddit(%d) error(%v)", aid, err)
  364. c.JSON(nil, err)
  365. return
  366. }
  367. if addit == nil {
  368. err = ecode.NothingFound
  369. c.JSON(nil, err)
  370. return
  371. }
  372. c.JSON(addit, nil)
  373. }
  374. func rejectedArchives(c *bm.Context) {
  375. params := c.Request.Form
  376. midStr := params.Get("mid")
  377. pnStr := params.Get("pn")
  378. psStr := params.Get("ps")
  379. stateStr := params.Get("state")
  380. startStr := params.Get("start")
  381. state, _ := strconv.Atoi(stateStr)
  382. if state >= 0 {
  383. c.JSON(nil, ecode.RequestErr)
  384. return
  385. }
  386. start, err := getTimeFromSecStr(startStr)
  387. if err != nil || start == nil {
  388. c.JSON(nil, ecode.RequestErr)
  389. return
  390. }
  391. mid, _ := strconv.ParseInt(midStr, 10, 64)
  392. pn, _ := strconv.Atoi(pnStr)
  393. ps, _ := strconv.Atoi(psStr)
  394. if pn < 1 {
  395. pn = 1
  396. }
  397. if ps < 1 {
  398. ps = 20
  399. }
  400. arcs, count, err := vdpSvc.RejectedArchives(c, mid, int32(state), int32(pn), int32(ps), start)
  401. if err != nil {
  402. c.JSON(nil, err)
  403. return
  404. }
  405. data := map[string]interface{}{
  406. "page": map[string]int{
  407. "num": pn,
  408. "size": ps,
  409. "total": int(count),
  410. },
  411. "archives": arcs,
  412. }
  413. c.JSON(data, nil)
  414. }
  415. func getTimeFromSecStr(secStr string) (t *time.Time, err error) {
  416. sec, err := strconv.ParseInt(secStr, 10, 64)
  417. if err != nil || sec <= 0 {
  418. return
  419. }
  420. ti := time.Unix(sec, 0)
  421. t = &ti
  422. return
  423. }