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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package modelapp
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "image"
  6. "image/png"
  7. "strconv"
  8. "code.gitea.io/gitea/modules/setting"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "github.com/go-resty/resty/v2"
  12. )
  13. var restyClient *resty.Client
  14. var tplExploreUpload base.TplName = "model/tuomin/upload"
  15. var uploadUrl string = "/extension/tuomin/upload"
  16. var allowedContentType = []string{"image/jpeg", "image/jpg", "image/png"}
  17. func ProcessImageUI(ctx *context.Context) {
  18. ctx.HTML(200, tplExploreUpload)
  19. }
  20. func ProcessImage(ctx *context.Context) {
  21. file, header, err := ctx.GetFile("file")
  22. if err != nil {
  23. ctx.Flash.Error(ctx.Tr("model_app.get_file_fail"))
  24. ctx.Redirect(setting.AppSubURL + uploadUrl)
  25. return
  26. }
  27. defer file.Close()
  28. contentType := header.Header.Get("Content-Type")
  29. if !isInAllowedContentType(contentType) {
  30. ctx.Flash.Error(ctx.Tr("model_app.content_type_unsupported"))
  31. ctx.Redirect(setting.AppSubURL + uploadUrl)
  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.Flash.Error(ctx.Tr("model_app.process_image_fail"))
  39. ctx.Redirect(setting.AppSubURL + uploadUrl)
  40. return
  41. }
  42. image, _, err := image.Decode(bytes.NewReader(res.Body()))
  43. if err != nil {
  44. ctx.Flash.Error(ctx.Tr("model_app.process_image_fail"))
  45. ctx.Redirect(setting.AppSubURL + uploadUrl)
  46. return
  47. }
  48. png.Encode(ctx.Resp, image)
  49. return
  50. }
  51. func isInAllowedContentType(contentType string) bool {
  52. for _, allowType := range allowedContentType {
  53. if allowType == contentType {
  54. return true
  55. }
  56. }
  57. return false
  58. }
  59. func getRestyClient() *resty.Client {
  60. if restyClient == nil {
  61. restyClient = resty.New()
  62. restyClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  63. }
  64. return restyClient
  65. }