Skip to content

Commit ec48bee

Browse files
committed
fixed a bug where I'd get the wrong image url for avatars
1 parent bb50ff3 commit ec48bee

File tree

7 files changed

+20
-18
lines changed

7 files changed

+20
-18
lines changed

.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
DATABASE_URL="postgres://admin:pass123@localhost:7500/cloudkit-db"
66
RATE_LIMIT="ngDPzgdE2apsLdSXLLnQYjbgNkt8ODeX36mq1hnK3NcfmVYwvMrqqkKgHLHNcLdjkcjdaIK9yoAzx0NLf5GLPfYVHbcfWCt83aSet88kgxkAhySGBCwvVuJE4aSRKtOd"
77
# Images with docker-compose for local development
8-
IMAGE_API="http://localhost:7501/image"
8+
PUBLIC_IMAGE_API_URL="http://localhost:7501/image"
99
IMAGE_API_TOKEN="" # Only used in Prod builds
1010
IMAGE_API_ACCOUNT_IDENTIFIER="" # Only used in Prod builds
1111

.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ REDIS_URL="redis://default:redispw@localhost:6379"
1010
REDIS_TOKEN="" # Only used in Prod builds
1111

1212
# Images: Cloudflare
13-
IMAGE_API_TOKEN="" # Only used in Prod builds
14-
IMAGE_API="http://localhost:6501/image"
13+
PUBLIC_IMAGE_API_TOKEN="" # Only used in Prod builds
14+
PUBLIC_IMAGE_API="http://localhost:6501/image"
1515
PUBLIC_IMAGE_DELIVERY="http://localhost:6501/image"
1616

1717
IS_CI=false

.github/workflows/MERGE_MASTER.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ env:
1414
REDIS_TOKEN: ${{ secrets.REDIS_TOKEN }}
1515
REDIS_URL: ${{ secrets.REDIS_URL }}
1616
IMAGE_API_TOKEN: ${{ secrets.IMAGE_API_TOKEN }}
17-
IMAGE_API: ${{ secrets.IMAGE_API }}
17+
PUBLIC_IMAGE_API_URL: ${{ secrets.PUBLIC_IMAGE_API_URL }}
1818
PUBLIC_IMAGE_DELIVERY: ${{ secrets.PUBLIC_IMAGE_DELIVERY }}
1919
IS_CI: true
2020

.github/workflows/PR.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ env:
1212
DATABASE_URL: ${{ secrets.DATABASE_URL }}
1313
DATA_PROXY: ${{ secrets.DATA_PROXY }}
1414
IMAGE_API_TOKEN: ${{ secrets.IMAGE_API_TOKEN }}
15-
IMAGE_API: ${{ secrets.IMAGE_API }}
15+
PUBLIC_IMAGE_API_URL: ${{ secrets.PUBLIC_IMAGE_API_URL }}
1616
PUBLIC_IMAGE_DELIVERY: ${{ secrets.PUBLIC_IMAGE_DELIVERY }}
1717
IMAGE_API_ACCOUNT_IDENTIFIER: ${{ secrets.IMAGE_API_ACCOUNT_IDENTIFIER }}
1818
IS_CI: true

apps/web/src/lib/server/repository/image-repository.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Image } from '@cloudkit/ui-core';
22
import { generateId } from 'lucia';
33

4-
import { IMAGE_API, IMAGE_API_TOKEN } from '$env/static/private';
5-
4+
import { IMAGE_API_TOKEN } from '$env/static/private';
5+
import { PUBLIC_IMAGE_API_URL } from '$env/static/public';
66
import type { CloudflareImageDeleteResponse, UserWithRelations } from '@cloudkit/ui-core';
77
import { isDevOrCi } from '@cloudkit/ui-core';
88
import { db } from './prisma-client';
@@ -63,7 +63,7 @@ class ImageRepository {
6363
await this.postToImageService(image);
6464
const id = generateId(31);
6565

66-
return db.image.create({ data: { id: id, url: `${IMAGE_API}/${id}` } });
66+
return db.image.create({ data: { id: id, url: `${PUBLIC_IMAGE_API_URL}/${id}` } });
6767
}
6868

6969
async deleteFromCloudflare(url: string): Promise<CloudflareImageDeleteResponse> {
@@ -134,13 +134,13 @@ class ImageRepository {
134134
async postToImageService(image: string | File): Promise<{ url: string; id: string }> {
135135
const id = generateId(31);
136136

137-
if (isDevOrCi) {
137+
if (!isDevOrCi) {
138138
try {
139-
await fetch(`${IMAGE_API}/${id}`, {
139+
await fetch(`${PUBLIC_IMAGE_API_URL}/${id}`, {
140140
method: 'PUT',
141141
headers: {
142142
'Content-prefix': 'image/webp',
143-
Slug: `${`${IMAGE_API}/${id}`}.webp`
143+
Slug: `${`${PUBLIC_IMAGE_API_URL}/${id}`}.webp`
144144
},
145145
body: image
146146
});
@@ -149,13 +149,13 @@ class ImageRepository {
149149
console.info('error posting to image service');
150150
}
151151

152-
return { url: `${IMAGE_API}/${id}`, id: id };
152+
return { url: `${PUBLIC_IMAGE_API_URL}/${id}`, id: id };
153153
} else {
154154
const form = new FormData();
155155

156156
form.append('file', new Blob([image]));
157157
form.append('id', id);
158-
const workerResponse = await fetch(IMAGE_API, {
158+
const workerResponse = await fetch(PUBLIC_IMAGE_API_URL, {
159159
method: 'POST',
160160
headers: {
161161
Authorization: `Bearer ${IMAGE_API_TOKEN}`
@@ -164,17 +164,17 @@ class ImageRepository {
164164
});
165165
const response = await workerResponse.json();
166166

167-
return { url: `${IMAGE_API}/${response.result.id}`, id: response.result.id };
167+
return { url: `${PUBLIC_IMAGE_API_URL}/${response.result.id}`, id: response.result.id };
168168
}
169169
}
170170

171171
async patchToImageService(image: File, id: string): Promise<string> {
172172
if (isDevOrCi) {
173-
await fetch(`${IMAGE_API}/${id}`, {
173+
await fetch(`${PUBLIC_IMAGE_API_URL}/${id}`, {
174174
method: 'PUT',
175175
headers: {
176176
'Content-prefix': 'image/webp',
177-
Slug: `${IMAGE_API}/${id}.webp`
177+
Slug: `${PUBLIC_IMAGE_API_URL}/${id}.webp`
178178
},
179179
body: image
180180
});
@@ -196,7 +196,7 @@ class ImageRepository {
196196

197197
form.append('file', new Blob([image]));
198198
form.append('id', id);
199-
const workerResponse = await fetch(IMAGE_API, {
199+
const workerResponse = await fetch(PUBLIC_IMAGE_API_URL, {
200200
method: 'POST',
201201
headers: {
202202
Authorization: `Bearer ${IMAGE_API_TOKEN}`

packages/db-schema/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"push:prod": "dotenv -e ../../.env.production -- prisma db push --schema ./prisma/schema.prisma",
1212
"push:manual": "dotenv -e ../../.env.development -- prisma db push --schema ./prisma/schema.prisma && pnpm format",
1313
"studio": "dotenv -e ../../.env.development -- prisma studio --schema ./prisma/schema.prisma && pnpm format",
14+
"studio:prod": "dotenv -e ../../.env.production -- prisma studio --schema ./prisma/schema.prisma && pnpm format",
1415
"format": "prettier --config ../../.prettierrc --write ."
1516
},
1617
"dependencies": {

packages/ui-core/src/components/media/avatar.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script lang="ts">
2+
import { PUBLIC_IMAGE_API_URL } from '$env/static/public';
23
import { Avatar } from '@skeletonlabs/skeleton';
34
export let width = '';
45
export let src: string;
@@ -9,7 +10,7 @@
910
export let updatedAt: Date;
1011
$: url = isBase64
1112
? src
12-
: `${src}${src.includes('img.cloudkit.fyi') ? (isLarge ? '/lg' : '/sm') : ''}`;
13+
: `${src}${src.includes(PUBLIC_IMAGE_API_URL) ? (isLarge ? '/lg' : '/sm') : ''}`;
1314
</script>
1415

1516
{#if src !== null}

0 commit comments

Comments
 (0)