You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

desensitization.go 1.9 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package modelapp
  2. import (
  3. "bytes"
  4. "code.gitea.io/gitea/models"
  5. "crypto/tls"
  6. "image"
  7. "image/png"
  8. "net/http"
  9. "strconv"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "github.com/go-resty/resty/v2"
  14. )
  15. var restyClient *resty.Client
  16. var tplExploreUpload base.TplName = "model/tuomin/upload"
  17. var uploadUrl = "/extension/tuomin/upload"
  18. var allowedContentType = []string{"image/jpeg", "image/jpg", "image/png"}
  19. func ProcessImageUI(ctx *context.Context) {
  20. ctx.HTML(200, tplExploreUpload)
  21. }
  22. func ProcessImage(ctx *context.Context) {
  23. file, header, err := ctx.GetFile("file")
  24. if err != nil {
  25. ctx.JSON(http.StatusBadRequest,models.BaseErrorMessage(ctx.Tr("model_app.get_file_fail")))
  26. return
  27. }
  28. defer file.Close()
  29. contentType := header.Header.Get("Content-Type")
  30. if !isInAllowedContentType(contentType) {
  31. ctx.JSON(http.StatusBadRequest,models.BaseErrorMessage(ctx.Tr("model_app.content_type_unsupported")))
  32. return
  33. }
  34. client := getRestyClient()
  35. res, err := client.R().SetMultipartField(
  36. "file", header.Filename, contentType, file).Post(setting.ModelApp.DesensitizationUrl + "?mode=" + strconv.Itoa(ctx.QueryInt("mode")))
  37. if err != nil {
  38. ctx.JSON(http.StatusBadRequest,models.BaseErrorMessage(ctx.Tr("model_app.process_image_fail")))
  39. return
  40. }
  41. image, _, err := image.Decode(bytes.NewReader(res.Body()))
  42. if err != nil {
  43. ctx.JSON(http.StatusBadRequest,models.BaseErrorMessage(ctx.Tr("model_app.process_image_fail")))
  44. return
  45. }
  46. png.Encode(ctx.Resp, image)
  47. return
  48. }
  49. func isInAllowedContentType(contentType string) bool {
  50. for _, allowType := range allowedContentType {
  51. if allowType == contentType {
  52. return true
  53. }
  54. }
  55. return false
  56. }
  57. func getRestyClient() *resty.Client {
  58. if restyClient == nil {
  59. restyClient = resty.New()
  60. restyClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  61. }
  62. return restyClient
  63. }