Theming (#624)
* Theming - Custom CSS for blogs - Custom themes for instance - New dark theme - UI to choose your instance theme - Option to disable blog themes if you prefer to only have the instance theme - UI to choose a blog theme
This commit is contained in:
+46
-3
@@ -2,15 +2,20 @@
|
||||
use atom_syndication::{ContentBuilder, Entry, EntryBuilder, LinkBuilder, Person, PersonBuilder};
|
||||
use rocket::{
|
||||
http::{
|
||||
hyper::header::{CacheControl, CacheDirective},
|
||||
hyper::header::{CacheControl, CacheDirective, ETag, EntityTag},
|
||||
uri::{FromUriParam, Query},
|
||||
RawStr, Status,
|
||||
},
|
||||
request::{self, FromFormValue, FromRequest, Request},
|
||||
response::{Flash, NamedFile, Redirect},
|
||||
response::{self, Flash, NamedFile, Redirect, Responder, Response},
|
||||
Outcome,
|
||||
};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{
|
||||
collections::hash_map::DefaultHasher,
|
||||
hash::Hasher,
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use template_utils::Ructe;
|
||||
|
||||
use plume_models::{posts::Post, Connection};
|
||||
@@ -168,6 +173,44 @@ pub struct CachedFile {
|
||||
cache_control: CacheControl,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ThemeFile(NamedFile);
|
||||
|
||||
impl<'r> Responder<'r> for ThemeFile {
|
||||
fn respond_to(mut self, r: &Request) -> response::Result<'r> {
|
||||
let mut contents = String::new();
|
||||
self.0
|
||||
.read_to_string(&mut contents)
|
||||
.map_err(|_| Status::InternalServerError)?;
|
||||
|
||||
let mut hasher = DefaultHasher::new();
|
||||
hasher.write(&contents.as_bytes());
|
||||
let etag = format!("{:x}", hasher.finish());
|
||||
|
||||
if r.headers()
|
||||
.get("If-None-Match")
|
||||
.any(|s| s[1..s.len() - 1] == etag)
|
||||
{
|
||||
Response::build()
|
||||
.status(Status::NotModified)
|
||||
.header(ETag(EntityTag::strong(etag)))
|
||||
.ok()
|
||||
} else {
|
||||
Response::build()
|
||||
.merge(self.0.respond_to(r)?)
|
||||
.header(ETag(EntityTag::strong(etag)))
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/static/cached/<_build_id>/css/<file..>", rank = 1)]
|
||||
pub fn theme_files(file: PathBuf, _build_id: &RawStr) -> Option<ThemeFile> {
|
||||
NamedFile::open(Path::new("static/").join(file))
|
||||
.ok()
|
||||
.map(ThemeFile)
|
||||
}
|
||||
|
||||
#[get("/static/cached/<_build_id>/<file..>", rank = 2)]
|
||||
pub fn plume_static_files(file: PathBuf, _build_id: &RawStr) -> Option<CachedFile> {
|
||||
static_files(file)
|
||||
|
||||
Reference in New Issue
Block a user