Skip to content

Commit

Permalink
feat(images): proxy (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksasiriski authored Mar 21, 2024
1 parent 4b8ebd7 commit d22673c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
API_URI=http://localhost:3030 # server reachable
PUBLIC_API_URI=http://localhost:3030 # client reachable
# use local frontend with official backend (CORS is enabled for http://localhost:5173)
# API_URI=https://api.hearch.co
# PUBLIC_API_URI=https://api.hearch.co
4 changes: 3 additions & 1 deletion src/lib/components/search/display/images/Image.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script lang="ts">
import { proxyImageLink } from '$lib/functions/proxyImageLink';
// types
import type { ResultType } from '$lib/types/result';
Expand Down Expand Up @@ -31,7 +33,7 @@
<img
id="img-{result.Rank}"
class="w-full h-full object-cover object-center transform hover:scale-110 transition duration-300 ease-in-out"
src={result.ImageResult.ThumbnailURL}
src={proxyImageLink(result.ImageResult.ThumbnailURL, result.ImageResult.ThumbnailURLHash)}
alt={result.Title}
{loading}
/>
Expand Down
4 changes: 3 additions & 1 deletion src/lib/components/search/display/images/Preview.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script lang="ts">
import { proxyImageLink } from '$lib/functions/proxyImageLink';
// types
import type { ResultType } from '$lib/types/result';
Expand All @@ -13,7 +15,7 @@
>
<img
id="link-{result.Rank}"
src={result.ImageResult.ThumbnailURL}
src={proxyImageLink(result.URL, result.URLHash)}
alt={result.Title}
class="h-full w-full object-contain transform transition"
/>
Expand Down
10 changes: 10 additions & 0 deletions src/lib/functions/createPublicApiUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { env } from '$env/dynamic/public';

export function createPublicApiUrl(path: string, params?: string): string {
const apiUri: string | undefined = env.PUBLIC_API_URI;
if (apiUri === undefined) {
throw new Error('PUBLIC_API_URI env is not defined');
}

return (apiUri.endsWith('/') ? apiUri : apiUri + '/') + path + (params ? `?${params}` : '');
}
16 changes: 16 additions & 0 deletions src/lib/functions/proxyImageLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { error } from '@sveltejs/kit';
import { createPublicApiUrl } from '$lib/functions/createPublicApiUrl';

export function proxyImageLink(url: string, hash: string): string {
const params: string = 'url=' + encodeURIComponent(url) + '&hash=' + hash;

let apiUrl: string;
try {
apiUrl = createPublicApiUrl('proxy', params);
} catch (err: any) {
// Internal Server Error
throw error(500, `Failed to create API URL: ${err.message}`);
}

return apiUrl;
}
2 changes: 2 additions & 0 deletions src/lib/types/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ type ImageResult = {
Original: ImageFormat;
Thumbnail: ImageFormat;
ThumbnailURL: string;
ThumbnailURLHash: string;
Source: string;
SourceURL: string;
};

export type ResultType = {
URL: string;
URLHash: string;
Rank: number;
Score: number;
Title: string;
Expand Down

0 comments on commit d22673c

Please sign in to comment.