chore (backend-rs): cache generated icons
This commit is contained in:
parent
07c5bfa600
commit
2c35a3885c
4 changed files with 24 additions and 6 deletions
2
packages/backend-rs/index.d.ts
vendored
2
packages/backend-rs/index.d.ts
vendored
|
@ -495,7 +495,7 @@ export declare function genId(): string
|
|||
/** Generate an ID using a specific datetime */
|
||||
export declare function genIdAt(date: Date): string
|
||||
|
||||
export declare function genIdenticon(id: string): Buffer
|
||||
export declare function genIdenticon(id: string): Promise<Buffer>
|
||||
|
||||
export declare function getFullApAccount(username: string, host?: string | undefined | null): string
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ pub enum Category {
|
|||
Block,
|
||||
Follow,
|
||||
CatLang,
|
||||
RandomIcon,
|
||||
#[cfg(test)]
|
||||
Test,
|
||||
}
|
||||
|
@ -35,6 +36,7 @@ fn categorize(category: Category, key: &str) -> String {
|
|||
Category::Block => "blocking",
|
||||
Category::Follow => "following",
|
||||
Category::CatLang => "catlang",
|
||||
Category::RandomIcon => "randomIcon",
|
||||
#[cfg(test)]
|
||||
Category::Test => "usedOnlyForTesting",
|
||||
};
|
||||
|
|
|
@ -1,13 +1,29 @@
|
|||
use identicon_rs::{error::IdenticonError, Identicon};
|
||||
use crate::database::cache;
|
||||
|
||||
pub fn generate(id: &str) -> Result<Vec<u8>, IdenticonError> {
|
||||
Identicon::new(id).set_border(35).export_png_data()
|
||||
#[macros::errors]
|
||||
pub enum Error {
|
||||
#[doc = "failed to generate identicon"]
|
||||
#[error(transparent)]
|
||||
Identicon(#[from] IdenticonError),
|
||||
#[error("Redis cache operation has failed")]
|
||||
Cache(#[from] cache::Error),
|
||||
}
|
||||
|
||||
pub async fn generate(id: &str) -> Result<Vec<u8>, Error> {
|
||||
if let Some(icon) = cache::get_one::<Vec<u8>>(cache::Category::RandomIcon, id).await? {
|
||||
Ok(icon)
|
||||
} else {
|
||||
let icon = Identicon::new(id).set_border(35).export_png_data()?;
|
||||
cache::set_one(cache::Category::RandomIcon, id, &icon, 10 * 60).await?;
|
||||
Ok(icon)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "napi")]
|
||||
#[napi_derive::napi(js_name = "genIdenticon")]
|
||||
pub fn generate_js(id: String) -> napi::Result<napi::bindgen_prelude::Buffer> {
|
||||
match generate(&id) {
|
||||
pub async fn generate_js(id: String) -> napi::Result<napi::bindgen_prelude::Buffer> {
|
||||
match generate(&id).await {
|
||||
Ok(icon) => Ok(icon.into()),
|
||||
Err(err) => Err(napi::Error::from_reason(format!(
|
||||
"\n{}\n",
|
||||
|
|
|
@ -118,7 +118,7 @@ router.get("/avatar/@:acct", async (ctx) => {
|
|||
router.get("/identicon/:x", async (ctx) => {
|
||||
const instanceMeta = await fetchMeta();
|
||||
if (instanceMeta.enableIdenticonGeneration) {
|
||||
const identicon = genIdenticon(ctx.params.x);
|
||||
const identicon = await genIdenticon(ctx.params.x);
|
||||
const [temp, cleanup] = await createTemp();
|
||||
fs.createWriteStream(temp).write(identicon);
|
||||
ctx.set("Content-Type", "image/png");
|
||||
|
|
Loading…
Reference in a new issue