optimise attach exts

optimise PrepResources
This commit is contained in:
Azareal 2020-03-28 09:10:54 +10:00
parent cd66782129
commit d3b2721746
6 changed files with 56 additions and 33 deletions

View File

@ -6,6 +6,7 @@ import (
//"fmt" //"fmt"
"os" "os"
"path/filepath"
"strings" "strings"
qgen "github.com/Azareal/Gosora/query_gen" qgen "github.com/Azareal/Gosora/query_gen"
@ -101,16 +102,14 @@ func (s *DefaultAttachmentStore) MiniGetList(originTable string, originID int) (
if err != nil { if err != nil {
return nil, err return nil, err
} }
extarr := strings.Split(a.Path, ".") a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(extarr) < 2 { if len(a.Ext) == 0 {
return nil, errors.New("corrupt attachment path") return nil, errors.New("corrupt attachment path")
} }
a.Ext = extarr[len(extarr)-1]
a.Image = ImageFileExts.Contains(a.Ext) a.Image = ImageFileExts.Contains(a.Ext)
alist = append(alist, a) alist = append(alist, a)
} }
err = rows.Err() if err = rows.Err(); err != nil {
if err != nil {
return nil, err return nil, err
} }
if len(alist) == 0 { if len(alist) == 0 {
@ -139,11 +138,10 @@ func (s *DefaultAttachmentStore) BulkMiniGetList(originTable string, ids []int)
if err != nil { if err != nil {
return nil, err return nil, err
} }
extarr := strings.Split(a.Path, ".") a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(extarr) < 2 { if len(a.Ext) == 0 {
return nil, errors.New("corrupt attachment path") return nil, errors.New("corrupt attachment path")
} }
a.Ext = extarr[len(extarr)-1]
a.Image = ImageFileExts.Contains(a.Ext) a.Image = ImageFileExts.Contains(a.Ext)
if currentID == 0 { if currentID == 0 {
currentID = a.OriginID currentID = a.OriginID
@ -169,11 +167,10 @@ func (s *DefaultAttachmentStore) FGet(id int) (*Attachment, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
extarr := strings.Split(a.Path, ".") a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(extarr) < 2 { if len(a.Ext) == 0 {
return nil, errors.New("corrupt attachment path") return nil, errors.New("corrupt attachment path")
} }
a.Ext = extarr[len(extarr)-1]
a.Image = ImageFileExts.Contains(a.Ext) a.Image = ImageFileExts.Contains(a.Ext)
return a, nil return a, nil
} }
@ -184,11 +181,10 @@ func (s *DefaultAttachmentStore) Get(id int) (*MiniAttachment, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
extarr := strings.Split(a.Path, ".") a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(extarr) < 2 { if len(a.Ext) == 0 {
return nil, errors.New("corrupt attachment path") return nil, errors.New("corrupt attachment path")
} }
a.Ext = extarr[len(extarr)-1]
a.Image = ImageFileExts.Contains(a.Ext) a.Image = ImageFileExts.Contains(a.Ext)
return a, nil return a, nil
} }

View File

@ -126,12 +126,10 @@ func panelUserCheck(w http.ResponseWriter, r *http.Request, u *User) (h *Header,
if len(theme.Resources) > 0 { if len(theme.Resources) > 0 {
rlist := theme.Resources rlist := theme.Resources
for _, res := range rlist { for _, res := range rlist {
if res.Location == "global" || res.Location == "panel" { if res.LocID == LocGlobal || res.LocID == LocPanel {
extarr := strings.Split(res.Name, ".") if res.Type == ResTypeSheet {
ext := extarr[len(extarr)-1]
if ext == "css" {
h.AddSheet(res.Name) h.AddSheet(res.Name)
} else if ext == "js" { } else if res.Type == ResTypeScript {
if res.Async { if res.Async {
h.AddScriptAsync(res.Name) h.AddScriptAsync(res.Name)
} else { } else {
@ -252,12 +250,10 @@ func PrepResources(u *User, h *Header, theme *Theme) {
if res.Loggedin && !u.Loggedin { if res.Loggedin && !u.Loggedin {
continue continue
} }
if res.Location == "global" || res.Location == "frontend" { if res.LocID == LocGlobal || res.LocID == LocFront {
extarr := strings.Split(res.Name, ".") if res.Type == ResTypeSheet {
ext := extarr[len(extarr)-1]
if ext == "css" {
h.AddSheet(res.Name) h.AddSheet(res.Name)
} else if ext == "js" { } else if res.Type == ResTypeScript {
if res.Async { if res.Async {
h.AddScriptAsync(res.Name) h.AddScriptAsync(res.Name)
} else { } else {

View File

@ -75,9 +75,23 @@ type TemplateMapping struct {
//When string //When string
} }
const (
ResTypeUnknown = iota
ResTypeSheet
ResTypeScript
)
const (
LocUnknown = iota
LocGlobal
LocFront
LocPanel
)
type ThemeResource struct { type ThemeResource struct {
Name string Name string
Type int // 0 = unknown, 1 = sheet, 2 = script
Location string Location string
LocID int
Loggedin bool // Only serve this resource to logged in users Loggedin bool // Only serve this resource to logged in users
Async bool Async bool
} }

View File

@ -177,6 +177,23 @@ func NewThemeList() (themes ThemeList, err error) {
log.Print("no overrides for " + theme.Name) log.Print("no overrides for " + theme.Name)
} }
for i, res := range theme.Resources {
ext := filepath.Ext(res.Name)
if ext == ".css" {
res.Type = ResTypeSheet
} else if ext == ".js" {
res.Type = ResTypeScript
}
if res.Location == "global" {
res.LocID = LocGlobal
} else if res.Location == "frontend" {
res.LocID = LocFront
} else if res.Location == "panel" {
res.LocID = LocPanel
}
theme.Resources[i] = res
}
// TODO: Bind the built template, or an interpreted one for any dock overrides this theme has // TODO: Bind the built template, or an interpreted one for any dock overrides this theme has
themes[theme.Name] = theme themes[theme.Name] = theme

View File

@ -263,8 +263,8 @@ func OpenSearchXml(w http.ResponseWriter, r *http.Request) c.RouteError {
w.Write([]byte(`<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/"> w.Write([]byte(`<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName>` + c.Site.Name + `</ShortName> <ShortName>` + c.Site.Name + `</ShortName>
<InputEncoding>UTF-8</InputEncoding> <InputEncoding>UTF-8</InputEncoding>
<Url type="text/html" template="` + furl + `/topics/?q={searchTerms}" /> <Url type="text/html" template="` + furl + `/topics/?q={searchTerms}"/>
<Url type="application/opensearchdescription+xml" rel="self" template="` + furl + `/opensearch.xml" /> <Url type="application/opensearchdescription+xml" rel="self" template="` + furl + `/opensearch.xml"/>
<moz:SearchForm>` + furl + `</moz:SearchForm> <moz:SearchForm>` + furl + `</moz:SearchForm>
</OpenSearchDescription>`)) </OpenSearchDescription>`))
return nil return nil

View File

@ -13,16 +13,16 @@
<script src="/s/jquery-3.1.1.min.js"></script> <script src="/s/jquery-3.1.1.min.js"></script>
{{range .Header.Scripts}} {{range .Header.Scripts}}
<script src="/s/{{.}}"></script>{{end}} <script src="/s/{{.}}"></script>{{end}}
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"/> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
{{if .Header.MetaDesc}}<meta name="description"content="{{.Header.MetaDesc}}"/>{{end}} {{if .Header.MetaDesc}}<meta name="description"content="{{.Header.MetaDesc}}">{{end}}
{{/** TODO: Have page / forum / topic level tags and descriptions below as-well **/}} {{/** TODO: Have page / forum / topic level tags and descriptions below as-well **/}}
<meta property="og:type"content="website"/> <meta property="og:type"content="website">
<meta property="og:site_name"content="{{.Header.Site.Name}}"> <meta property="og:site_name"content="{{.Header.Site.Name}}">
<meta property="og:title"content="{{.Title}} | {{.Header.Site.Name}}"> <meta property="og:title"content="{{.Title}} | {{.Header.Site.Name}}">
<meta name="twitter:title"content="{{.Title}} | {{.Header.Site.Name}}"/> <meta name="twitter:title"content="{{.Title}} | {{.Header.Site.Name}}">
{{if .OGDesc}}<meta property="og:description"content="{{.OGDesc}}"/> {{if .OGDesc}}<meta property="og:description"content="{{.OGDesc}}">
<meta property="twitter:description"content="{{.OGDesc}}"/>{{end}} <meta property="twitter:description"content="{{.OGDesc}}">{{end}}
{{if .GoogSiteVerify}}<meta name="google-site-verification"content="{{.GoogSiteVerify}}"/>{{end}} {{if .GoogSiteVerify}}<meta name="google-site-verification"content="{{.GoogSiteVerify}}">{{end}}
<link rel="search" type="application/opensearchdescription+xml" title="{{.Header.Site.Name}}" href="/opensearch.xml"> <link rel="search" type="application/opensearchdescription+xml" title="{{.Header.Site.Name}}" href="/opensearch.xml">
</head> </head>
<body> <body>