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"
"os"
"path/filepath"
"strings"
qgen "github.com/Azareal/Gosora/query_gen"
@ -101,16 +102,14 @@ func (s *DefaultAttachmentStore) MiniGetList(originTable string, originID int) (
if err != nil {
return nil, err
}
extarr := strings.Split(a.Path, ".")
if len(extarr) < 2 {
a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(a.Ext) == 0 {
return nil, errors.New("corrupt attachment path")
}
a.Ext = extarr[len(extarr)-1]
a.Image = ImageFileExts.Contains(a.Ext)
alist = append(alist, a)
}
err = rows.Err()
if err != nil {
if err = rows.Err(); err != nil {
return nil, err
}
if len(alist) == 0 {
@ -139,11 +138,10 @@ func (s *DefaultAttachmentStore) BulkMiniGetList(originTable string, ids []int)
if err != nil {
return nil, err
}
extarr := strings.Split(a.Path, ".")
if len(extarr) < 2 {
a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(a.Ext) == 0 {
return nil, errors.New("corrupt attachment path")
}
a.Ext = extarr[len(extarr)-1]
a.Image = ImageFileExts.Contains(a.Ext)
if currentID == 0 {
currentID = a.OriginID
@ -169,11 +167,10 @@ func (s *DefaultAttachmentStore) FGet(id int) (*Attachment, error) {
if err != nil {
return nil, err
}
extarr := strings.Split(a.Path, ".")
if len(extarr) < 2 {
a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(a.Ext) == 0 {
return nil, errors.New("corrupt attachment path")
}
a.Ext = extarr[len(extarr)-1]
a.Image = ImageFileExts.Contains(a.Ext)
return a, nil
}
@ -184,11 +181,10 @@ func (s *DefaultAttachmentStore) Get(id int) (*MiniAttachment, error) {
if err != nil {
return nil, err
}
extarr := strings.Split(a.Path, ".")
if len(extarr) < 2 {
a.Ext = strings.TrimPrefix(filepath.Ext(a.Path), ".")
if len(a.Ext) == 0 {
return nil, errors.New("corrupt attachment path")
}
a.Ext = extarr[len(extarr)-1]
a.Image = ImageFileExts.Contains(a.Ext)
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 {
rlist := theme.Resources
for _, res := range rlist {
if res.Location == "global" || res.Location == "panel" {
extarr := strings.Split(res.Name, ".")
ext := extarr[len(extarr)-1]
if ext == "css" {
if res.LocID == LocGlobal || res.LocID == LocPanel {
if res.Type == ResTypeSheet {
h.AddSheet(res.Name)
} else if ext == "js" {
} else if res.Type == ResTypeScript {
if res.Async {
h.AddScriptAsync(res.Name)
} else {
@ -252,12 +250,10 @@ func PrepResources(u *User, h *Header, theme *Theme) {
if res.Loggedin && !u.Loggedin {
continue
}
if res.Location == "global" || res.Location == "frontend" {
extarr := strings.Split(res.Name, ".")
ext := extarr[len(extarr)-1]
if ext == "css" {
if res.LocID == LocGlobal || res.LocID == LocFront {
if res.Type == ResTypeSheet {
h.AddSheet(res.Name)
} else if ext == "js" {
} else if res.Type == ResTypeScript {
if res.Async {
h.AddScriptAsync(res.Name)
} else {

View File

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

View File

@ -177,6 +177,23 @@ func NewThemeList() (themes ThemeList, err error) {
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
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/">
<ShortName>` + c.Site.Name + `</ShortName>
<InputEncoding>UTF-8</InputEncoding>
<Url type="text/html" template="` + furl + `/topics/?q={searchTerms}" />
<Url type="application/opensearchdescription+xml" rel="self" template="` + furl + `/opensearch.xml" />
<Url type="text/html" template="` + furl + `/topics/?q={searchTerms}"/>
<Url type="application/opensearchdescription+xml" rel="self" template="` + furl + `/opensearch.xml"/>
<moz:SearchForm>` + furl + `</moz:SearchForm>
</OpenSearchDescription>`))
return nil

View File

@ -13,16 +13,16 @@
<script src="/s/jquery-3.1.1.min.js"></script>
{{range .Header.Scripts}}
<script src="/s/{{.}}"></script>{{end}}
<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}}
<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}}
{{/** 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:title"content="{{.Title}} | {{.Header.Site.Name}}">
<meta name="twitter:title"content="{{.Title}} | {{.Header.Site.Name}}"/>
{{if .OGDesc}}<meta property="og:description"content="{{.OGDesc}}"/>
<meta property="twitter:description"content="{{.OGDesc}}"/>{{end}}
{{if .GoogSiteVerify}}<meta name="google-site-verification"content="{{.GoogSiteVerify}}"/>{{end}}
<meta name="twitter:title"content="{{.Title}} | {{.Header.Site.Name}}">
{{if .OGDesc}}<meta property="og:description"content="{{.OGDesc}}">
<meta property="twitter:description"content="{{.OGDesc}}">{{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">
</head>
<body>