fix: add JSON tags to struct fields for better serialization (close #3575 #3577)

pull/3472/merge 4.19.1
Aaron Liu 2 weeks ago
parent e03a104a7d
commit a8becb9f5b

@ -216,7 +216,14 @@ func loadShareForOG(c *gin.Context, shareID int, password string) (*explorer.Sha
func renderOGHTML(data *ogData) string { func renderOGHTML(data *ogData) string {
var buf bytes.Buffer var buf bytes.Buffer
if err := ogTemplate.Execute(&buf, data); err != nil { if err := ogTemplate.Execute(&buf, map[string]string{
"SiteName": data.SiteName,
"Title": data.Title,
"Description": data.Description,
"ImageURL": data.ImageURL,
"ShareURL": data.ShareURL,
"RedirectURL": data.RedirectURL,
}); err != nil {
return "" return ""
} }
return buf.String() return buf.String()

@ -44,7 +44,7 @@ func NewResetEmail(ctx context.Context, settings setting.Provider, user *ent.Use
} }
var resTitle strings.Builder var resTitle strings.Builder
err = tmplTitle.Execute(&resTitle, resetCtx) err = tmplTitle.Execute(&resTitle, resetCtx.templateData())
if err != nil { if err != nil {
return "", "", fmt.Errorf("failed to execute email title: %w", err) return "", "", fmt.Errorf("failed to execute email title: %w", err)
} }
@ -55,7 +55,7 @@ func NewResetEmail(ctx context.Context, settings setting.Provider, user *ent.Use
} }
var resBody strings.Builder var resBody strings.Builder
err = tmplBody.Execute(&resBody, resetCtx) err = tmplBody.Execute(&resBody, resetCtx.templateData())
if err != nil { if err != nil {
return "", "", fmt.Errorf("failed to execute email template: %w", err) return "", "", fmt.Errorf("failed to execute email template: %w", err)
} }
@ -90,7 +90,7 @@ func NewActivationEmail(ctx context.Context, settings setting.Provider, user *en
} }
var resTitle strings.Builder var resTitle strings.Builder
err = tmplTitle.Execute(&resTitle, activationCtx) err = tmplTitle.Execute(&resTitle, activationCtx.templateData())
if err != nil { if err != nil {
return "", "", fmt.Errorf("failed to execute email title: %w", err) return "", "", fmt.Errorf("failed to execute email title: %w", err)
} }
@ -101,7 +101,7 @@ func NewActivationEmail(ctx context.Context, settings setting.Provider, user *en
} }
var resBody strings.Builder var resBody strings.Builder
err = tmplBody.Execute(&resBody, activationCtx) err = tmplBody.Execute(&resBody, activationCtx.templateData())
if err != nil { if err != nil {
return "", "", fmt.Errorf("failed to execute email template: %w", err) return "", "", fmt.Errorf("failed to execute email template: %w", err)
} }
@ -112,24 +112,57 @@ func NewActivationEmail(ctx context.Context, settings setting.Provider, user *en
func commonContext(ctx context.Context, settings setting.Provider) *CommonContext { func commonContext(ctx context.Context, settings setting.Provider) *CommonContext {
logo := settings.Logo(ctx) logo := settings.Logo(ctx)
siteUrl := settings.SiteURL(ctx) siteUrl := settings.SiteURL(ctx)
res := &CommonContext{ resolvedLogo := *logo
SiteBasic: settings.SiteBasic(ctx),
Logo: settings.Logo(ctx),
SiteUrl: siteUrl.String(),
}
// Add site url if logo is not an url // Add site url if logo is not an url
if !strings.HasPrefix(logo.Light, "http") { if !strings.HasPrefix(logo.Light, "http") {
logoPath, _ := url.Parse(logo.Light) logoPath, _ := url.Parse(logo.Light)
res.Logo.Light = siteUrl.ResolveReference(logoPath).String() resolvedLogo.Light = siteUrl.ResolveReference(logoPath).String()
} }
if !strings.HasPrefix(logo.Normal, "http") { if !strings.HasPrefix(logo.Normal, "http") {
logoPath, _ := url.Parse(logo.Normal) logoPath, _ := url.Parse(logo.Normal)
res.Logo.Normal = siteUrl.ResolveReference(logoPath).String() resolvedLogo.Normal = siteUrl.ResolveReference(logoPath).String()
}
return &CommonContext{
SiteBasic: settings.SiteBasic(ctx),
Logo: &resolvedLogo,
SiteUrl: siteUrl.String(),
}
}
func (c *CommonContext) templateData() map[string]any {
return map[string]any{
"SiteBasic": map[string]string{
"Name": c.SiteBasic.Name,
"Title": c.SiteBasic.Title,
"ID": c.SiteBasic.ID,
"Description": c.SiteBasic.Description,
"Script": c.SiteBasic.Script,
},
"Logo": map[string]string{
"Normal": c.Logo.Normal,
"Light": c.Logo.Light,
},
"SiteUrl": c.SiteUrl,
}
}
func (c ResetContext) templateData() map[string]any {
return map[string]any{
"CommonContext": c.CommonContext.templateData(),
"User": c.User,
"Url": c.Url,
} }
}
return res func (c ActivationContext) templateData() map[string]any {
return map[string]any{
"CommonContext": c.CommonContext.templateData(),
"User": c.User,
"Url": c.Url,
}
} }
func selectTemplate(templates []setting.EmailTemplate, u *ent.User) setting.EmailTemplate { func selectTemplate(templates []setting.EmailTemplate, u *ent.User) setting.EmailTemplate {
@ -144,4 +177,4 @@ func selectTemplate(templates []setting.EmailTemplate, u *ent.User) setting.Emai
} }
return selected return selected
} }

@ -56,8 +56,8 @@ func (s *EntityDownloadService) Serve(c *gin.Context) error {
type ( type (
SetCurrentVersionParamCtx struct{} SetCurrentVersionParamCtx struct{}
SetCurrentVersionService struct { SetCurrentVersionService struct {
Uri string `uri:"uri" binding:"required"` Uri string `json:"uri" uri:"uri" binding:"required"`
Version string `uri:"version" binding:"required"` Version string `json:"version" uri:"version" binding:"required"`
} }
) )
@ -88,8 +88,8 @@ func (s *SetCurrentVersionService) Set(c *gin.Context) error {
type ( type (
DeleteVersionParamCtx struct{} DeleteVersionParamCtx struct{}
DeleteVersionService struct { DeleteVersionService struct {
Uri string `uri:"uri" binding:"required"` Uri string `json:"uri" uri:"uri" binding:"required"`
Version string `uri:"version" binding:"required"` Version string `json:"version" uri:"version" binding:"required"`
} }
) )

@ -59,8 +59,8 @@ func BuildArchiveListFilesResponse(files []manager.ArchivedFile) *ArchiveListFil
} }
type PutRelativeResponse struct { type PutRelativeResponse struct {
Name string Name string `json:"Name"`
Url string Url string `json:"Url"`
} }
type DirectLinkResponse struct { type DirectLinkResponse struct {
@ -196,45 +196,45 @@ func BuildUploadSessionResponse(session *fs.UploadCredential, hasher hashid.Enco
// WopiFileInfo Response for `CheckFileInfo` // WopiFileInfo Response for `CheckFileInfo`
type WopiFileInfo struct { type WopiFileInfo struct {
// Required // Required
BaseFileName string BaseFileName string `json:"BaseFileName"`
Version string Version string `json:"Version"`
Size int64 Size int64 `json:"Size"`
// Breadcrumb // Breadcrumb
BreadcrumbBrandName string BreadcrumbBrandName string `json:"BreadcrumbBrandName"`
BreadcrumbBrandUrl string BreadcrumbBrandUrl string `json:"BreadcrumbBrandUrl"`
BreadcrumbFolderName string BreadcrumbFolderName string `json:"BreadcrumbFolderName"`
BreadcrumbFolderUrl string BreadcrumbFolderUrl string `json:"BreadcrumbFolderUrl"`
// Post Message // Post Message
FileSharingPostMessage bool FileSharingPostMessage bool `json:"FileSharingPostMessage"`
FileVersionPostMessage bool FileVersionPostMessage bool `json:"FileVersionPostMessage"`
ClosePostMessage bool ClosePostMessage bool `json:"ClosePostMessage"`
PostMessageOrigin string PostMessageOrigin string `json:"PostMessageOrigin"`
// Other miscellaneous properties // Other miscellaneous properties
FileNameMaxLength int FileNameMaxLength int `json:"FileNameMaxLength"`
LastModifiedTime string LastModifiedTime string `json:"LastModifiedTime"`
// User metadata // User metadata
IsAnonymousUser bool IsAnonymousUser bool `json:"IsAnonymousUser"`
UserFriendlyName string UserFriendlyName string `json:"UserFriendlyName"`
UserId string UserId string `json:"UserId"`
OwnerId string OwnerId string `json:"OwnerId"`
// Permission // Permission
ReadOnly bool ReadOnly bool `json:"ReadOnly"`
UserCanRename bool UserCanRename bool `json:"UserCanRename"`
UserCanReview bool UserCanReview bool `json:"UserCanReview"`
UserCanWrite bool UserCanWrite bool `json:"UserCanWrite"`
UserCanNotWriteRelative bool UserCanNotWriteRelative bool `json:"UserCanNotWriteRelative"`
SupportsRename bool SupportsRename bool `json:"SupportsRename"`
SupportsReviewing bool SupportsReviewing bool `json:"SupportsReviewing"`
SupportsUpdate bool SupportsUpdate bool `json:"SupportsUpdate"`
SupportsLocks bool SupportsLocks bool `json:"SupportsLocks"`
EnableShare bool EnableShare bool `json:"EnableShare"`
} }
type ViewerSessionResponse struct { type ViewerSessionResponse struct {

@ -30,7 +30,7 @@ type (
Metadata map[string]string `json:"metadata" binding:"max=256"` Metadata map[string]string `json:"metadata" binding:"max=256"`
EntityType string `json:"entity_type" binding:"eq=|eq=live_photo|eq=version"` EntityType string `json:"entity_type" binding:"eq=|eq=live_photo|eq=version"`
EncryptionSupported []types.Cipher `json:"encryption_supported"` EncryptionSupported []types.Cipher `json:"encryption_supported"`
Previous string `form:"previous"` Previous string `json:"previous" form:"previous"`
} }
) )

Loading…
Cancel
Save