app.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package http
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "strconv"
  7. "go-common/app/interface/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/time"
  12. )
  13. func appEdit(c *bm.Context) {
  14. var (
  15. aid int64
  16. ap = &archive.ArcParam{}
  17. cp = &archive.ArcParam{}
  18. err error
  19. )
  20. mobi := c.Request.Form.Get("mobi_app")
  21. build := c.Request.Form.Get("build")
  22. midI, _ := c.Get("mid")
  23. mid, ok := midI.(int64)
  24. if !ok || mid <= 0 {
  25. c.JSON(nil, ecode.RequestErr)
  26. return
  27. }
  28. bs, err := ioutil.ReadAll(c.Request.Body)
  29. if err != nil {
  30. c.JSON(nil, ecode.RequestErr)
  31. return
  32. }
  33. c.Request.Body.Close()
  34. if mobi == "iphone" && (build == "4470" || build == "4430") {
  35. err = iosUnmarshal(bs, ap)
  36. err = iosUnmarshal(bs, cp)
  37. } else {
  38. err = json.Unmarshal(bs, ap)
  39. err = json.Unmarshal(bs, cp)
  40. }
  41. if err != nil {
  42. c.JSON(nil, ecode.VideoupParamErr)
  43. return
  44. }
  45. aid = ap.Aid
  46. ap.Mid = mid
  47. // 老的只编辑基础信息, 不需要按照平台自动转换
  48. ap.UpFrom = archive.UpFromAPP
  49. defer func() {
  50. cp.Aid = ap.Aid
  51. cp.Mid = ap.Mid
  52. cp.UpFrom = ap.UpFrom
  53. cp.IPv6 = ap.IPv6
  54. build, buvid := getBuildInfo(c)
  55. vdpSvc.SendArchiveLog(aid, build, buvid, "edit", "app", cp, err)
  56. }()
  57. err = vdpSvc.AppEdit(c, ap, mid)
  58. if err != nil {
  59. c.JSON(nil, err)
  60. log.Error("editErr err(%+v)|ap(%+v)", err, ap)
  61. return
  62. }
  63. c.JSON(map[string]interface{}{
  64. "aid": ap.Aid,
  65. }, nil)
  66. }
  67. func iosUnmarshal(bs []byte, ap *archive.ArcParam) (err error) {
  68. var apStr = struct {
  69. Aid string `json:"aid"`
  70. Copyright string `json:"copyright"`
  71. Cover string `json:"cover"`
  72. Title string `json:"title"`
  73. TypeID string `json:"tid"`
  74. Tag string `json:"tag"`
  75. Desc string `json:"desc"`
  76. MissionID string `json:"mission_id"`
  77. OpenElec string `json:"open_elec"`
  78. DTime string `json:"dtime"`
  79. }{}
  80. if err = json.Unmarshal(bs, &apStr); err != nil {
  81. return
  82. }
  83. aid, err := strconv.ParseInt(apStr.Aid, 10, 64)
  84. if err != nil {
  85. return
  86. }
  87. copyright, err := strconv.ParseInt(apStr.Copyright, 10, 8)
  88. if err != nil {
  89. return
  90. }
  91. typeID, err := strconv.ParseInt(apStr.TypeID, 10, 16)
  92. if err != nil {
  93. return
  94. }
  95. missionID, err := strconv.ParseInt(apStr.MissionID, 10, 10)
  96. if err != nil {
  97. return
  98. }
  99. openElec, err := strconv.ParseInt(apStr.OpenElec, 10, 8)
  100. if err != nil {
  101. return
  102. }
  103. dtime, err := strconv.ParseInt(apStr.DTime, 10, 64)
  104. if err != nil {
  105. return
  106. }
  107. ap.Aid = aid
  108. ap.Copyright = int8(copyright)
  109. ap.TypeID = int16(typeID)
  110. ap.MissionID = int(missionID)
  111. ap.OpenElec = int8(openElec)
  112. ap.DTime = time.Time(dtime)
  113. ap.Cover = apStr.Cover
  114. ap.Title = apStr.Title
  115. ap.Tag = apStr.Tag
  116. ap.Desc = apStr.Desc
  117. return
  118. }
  119. func appUpCover(c *bm.Context) {
  120. midI, _ := c.Get("mid")
  121. mid, ok := midI.(int64)
  122. if !ok || mid <= 0 {
  123. c.JSON(nil, ecode.RequestErr)
  124. return
  125. }
  126. file, _, err := c.Request.FormFile("file")
  127. if err != nil {
  128. log.Error("c.Request().FormFile(\"file\") error(%v) | ", err)
  129. c.JSON(nil, ecode.RequestErr)
  130. return
  131. }
  132. bs, err := ioutil.ReadAll(file)
  133. file.Close()
  134. if err != nil {
  135. log.Error("ioutil.ReadAll(c.Request().Body) error(%v)", err)
  136. c.JSON(nil, ecode.RequestErr)
  137. return
  138. }
  139. ftype := http.DetectContentType(bs)
  140. if ftype != "image/jpeg" && ftype != "image/png" && ftype != "image/webp" {
  141. log.Error("filetype not allow file type(%s)", ftype)
  142. c.JSON(nil, ecode.RequestErr)
  143. return
  144. }
  145. url, err := vdpSvc.AppUpCover(c, ftype, bs, mid)
  146. if err != nil {
  147. c.JSON(nil, err)
  148. return
  149. }
  150. c.JSON(map[string]interface{}{
  151. "url": url,
  152. }, nil)
  153. }
  154. func appAdd(c *bm.Context) {
  155. var (
  156. aid int64
  157. ap = &archive.ArcParam{}
  158. cp = &archive.ArcParam{}
  159. err error
  160. )
  161. form := c.Request.Form
  162. ar := &archive.AppRequest{
  163. Build: form.Get("build"),
  164. MobiApp: form.Get("mobi_app"),
  165. Platform: form.Get("platform"),
  166. Device: form.Get("device"),
  167. }
  168. midI, _ := c.Get("mid")
  169. mid, ok := midI.(int64)
  170. if !ok || mid <= 0 {
  171. c.JSON(nil, ecode.RequestErr)
  172. return
  173. }
  174. bs, err := ioutil.ReadAll(c.Request.Body)
  175. if err != nil {
  176. c.JSON(nil, ecode.RequestErr)
  177. return
  178. }
  179. c.Request.Body.Close()
  180. err = json.Unmarshal(bs, ap)
  181. err = json.Unmarshal(bs, cp)
  182. if err != nil {
  183. c.JSON(nil, ecode.VideoupParamErr)
  184. return
  185. }
  186. ap.Aid = 0
  187. ap.Mid = mid
  188. ap.UpFrom = appUpFrom(ar.Platform, ar.Device)
  189. defer func() {
  190. cp.Aid = ap.Aid
  191. cp.Mid = ap.Mid
  192. cp.UpFrom = ap.UpFrom
  193. cp.IPv6 = ap.IPv6
  194. build, buvid := getBuildInfo(c)
  195. vdpSvc.SendArchiveLog(aid, build, buvid, "add", ar.Platform, cp, err)
  196. }()
  197. if err == nil {
  198. aid, err = vdpSvc.AppAdd(c, mid, ap, ar)
  199. }
  200. if err != nil {
  201. log.Error("AppAdd Err mid(%+d)|ap(%+v)|err(%+v)", mid, ap, err)
  202. c.JSON(nil, err)
  203. return
  204. }
  205. c.JSON(map[string]interface{}{
  206. "aid": aid,
  207. }, nil)
  208. }
  209. func appEditFull(c *bm.Context) {
  210. var (
  211. aid int64
  212. ap = &archive.ArcParam{}
  213. cp = &archive.ArcParam{}
  214. err error
  215. )
  216. form := c.Request.Form
  217. ar := &archive.AppRequest{
  218. Build: form.Get("build"),
  219. MobiApp: form.Get("mobi_app"),
  220. Platform: form.Get("platform"),
  221. Device: form.Get("device"),
  222. }
  223. buildStr := form.Get("build")
  224. buildNum, _ := strconv.ParseInt(buildStr, 10, 64)
  225. midI, _ := c.Get("mid")
  226. mid, _ := midI.(int64)
  227. ap = &archive.ArcParam{}
  228. bs, err := ioutil.ReadAll(c.Request.Body)
  229. if err != nil {
  230. c.JSON(nil, ecode.RequestErr)
  231. return
  232. }
  233. c.Request.Body.Close()
  234. err = json.Unmarshal(bs, ap)
  235. err = json.Unmarshal(bs, cp)
  236. if err != nil {
  237. c.JSON(nil, ecode.VideoupParamErr)
  238. return
  239. }
  240. aid = ap.Aid
  241. ap.Mid = mid
  242. ap.UpFrom = appUpFrom(ar.Platform, ar.Device)
  243. defer func() {
  244. cp.Aid = ap.Aid
  245. cp.Mid = ap.Mid
  246. cp.UpFrom = ap.UpFrom
  247. cp.IPv6 = ap.IPv6
  248. build, buvid := getBuildInfo(c)
  249. vdpSvc.SendArchiveLog(aid, build, buvid, "edit", ar.Platform, cp, err)
  250. }()
  251. err = vdpSvc.AppEditFull(c, ap, mid, buildNum, ar)
  252. if err != nil {
  253. log.Error("AppEditFull Err mid(%+d)|ap(%+v)|err(%+v)", mid, ap, err)
  254. c.JSON(nil, err)
  255. return
  256. }
  257. c.JSON(map[string]interface{}{
  258. "aid": ap.Aid,
  259. }, nil)
  260. }
  261. func appUpFrom(platfrom, device string) (res int8) {
  262. if platfrom == "ios" {
  263. if device == "pad" {
  264. res = archive.UpFromIpad
  265. } else {
  266. res = archive.UpFromAPPiOS
  267. }
  268. } else if platfrom == "android" {
  269. res = archive.UpFromAPPAndroid
  270. } else {
  271. res = archive.UpFromAPP
  272. }
  273. return
  274. }