cover dynamically loaded static resources

cover default noavatars
This commit is contained in:
Azareal 2020-07-31 09:40:14 +10:00
parent 43d72e6f3b
commit 26ad61057a
7 changed files with 34 additions and 30 deletions

View File

@ -182,17 +182,6 @@ var noavatarCache200 []string
var noavatarCache48 []string
func ProcessConfig() (err error) {
if !Config.DisableDefaultNoavatar {
noavatarCache200 = make([]string, 5)
noavatarCache48 = make([]string, 5)
for i := 0; i < 5; i++ {
noavatarCache200[i] = "/s/n" + strconv.Itoa(i) + "-" + strconv.Itoa(200) + ".png?i=0"
noavatarCache48[i] = "/s/n" + strconv.Itoa(i) + "-" + strconv.Itoa(48) + ".png?i=0"
}
}
Config.Noavatar = strings.Replace(Config.Noavatar, "{site_url}", Site.URL, -1)
guestAvatar = GuestAvatar{buildNoavatar(0, 200), buildNoavatar(0, 48)}
// Strip these unnecessary bits, if we find them.
Site.URL = strings.TrimPrefix(Site.URL, "http://")
Site.URL = strings.TrimPrefix(Site.URL, "https://")
@ -257,6 +246,16 @@ func ProcessConfig() (err error) {
if Config.StaticResBase != "" {
StaticFiles.Prefix = Config.StaticResBase
}
if !Config.DisableDefaultNoavatar {
noavatarCache200 = make([]string, 5)
noavatarCache48 = make([]string, 5)
for i := 0; i < 5; i++ {
noavatarCache200[i] = StaticFiles.Prefix + "n" + strconv.Itoa(i) + "-" + strconv.Itoa(200) + ".png?i=0"
noavatarCache48[i] = StaticFiles.Prefix + "n" + strconv.Itoa(i) + "-" + strconv.Itoa(48) + ".png?i=0"
}
}
Config.Noavatar = strings.Replace(Config.Noavatar, "{site_url}", Site.URL, -1)
guestAvatar = GuestAvatar{buildNoavatar(0, 200), buildNoavatar(0, 48)}
if Config.PostIPCutoff == 0 {
Config.PostIPCutoff = 120 // Default cutoff

View File

@ -788,7 +788,7 @@ func buildNoavatar(uid, width int) string {
} else if width == 48 {
return noavatarCache48[uid]
}
return "/s/n" + strconv.Itoa(uid) + "-" + strconv.Itoa(width) + ".png?i=0"
return StaticFiles.Prefix + "n" + strconv.Itoa(uid) + "-" + strconv.Itoa(width) + ".png?i=0"
}
return strings.Replace(strings.Replace(Config.Noavatar, "{id}", strconv.Itoa(uid), 1), "{width}", strconv.Itoa(width), 1)
}

View File

@ -896,11 +896,11 @@ function mainInit(){
if(xr!=null) {
for(let res of xr.split(",")) {
let pro;
if(stripQ(getExt(res))=="css") pro = asyncGetSheet("/s/"+res)
else pro = asyncGetScript("/s/"+res)
if(stripQ(getExt(res))=="css") pro = asyncGetSheet(pre+res)
else pro = asyncGetScript(pre+res)
pro.then(() => log("Loaded "+res))
.catch(e => {
log("Unable to get '"+res+"'",e);
log("Unable to get "+res,e);
console.trace();
});
}

View File

@ -6,6 +6,7 @@ var tmplPhrases=[]; // [key] array of phrases indexed by order of use
var hooks={};
var ranInitHooks={}
var log=console.log;
var pre="/s/";
function runHook(name,...args) {
if(!(name in hooks)) {
@ -32,7 +33,10 @@ function runInitHook(name,...args) {
}
function addInitHook(name,h) {
addHook(name,h);
if(name in ranInitHooks) h();
if(name in ranInitHooks) {
log("Delay running "+name);
h();
}
}
// Temporary hack for templates
@ -52,7 +56,6 @@ function asyncGetScript(src) {
isAbort ? reject(e) : resolve();
}
}
script.onerror = e => {
reject(e);
};
@ -66,9 +69,9 @@ function asyncGetScript(src) {
}
function notifyOnScript(src) {
src = "/s/"+src;
src = pre+src;
return new Promise((resolve,reject) => {
let ss = src.replace("/s/","");
let ss = src.replace(pre,"");
try {
let ssp = ss.charAt(0).toUpperCase() + ss.slice(1)
log("ssp",ssp)
@ -119,8 +122,8 @@ function loadScript(name,h,fail) {
let parts = value.split("; current_theme=");
if(parts.length==2) fname += "_"+parts.pop().split(";").shift();
let url = `/s/${fname}.js`
let iurl = `/s/${name}.js`
let url = pre+fname+".js"
let iurl = pre+name+".js"
asyncGetScript(url)
.then(h).catch(e => {
log("Unable to get "+url,e);
@ -209,10 +212,11 @@ function fetchPhrases(plist) {
.then(d => {
log("me",d);
me=d;
pre=d.StaticPrefix;
runInitHook("pre_init");
});
} else {
me={User:{ID:0,S:""},Site:{"MaxRequestSize":0}};
me={User:{ID:0,S:""},Site:{"MaxReqSize":0}};
runInitHook("pre_init");
}
})()

View File

@ -23,7 +23,7 @@ var imageExts = ["png","jpg","jpe","jpeg","jif","jfi","jfif","svg","bmp","gif","
log("file "+i,files[i]);
totalSize += files[i]["size"];
}
if(totalSize > me.Site.MaxRequestSize) throw("You can't upload this much at once, max: "+me.Site.MaxRequestSize);
if(totalSize > me.Site.MaxReqSize) throw("You can't upload this much at once, max: "+me.Site.MaxReqSize);
for(let i=0; i<files.length; i++) {
let fname = files[i]["name"];

View File

@ -229,7 +229,8 @@ type JsonMe struct {
// We don't want to expose too much information about the site, so we'll make this a small subset of c.site
type MeSite struct {
MaxRequestSize int
MaxReqSize int
StaticPrefix string
}
// APIMe returns information about the current logged-in user
@ -243,7 +244,7 @@ func APIMe(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
// TODO: Use this header anywhere with a user check?
h.Set("Cache-Control", "private")
me := JsonMe{u.Me(), MeSite{c.Site.MaxRequestSize}}
me := JsonMe{u.Me(), MeSite{c.Site.MaxRequestSize, c.StaticFiles.Prefix}}
jsonBytes, err := json.Marshal(me)
if err != nil {
return c.InternalErrorJS(err, w, r)

View File

@ -21,7 +21,7 @@
addInitHook("end_init", () => {
let loggedIn = document.head.querySelector("[property='x-mem']")!=null;
if(loggedIn) {
if(navigator.userAgent.indexOf("Firefox")!=-1) $.trumbowyg.svgPath = "/s/trumbowyg/ui/icons.svg";
if(navigator.userAgent.indexOf("Firefox")!=-1) $.trumbowyg.svgPath = pre+"trumbowyg/ui/icons.svg";
// Is there we way we can append instead? Maybe, an editor plugin?
attachItemCallback = function(attachItem) {