Skip to content

Commit

Permalink
Merge branch 'feat/1.4.2/seo' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkinStars committed Nov 29, 2024
2 parents 86a3da6 + ce0ec4b commit a63078e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
12 changes: 10 additions & 2 deletions internal/controller/template_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,20 @@ func (tc *TemplateController) UserInfo(ctx *gin.Context) {
return
}

questionList, answerList, err := tc.questionService.SearchUserTopList(ctx, req.Username, "")
if err != nil {
tc.Page404(ctx)
return
}

siteInfo := tc.SiteInfo(ctx)
siteInfo.Canonical = fmt.Sprintf("%s/users/%s", siteInfo.General.SiteUrl, username)
siteInfo.Title = fmt.Sprintf("%s - %s", username, siteInfo.General.Name)
tc.html(ctx, http.StatusOK, "homepage.html", siteInfo, gin.H{
"userinfo": userinfo,
"bio": template.HTML(userinfo.BioHTML),
"userinfo": userinfo,
"bio": template.HTML(userinfo.BioHTML),
"topQuestions": questionList,
"topAnswers": answerList,
})

}
Expand Down
5 changes: 5 additions & 0 deletions internal/controller/upload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package controller

import (
"github.com/apache/incubator-answer/internal/base/handler"
"github.com/apache/incubator-answer/internal/base/middleware"
"github.com/apache/incubator-answer/internal/base/reason"
"github.com/apache/incubator-answer/internal/schema"
"github.com/apache/incubator-answer/internal/service/uploader"
Expand Down Expand Up @@ -75,6 +76,10 @@ func (uc *UploadController) UploadFile(ctx *gin.Context) {
case fileFromPost:
url, err = uc.uploaderService.UploadPostFile(ctx)
case fileFromBranding:
if !middleware.GetIsAdminFromContext(ctx) {
handler.HandleResponse(ctx, errors.Forbidden(reason.ForbiddenError), nil)
return
}
url, err = uc.uploaderService.UploadBrandingFile(ctx)
case fileFromPostAttachment:
url, err = uc.uploaderService.UploadPostAttachment(ctx)
Expand Down
22 changes: 19 additions & 3 deletions internal/service/uploader/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ func (us *uploaderService) UploadAvatarFile(ctx *gin.Context) (url string, err e
return url, nil
}

// max size
ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, 5*1024*1024)
siteWrite, err := us.siteInfoService.GetSiteWrite(ctx)
if err != nil {
return "", err
}

ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, siteWrite.GetMaxImageSize())
file, fileHeader, err := ctx.Request.FormFile("file")
if err != nil {
return "", errors.BadRequest(reason.RequestFormatError).WithError(err)
Expand Down Expand Up @@ -317,8 +321,20 @@ func (us *uploaderService) uploadAttachmentFile(ctx *gin.Context, file *multipar

func (us *uploaderService) tryToUploadByPlugin(ctx *gin.Context, source plugin.UploadSource) (
url string, err error) {
siteWrite, err := us.siteInfoService.GetSiteWrite(ctx)
if err != nil {
return "", err
}
cond := plugin.UploadFileCondition{
Source: source,
MaxImageSize: siteWrite.MaxImageSize,
MaxAttachmentSize: siteWrite.MaxAttachmentSize,
MaxImageMegapixel: siteWrite.MaxImageMegapixel,
AuthorizedImageExtensions: siteWrite.AuthorizedImageExtensions,
AuthorizedAttachmentExtensions: siteWrite.AuthorizedAttachmentExtensions,
}
_ = plugin.CallStorage(func(fn plugin.Storage) error {
resp := fn.UploadFile(ctx, source)
resp := fn.UploadFile(ctx, cond)
if resp.OriginalError != nil {
log.Errorf("upload file by plugin failed, err: %v", resp.OriginalError)
err = errors.BadRequest("").WithMsg(resp.DisplayErrorMsg.Translate(ctx)).WithError(err)
Expand Down
17 changes: 16 additions & 1 deletion plugin/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ var (
}
)

type UploadFileCondition struct {
// Source is the source of the file
Source UploadSource
// MaxImageSize is the maximum size of the image in MB
MaxImageSize int
// MaxAttachmentSize is the maximum size of the attachment in MB
MaxAttachmentSize int
// MaxImageMegapixel is the maximum megapixel of the image
MaxImageMegapixel int
// AuthorizedImageExtensions is the list of authorized image extensions
AuthorizedImageExtensions []string
// AuthorizedAttachmentExtensions is the list of authorized attachment extensions
AuthorizedAttachmentExtensions []string
}

type UploadFileResponse struct {
// FullURL is the URL that can be used to access the file
FullURL string
Expand All @@ -66,7 +81,7 @@ type Storage interface {

// UploadFile uploads a file to storage.
// The file is in the Form of the ctx and the key is "file"
UploadFile(ctx *GinContext, source UploadSource) UploadFileResponse
UploadFile(ctx *GinContext, condition UploadFileCondition) UploadFileResponse
}

var (
Expand Down
33 changes: 32 additions & 1 deletion ui/template/homepage.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,41 @@
<div>
<h5 class="mb-3">{{translator $.language "ui.personal.about_me"}}</h5>
{{if .bio }}
<div class="text-center mb-4">{{.bio}}</div>
<div class="mb-5 text-break fmt">{{.bio}}</div>
{{else}}
<div class="text-center py-5 mb-4">{{translator $.language "ui.personal.about_me_empty"}}</div>
{{end}}
<div class="mb-4 row">
<div class="mb-4 col-md-6 col-sm-12">
<h5 class="mb-3">Top Answers</h5>
<ol class="list-unstyled">
{{ range .topAnswers }}
<li class="mb-2">
<a class="text-truncate-1" href="{{$.baseURL}}/questions/{{.QuestionID}}/{{.AnswerID}}">{{.QuestionInfo.Title}}</a>
<div class="text-secondary small">
<i class="br bi-hand-thumbs-up-fill me-1"></i><span>{{.VoteCount}} votes</span>
</div>
</li>
{{ end }}
</ol>
</div>
<div class="col-md-6 col-sm-12">
<h5 class="mb-3">Top Questions</h5>
<ol class="list-unstyled">
{{ range .topQuestions }}
<li class="mb-2">
<a class="text-truncate-1" href="{{$.baseURL}}/questions/{{.ID}}">{{.Title}}</a>
<div class="text-secondary small">
<i class="br bi-hand-thumbs-up-fill me-1"></i><span> {{.VoteCount}} votes</span>
<div class="d-inline-block text-secondary ms-3 small text-success">
<i class="br bi-check-circle-fill"></i><span> {{.AnswerCount}} answers</span>
</div>
</div>
</li>
{{ end }}
</ol>
</div>
</div>
</div>
</div>
<div class="mt-5 mt-lg-0 col-xxl-3 col-lg-4 col-sm-12">
Expand Down

0 comments on commit a63078e

Please sign in to comment.