|
|
@@ -2,9 +2,11 @@ package modelapp |
|
|
|
|
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"code.gitea.io/gitea/models" |
|
|
|
"crypto/tls" |
|
|
|
"image" |
|
|
|
"image/png" |
|
|
|
"net/http" |
|
|
|
"strconv" |
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting" |
|
|
@@ -17,7 +19,7 @@ import ( |
|
|
|
|
|
|
|
var restyClient *resty.Client |
|
|
|
var tplExploreUpload base.TplName = "model/tuomin/upload" |
|
|
|
var uploadUrl string = "/extension/tuomin/upload" |
|
|
|
var uploadUrl = "/extension/tuomin/upload" |
|
|
|
var allowedContentType = []string{"image/jpeg", "image/jpg", "image/png"} |
|
|
|
|
|
|
|
func ProcessImageUI(ctx *context.Context) { |
|
|
@@ -28,8 +30,7 @@ func ProcessImage(ctx *context.Context) { |
|
|
|
|
|
|
|
file, header, err := ctx.GetFile("file") |
|
|
|
if err != nil { |
|
|
|
ctx.Flash.Error(ctx.Tr("model_app.get_file_fail")) |
|
|
|
ctx.Redirect(setting.AppSubURL + uploadUrl) |
|
|
|
ctx.JSON(http.StatusBadRequest,models.BaseErrorMessage(ctx.Tr("model_app.get_file_fail"))) |
|
|
|
return |
|
|
|
} |
|
|
|
defer file.Close() |
|
|
@@ -37,8 +38,7 @@ func ProcessImage(ctx *context.Context) { |
|
|
|
contentType := header.Header.Get("Content-Type") |
|
|
|
|
|
|
|
if !isInAllowedContentType(contentType) { |
|
|
|
ctx.Flash.Error(ctx.Tr("model_app.content_type_unsupported")) |
|
|
|
ctx.Redirect(setting.AppSubURL + uploadUrl) |
|
|
|
ctx.JSON(http.StatusBadRequest,models.BaseErrorMessage(ctx.Tr("model_app.content_type_unsupported"))) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
@@ -46,14 +46,12 @@ func ProcessImage(ctx *context.Context) { |
|
|
|
res, err := client.R().SetMultipartField( |
|
|
|
"file", header.Filename, contentType, file).Post(setting.ModelApp.DesensitizationUrl + "?mode=" + strconv.Itoa(ctx.QueryInt("mode"))) |
|
|
|
if err != nil { |
|
|
|
ctx.Flash.Error(ctx.Tr("model_app.process_image_fail")) |
|
|
|
ctx.Redirect(setting.AppSubURL + uploadUrl) |
|
|
|
ctx.JSON(http.StatusBadRequest,models.BaseErrorMessage(ctx.Tr("model_app.process_image_fail"))) |
|
|
|
return |
|
|
|
} |
|
|
|
image, _, err := image.Decode(bytes.NewReader(res.Body())) |
|
|
|
if err != nil { |
|
|
|
ctx.Flash.Error(ctx.Tr("model_app.process_image_fail")) |
|
|
|
ctx.Redirect(setting.AppSubURL + uploadUrl) |
|
|
|
ctx.JSON(http.StatusBadRequest,models.BaseErrorMessage(ctx.Tr("model_app.process_image_fail"))) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|