router_material.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. package back
  2. import (
  3. "bufio"
  4. "html/template"
  5. "image"
  6. "image/jpeg"
  7. "mime/multipart"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "github.com/gin-gonic/gin"
  12. "github.com/nfnt/resize"
  13. )
  14. func prepareRouterMaterial() {
  15. router.POST("/material-insert/:category", handlerInsertMaterial)
  16. router.POST("/material-update/:category", handlerUpdateMaterial)
  17. router.POST("/material-delete/:link", handlerDeleteMaterial)
  18. router.POST("/material-restore/:link", handlerRestoreMaterial)
  19. router.POST("/material-select-limit/:category", handlerSelectLimitMaterial)
  20. router.POST("/material-search", handlerMaterialSearch)
  21. router.POST("/material-get-source/:link", handlerMaterialGetSource)
  22. router.POST("/material-get", handlerMaterialGet)
  23. }
  24. func handlerMaterial(c *gin.Context) {
  25. m := material{}
  26. h := gin.H{}
  27. m.Link = c.Param("link")
  28. isAvailableMaterial := true
  29. e := m.SelectOne()
  30. if e != nil {
  31. if strings.Contains(e.Error(), "no rows") {
  32. h["OtherPage"] = "404"
  33. isAvailableMaterial = false
  34. }
  35. }
  36. h["Page"] = m
  37. if m.Active == false {
  38. h["OtherPage"] = "404"
  39. isAvailableMaterial = false
  40. } else {
  41. if isAvailableMaterial {
  42. if m.Category != c.Param("category") {
  43. c.Redirect(302, Setting.ServerDomain+"/данные/"+m.Category+"/"+m.Link)
  44. return
  45. }
  46. }
  47. }
  48. isAdmin := false
  49. isLogin := false
  50. isSecreter := false
  51. role := getRoleFromContext(c)
  52. if len(role) != 0 {
  53. if role == "__admin" {
  54. isAdmin = true
  55. isAvailableMaterial = true
  56. } else if role == "_secretary" {
  57. isSecreter = true
  58. }
  59. isLogin = true
  60. }
  61. h["IsMain"] = false
  62. h["IsLogin"] = isLogin
  63. h["IsAdmin"] = isAdmin
  64. h["isSecreter"] = isSecreter
  65. if isAdmin {
  66. h["Menu"] = templateMenuAll
  67. h["MenuTop"] = templateMenuAllTop
  68. } else {
  69. h["Menu"] = templateMenu
  70. h["MenuTop"] = templateMenuTop
  71. }
  72. h["AvailableMaterial"] = isAvailableMaterial
  73. h["ServerVersion"] = Setting.ServerVersion
  74. c.HTML(200, "index.html", h)
  75. }
  76. func handlerMaterialSearch(c *gin.Context) {
  77. s := search{}
  78. e := c.BindJSON(&s)
  79. if e != nil {
  80. logger.Println("ERROR", e)
  81. c.JSON(200, gin.H{
  82. "Error": nil,
  83. "Data": nil,
  84. })
  85. return
  86. }
  87. m := material{}
  88. e = m.Search(s.Phrase)
  89. if e != nil {
  90. logger.Println("ERROR", e)
  91. c.JSON(200, gin.H{
  92. "Error": nil,
  93. "Data": nil,
  94. })
  95. return
  96. }
  97. c.JSON(200, gin.H{
  98. "Error": nil,
  99. "Data": m.Rows,
  100. })
  101. }
  102. func handlerInsertMaterial(c *gin.Context) {
  103. man := getManagerFromContext(c)
  104. if man.Role != "__admin" {
  105. c.JSON(200, gin.H{
  106. "Error": errorAccessRestricted,
  107. })
  108. return
  109. }
  110. form, e := c.MultipartForm()
  111. if e != nil {
  112. logger.Println("ERROR", e)
  113. c.JSON(200, gin.H{
  114. "Error": errorBadRequestData,
  115. })
  116. return
  117. }
  118. m := material{}
  119. m.Category = c.Param("category")
  120. m.Author = man.ID
  121. m.Caption = form.Value["Caption"][0]
  122. m.Source = form.Value["Source"][0]
  123. m.SourceImages = form.Value["SourceImages"][0]
  124. m.Link = form.Value["Link"][0]
  125. m.Content = template.HTML(form.Value["Content"][0])
  126. m.Preview.Valid, _ = strconv.ParseBool(form.Value["PreviewValid"][0])
  127. if m.Preview.Valid {
  128. m.Preview.String = form.Value["PreviewString"][0]
  129. }
  130. m.Image.Valid, _ = strconv.ParseBool(form.Value["ImageValid"][0])
  131. if m.Image.Valid {
  132. m.Image.String = form.Value["ImageString"][0]
  133. }
  134. imagePath := Setting.Data + form.Value["ImagePath"][0]
  135. thumbPath := Setting.Data + form.Value["ThumbPath"][0]
  136. _, err := os.Stat(imagePath)
  137. if os.IsNotExist(err) {
  138. err = os.MkdirAll(imagePath, 0777)
  139. if err != nil {
  140. logger.Println("ERROR: ", err.Error())
  141. c.JSON(200, gin.H{
  142. "Error": err.Error(),
  143. })
  144. return
  145. }
  146. }
  147. _, err = os.Stat(thumbPath)
  148. if os.IsNotExist(err) {
  149. err = os.MkdirAll(thumbPath, 0777)
  150. if err != nil {
  151. logger.Println("ERROR: ", err.Error())
  152. c.JSON(200, gin.H{
  153. "Error": err.Error(),
  154. })
  155. return
  156. }
  157. }
  158. e = uploadImageFiles(imagePath, thumbPath, form.File["Files"], c)
  159. if e != nil {
  160. logger.Println(e)
  161. c.JSON(200, gin.H{
  162. "Error": e.Error(),
  163. })
  164. return
  165. }
  166. e = m.Insert()
  167. if e != nil {
  168. logger.Println(e)
  169. c.JSON(200, gin.H{
  170. "Error": e.Error(),
  171. })
  172. return
  173. }
  174. if m.Category == "новость" {
  175. prepareNews()
  176. prepareNewsPagination()
  177. }
  178. if m.Category == "объявление" {
  179. prepareNotice()
  180. }
  181. c.JSON(200, gin.H{
  182. "Error": nil,
  183. })
  184. }
  185. func handlerUpdateMaterial(c *gin.Context) {
  186. man := getManagerFromContext(c)
  187. if man.Role != "__admin" {
  188. c.JSON(200, gin.H{
  189. "Error": errorAccessRestricted,
  190. })
  191. return
  192. }
  193. form, e := c.MultipartForm()
  194. if e != nil {
  195. logger.Println("ERROR", e)
  196. c.JSON(200, gin.H{
  197. "Error": errorBadRequestData,
  198. })
  199. return
  200. }
  201. m := material{}
  202. m.Category = c.Param("category")
  203. m.Author = man.ID
  204. m.Caption = form.Value["Caption"][0]
  205. m.Source = form.Value["Source"][0]
  206. // m.SourceImages = form.Value["SourceImages"][0]
  207. m.Link = form.Value["Link"][0]
  208. m.Content = template.HTML(form.Value["Content"][0])
  209. m.Preview.Valid, _ = strconv.ParseBool(form.Value["PreviewValid"][0])
  210. if m.Preview.Valid {
  211. m.Preview.String = form.Value["PreviewString"][0]
  212. }
  213. // m.Image.Valid, _ = strconv.ParseBool(form.Value["ImageValid"][0])
  214. // if m.Image.Valid {
  215. // m.Image.String = form.Value["ImageString"][0]
  216. // }
  217. imagePath := Setting.Data + form.Value["ImagePath"][0]
  218. thumbPath := Setting.Data + form.Value["ThumbPath"][0]
  219. _, err := os.Stat(imagePath)
  220. if os.IsNotExist(err) {
  221. err = os.MkdirAll(imagePath, 0777)
  222. if err != nil {
  223. logger.Println("ERROR: ", err.Error())
  224. c.JSON(200, gin.H{
  225. "Error": err.Error(),
  226. })
  227. return
  228. }
  229. }
  230. _, err = os.Stat(thumbPath)
  231. if os.IsNotExist(err) {
  232. err = os.MkdirAll(thumbPath, 0777)
  233. if err != nil {
  234. logger.Println("ERROR: ", err.Error())
  235. c.JSON(200, gin.H{
  236. "Error": err.Error(),
  237. })
  238. return
  239. }
  240. }
  241. e = uploadImageFiles(imagePath, thumbPath, form.File["Files"], c)
  242. if e != nil {
  243. logger.Println(e)
  244. c.JSON(200, gin.H{
  245. "Error": e.Error(),
  246. })
  247. return
  248. }
  249. e = m.Update()
  250. if e != nil {
  251. logger.Println(e)
  252. c.JSON(200, gin.H{
  253. "Error": e.Error(),
  254. })
  255. return
  256. }
  257. c.JSON(200, gin.H{
  258. "Error": nil,
  259. })
  260. }
  261. func handlerDeleteMaterial(c *gin.Context) {
  262. man := getManagerFromContext(c)
  263. if man.Role != "__admin" {
  264. c.JSON(200, gin.H{
  265. "Error": errorAccessRestricted,
  266. })
  267. return
  268. }
  269. m := material{}
  270. m.Link = c.Param("link")
  271. m.Active = false
  272. e := m.Delete()
  273. if e != nil {
  274. logger.Println(e)
  275. c.JSON(200, gin.H{
  276. "Error": e.Error(),
  277. })
  278. return
  279. }
  280. c.JSON(200, gin.H{
  281. "Error": nil,
  282. })
  283. }
  284. func handlerRestoreMaterial(c *gin.Context) {
  285. man := getManagerFromContext(c)
  286. if man.Role != "__admin" {
  287. c.JSON(200, gin.H{
  288. "Error": errorAccessRestricted,
  289. })
  290. return
  291. }
  292. m := material{}
  293. m.Link = c.Param("link")
  294. m.Active = true
  295. e := m.Delete()
  296. if e != nil {
  297. logger.Println(e)
  298. c.JSON(200, gin.H{
  299. "Error": e.Error(),
  300. })
  301. return
  302. }
  303. c.JSON(200, gin.H{
  304. "Error": nil,
  305. })
  306. }
  307. func handlerSelectLimitMaterial(c *gin.Context) {
  308. l := limit{}
  309. e := c.BindJSON(&l)
  310. if e != nil {
  311. logger.Println(e.Error())
  312. c.JSON(200, gin.H{
  313. "Error": e.Error(),
  314. })
  315. return
  316. }
  317. news := material{}
  318. news.Category = c.Param("category")
  319. e = news.Select(l.Offset, l.Limit)
  320. if e != nil {
  321. logger.Println(e.Error())
  322. c.JSON(200, gin.H{
  323. "Error": e.Error(),
  324. })
  325. return
  326. }
  327. c.JSON(200, gin.H{
  328. "Error": nil,
  329. "Data": news.Rows,
  330. })
  331. }
  332. func uploadImageFiles(imagePath, thumbPath string, files []*multipart.FileHeader, c *gin.Context) error {
  333. for _, file := range files {
  334. if e := c.SaveUploadedFile(file, imagePath+file.Filename); e != nil {
  335. logger.Println("ERROR: ", e.Error())
  336. return e
  337. }
  338. src, e := file.Open()
  339. if e != nil {
  340. logger.Println("ERROR: ", e.Error())
  341. return e
  342. }
  343. img0, _, e := image.Decode(src)
  344. if e != nil {
  345. logger.Println("ERROR: ", e.Error())
  346. return e
  347. }
  348. img := resize.Resize(450, 0, img0, resize.Bicubic)
  349. var fileThumb *os.File
  350. fileThumb, e = os.Create(thumbPath + file.Filename)
  351. if e != nil {
  352. logger.Println("ERROR: ", e.Error())
  353. return e
  354. }
  355. defer fileThumb.Close()
  356. fileWrite := bufio.NewWriter(fileThumb)
  357. e = jpeg.Encode(fileWrite, img, nil)
  358. if e != nil {
  359. logger.Println("ERROR: ", e.Error())
  360. return e
  361. }
  362. }
  363. return nil
  364. }
  365. func handlerMaterialGetSource(c *gin.Context) {
  366. m := material{}
  367. m.Link = c.Param("link")
  368. e := m.SelectSource()
  369. if e != nil {
  370. logger.Println(e)
  371. c.JSON(200, gin.H{
  372. "Error": e.Error(),
  373. })
  374. return
  375. }
  376. c.JSON(200, gin.H{
  377. "Error": nil,
  378. "Data": m,
  379. })
  380. }
  381. func handlerMaterialGet(c *gin.Context) {
  382. m := material{}
  383. e := m.SelectAll()
  384. if e != nil {
  385. logger.Println("ERROR", e)
  386. c.JSON(200, gin.H{
  387. "Error": nil,
  388. "Data": nil,
  389. })
  390. return
  391. }
  392. c.JSON(200, gin.H{
  393. "Error": nil,
  394. "Data": m.Rows,
  395. })
  396. }