Skip to content

Commit

Permalink
Merge pull request #473 from hearchco/as/feat/timestamped-proxy
Browse files Browse the repository at this point in the history
feat(proxy): make hashes expire after time
  • Loading branch information
aleksasiriski authored Sep 2, 2024
2 parents ef9cc5c + a03a689 commit cd50337
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/results/general/single.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
const favicon =
result.favicon_hash && result.favicon_hash != ''
? proxyFaviconLink(result.url, result.favicon_hash)
? proxyFaviconLink(result.url, result.favicon_hash, result.favicon_hash_timestamp)
: Logo;
const shortDesc =
Expand Down
6 changes: 5 additions & 1 deletion src/lib/components/results/images/preview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
<a href={result.url}>
<img
id="link-{result.rank}"
src={proxyImageLink(result.thumbnail_url, result.thumbnail_url_hash)}
src={proxyImageLink(
result.thumbnail_url,
result.thumbnail_url_hash,
result.thumbnail_url_hash_timestamp
)}
alt={result.title}
class="max-h-[40dvh] w-full object-contain bg-neutral-100 dark:bg-neutral-800 rounded-lg"
/>
Expand Down
6 changes: 5 additions & 1 deletion src/lib/components/results/images/single.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
<img
id="img-{result.rank}"
class="size-full object-cover object-center"
src={proxyImageLink(result.thumbnail_url, result.thumbnail_url_hash)}
src={proxyImageLink(
result.thumbnail_url,
result.thumbnail_url_hash,
result.thumbnail_url_hash_timestamp
)}
alt={result.title}
{loading}
/>
Expand Down
11 changes: 7 additions & 4 deletions src/lib/functions/api/proxyimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { concatSearchParams } from './concatparams';
* Create a public API URL for the proxy image endpoint.
* @param {string} url
* @param {string} hash
* @param {string} timestamp
* @param {boolean} [favicon]
* @returns {string}
*/
export function proxyImageLink(url, hash, favicon = false) {
export function proxyImageLink(url, hash, timestamp, favicon = false) {
const params = concatSearchParams([
['hash', hash],
['url', url],
['hash', hash],
['timestamp', timestamp],
['favicon', favicon.toString()]
]);

Expand All @@ -32,9 +34,10 @@ export function proxyImageLink(url, hash, favicon = false) {
* Create a public API URL for the proxy favicon image endpoint.
* @param {string} url
* @param {string} hash
* @param {string} timestamp
* @returns {string}
*/
export function proxyFaviconLink(url, hash) {
export function proxyFaviconLink(url, hash, timestamp) {
const uriPattern = '^(http(s?))(://)([^/]+)';
const uriRegex = new RegExp(uriPattern);
const uriMatch = url.match(uriRegex);
Expand All @@ -44,5 +47,5 @@ export function proxyFaviconLink(url, hash) {
}

const uri = uriMatch[0];
return proxyImageLink(uri, hash, true);
return proxyImageLink(uri, hash, timestamp, true);
}
5 changes: 4 additions & 1 deletion src/lib/types/search/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
* Describes the properties of a search result.
* @typedef {Object} ResultType
* @property {string} url - URL of the result.
* @property {string} url_hash - Hash of the URL.
* @property {string} url_hash - Hash of the URL (used only for images).
* @property {string} url_hash_timestamp - Timestamp of the URL hash (used only for images).
* @property {number} rank - The rank of the result.
* @property {number} score - The score of the result.
* @property {string} title - The title of the result.
* @property {string} description - The description of the result.
* @property {EngineRankType[]} engine_ranks - Rankings on different search engines.
* @property {string} favicon_hash - Hash required for proxying the favicon.
* @property {string} favicon_hash_timestamp - Timestamp of the favicon hash.
* ImageResultType
* @property {ImageFormatType} original - The original image format details.
* @property {ImageFormatType} thumbnail - The thumbnail image format details.
* @property {string} thumbnail_url - URL to the thumbnail image.
* @property {string} thumbnail_url_hash - Hash of the thumbnail URL.
* @property {string} thumbnail_url_hash_timestamp - Timestamp of the thumbnail URL hash.
* @property {string} source - The source of the image.
* @property {string} source_url - URL of the image source.
*/
Expand Down

0 comments on commit cd50337

Please sign in to comment.