urls_controller.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) [2022] [巴拉迪维 BaratSemet]
  2. // [ohUrlShortener] is licensed under Mulan PSL v2.
  3. // You can use this software according to the terms and conditions of the Mulan PSL v2.
  4. // You may obtain a copy of Mulan PSL v2 at:
  5. // http://license.coscl.org.cn/MulanPSL2
  6. // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  7. // See the Mulan PSL v2 for more details.
  8. package controller
  9. import (
  10. "net/http"
  11. "ohurlshortener/core"
  12. "ohurlshortener/service"
  13. "ohurlshortener/utils"
  14. "github.com/gin-gonic/gin"
  15. )
  16. // ShortUrlDetail 重定向到目标地址
  17. func ShortUrlDetail(c *gin.Context) {
  18. url := c.Param("url")
  19. if utils.EmptyString(url) {
  20. c.HTML(http.StatusNotFound, "error.html", gin.H{
  21. "title": "404 - ohUrlShortener",
  22. "code": http.StatusNotFound,
  23. "message": "您访问的页面已失效",
  24. "label": "Status Not Found",
  25. })
  26. return
  27. }
  28. memUrl, err := service.Search4ShortUrl(url)
  29. if err != nil {
  30. c.HTML(http.StatusInternalServerError, "error.html", gin.H{
  31. "title": "内部错误 - ohUrlShortener",
  32. "code": http.StatusInternalServerError,
  33. "message": err.Error(),
  34. "label": "Error",
  35. })
  36. return
  37. }
  38. if utils.EmptyString(memUrl.DestUrl) {
  39. c.HTML(http.StatusNotFound, "error.html", gin.H{
  40. "title": "404 - ohUrlShortener",
  41. "code": http.StatusNotFound,
  42. "message": "您访问的页面已失效",
  43. "label": "Status Not Found",
  44. })
  45. return
  46. }
  47. ua := c.Request.UserAgent()
  48. switch ot := memUrl.OpenType; ot {
  49. case core.OpenInAndroid:
  50. if utils.IsAndroid(ua) {
  51. redirectSuccess(url, memUrl.DestUrl, c)
  52. } else {
  53. redirectFail(c)
  54. }
  55. case core.OpenInDingTalk:
  56. if utils.IsDingTalk(ua) {
  57. redirectSuccess(url, memUrl.DestUrl, c)
  58. } else {
  59. redirectFail(c)
  60. }
  61. case core.OpenInChrome:
  62. if utils.IsChrome(ua) {
  63. redirectSuccess(url, memUrl.DestUrl, c)
  64. } else {
  65. redirectFail(c)
  66. }
  67. case core.OpenInIPad:
  68. if utils.IsIPad(ua) {
  69. redirectSuccess(url, memUrl.DestUrl, c)
  70. } else {
  71. redirectFail(c)
  72. }
  73. case core.OpenInIPhone:
  74. if utils.IsIPhone(ua) {
  75. redirectSuccess(url, memUrl.DestUrl, c)
  76. } else {
  77. redirectFail(c)
  78. }
  79. case core.OpenInSafari:
  80. if utils.IsSafari(ua) {
  81. redirectSuccess(url, memUrl.DestUrl, c)
  82. } else {
  83. redirectFail(c)
  84. }
  85. case core.OpenInWeChat:
  86. if utils.IsWeChatUA(ua) {
  87. redirectSuccess(url, memUrl.DestUrl, c)
  88. } else {
  89. redirectFail(c)
  90. }
  91. case core.OpenInFirefox:
  92. if utils.IsFirefox(ua) {
  93. redirectSuccess(url, memUrl.DestUrl, c)
  94. } else {
  95. redirectFail(c)
  96. }
  97. case core.OpenInAll:
  98. redirectSuccess(url, memUrl.DestUrl, c)
  99. }
  100. }
  101. func redirectSuccess(shortUrl, destUrl string, ctx *gin.Context) {
  102. ctx.Redirect(http.StatusFound, destUrl)
  103. go service.NewAccessLog(shortUrl, ctx.ClientIP(), ctx.Request.UserAgent(), ctx.Request.Referer())
  104. }
  105. func redirectFail(ctx *gin.Context) {
  106. ctx.HTML(http.StatusNotFound, "error.html", gin.H{
  107. "title": "404 - ohUrlShortener",
  108. "code": http.StatusNotFound,
  109. "message": "不支持的打开方式",
  110. "label": "Status Not Found",
  111. })
  112. }