Skip to content

Commit

Permalink
Merge pull request #224 from decaf-dev/dev
Browse files Browse the repository at this point in the history
* Add social image cache (#223)

* feat: add social image cache

* feat: add cached entry expiration time

* chore: bump version
  • Loading branch information
decaf-dev authored Jul 24, 2024
2 parents 7090207 + d369a18 commit 2a3017a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 7 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "vault-explorer",
"name": "Vault Explorer",
"version": "1.27.2",
"version": "1.28.0",
"minAppVersion": "1.4.13",
"description": "Explore your vault in visual format",
"author": "DecafDev",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-vault-explorer",
"version": "1.27.2",
"version": "1.28.0",
"description": "Explore your vault in visual format",
"main": "main.js",
"scripts": {
Expand Down Expand Up @@ -36,6 +36,7 @@
"typescript": "4.7.4"
},
"dependencies": {
"idb": "^8.0.0",
"js-logger": "^1.6.1",
"lodash": "^4.17.21",
"nanoid": "^5.0.7",
Expand Down
4 changes: 2 additions & 2 deletions src/svelte/app/components/grid-card.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import Icon from "src/svelte/shared/components/icon.svelte";
import { getIconIdForFile } from "../services/file-icon";
import License from "src/svelte/shared/services/license";
import { fetchSocialMediaImage } from "../services/social-media-image";
import { fetchSocialImage } from "../services/social-media-image";
import { PluginEvent } from "src/event/types";
import GridCardContainer from "./grid-card-container.svelte";
import GridCardTitle from "./grid-card-title.svelte";
Expand Down Expand Up @@ -144,7 +144,7 @@
if (!loadSocialMediaImage) return;
if (imageUrl === null && url !== null) {
imgSrc = await fetchSocialMediaImage(url);
imgSrc = await fetchSocialImage(url);
}
}
Expand Down
72 changes: 70 additions & 2 deletions src/svelte/app/services/social-media-image.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,62 @@
import { DBSchema, IDBPDatabase, openDB } from "idb";
import Logger from "js-logger";
import { requestUrl } from "obsidian";

export const fetchSocialMediaImage = async (url: string) => {
const DATABASE_NAME = "vaultexplorer";
const STORE_NAME = "socialMediaImage";
const ENTRY_EXPIRATION_TIME = 1000 * 60 * 60 * 24 * 7; //1 week

interface SocialImageDB extends DBSchema {
socialMediaImage: {
key: string;
value: {
url: string;
socialImageUrl: string;
timestamp: number;
};
};
}

export const clearSocialImageCache = async () => {
Logger.trace({
fileName: "social-media-image.ts",
functionName: "clearSocialMediaImageCache",
message: "called",
});
const db = await openDatabase();
await db.clear(STORE_NAME);
};

export const fetchSocialImage = async (url: string) => {
Logger.trace({
fileName: "social-media-image.ts",
functionName: "fetchSocialMediaImage",
message: "called",
});

try {
const entry = await getCachedSocialImageEntry(url);
if (entry !== null) {
Logger.trace(
{
fileName: "social-media-image.ts",
functionName: "fetchSocialMediaImage",
message: "found cached entry",
},
entry
);
if (Date.now() - entry.timestamp < ENTRY_EXPIRATION_TIME) {
const { socialImageUrl } = entry;
Logger.debug({
fileName: "social-media-image.ts",
functionName: "fetchSocialMediaImage",
message:
"timestamp is within expiration time. returning cached image url",
});
return socialImageUrl;
}
}

const response = await requestUrl({
url,
method: "GET",
Expand All @@ -32,6 +80,7 @@ export const fetchSocialMediaImage = async (url: string) => {
},
{ imageUrl }
);
await putSocialImageUrl(url, imageUrl);
} else {
Logger.warn(
{
Expand All @@ -51,7 +100,7 @@ export const fetchSocialMediaImage = async (url: string) => {
functionName: "fetchSocialMediaImage",
message: "failed to fetch",
},
{ url, error }
error
);
return null;
}
Expand All @@ -63,3 +112,22 @@ const getMetaTagContent = (document: Document, property: string) => {
document.querySelector(`meta[name='${property}']`);
return tag ? tag.getAttribute("content") : "";
};

const putSocialImageUrl = async (url: string, socialImageUrl: string) => {
const db = await openDatabase();
db.put(STORE_NAME, { url, socialImageUrl, timestamp: Date.now() });
};

const getCachedSocialImageEntry = async (url: string) => {
const db = await openDatabase();
const cachedEntry = await db.get(STORE_NAME, url);
return cachedEntry ?? null;
};

const openDatabase = (): Promise<IDBPDatabase<SocialImageDB>> => {
return openDB<SocialImageDB>(DATABASE_NAME, 1, {
upgrade(db) {
db.createObjectStore(STORE_NAME, { keyPath: "url" });
},
});
};
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@
"1.26.3": "1.4.13",
"1.27.0": "1.4.13",
"1.27.1": "1.4.13",
"1.27.2": "1.4.13"
"1.27.2": "1.4.13",
"1.28.0": "1.4.13"
}

0 comments on commit 2a3017a

Please sign in to comment.