Skip to content

Commit

Permalink
Fix upload error (#2071)
Browse files Browse the repository at this point in the history
* Handle error

* Fix url

* Fix worker url

* Remove log

* Remove env use
  • Loading branch information
harsh-mn-yral authored Jan 5, 2024
1 parent 08953b9 commit 9b07338
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 53 deletions.
3 changes: 1 addition & 2 deletions packages/experiments/src/lib/utils/getDefaultImageUrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const imageHost =
'https://hotornot.wtf/cdn-cgi/imagedelivery/abXI9nS4DYYtyR1yFFtziA'
export const imageHost = 'https://imagedelivery.net/abXI9nS4DYYtyR1yFFtziA'

const cfAvatarImageIds = [
'01452301-3861-4b96-c9ee-b3e443a22300',
Expand Down
3 changes: 1 addition & 2 deletions packages/web-client/src/lib/helpers/image.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Log from '$lib/utils/Log'
import { authState } from '$lib/stores/auth'
import { get } from 'svelte/store'

const cfWorkerHost = import.meta.env.VITE_CLOUDFLARE_WORKERS_API_HOST
import { cfWorkerHost } from './stream'

async function generateUrl() {
try {
Expand Down
3 changes: 1 addition & 2 deletions packages/web-client/src/lib/helpers/signup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Log from '$lib/utils/Log'
import { configuration } from './backend'

const cfWorkerHost = import.meta.env.VITE_CLOUDFLARE_WORKERS_API_HOST
import { cfWorkerHost } from './stream'

export async function checkSignupStatusCf(): Promise<boolean> {
try {
Expand Down
64 changes: 36 additions & 28 deletions packages/web-client/src/lib/helpers/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Log from '$lib/utils/Log'
import { authState } from '$lib/stores/auth'
import { get } from 'svelte/store'

const cfWorkerHost = import.meta.env.VITE_CLOUDFLARE_WORKERS_API_HOST
export const cfWorkerHost =
'https://hot-or-not-upload-api-main.go-bazzinga.workers.dev'

async function generateUrl() {
const authStateData = get(authState)
Expand All @@ -25,37 +26,44 @@ export async function uploadVideoToStream(
file: Blob | File,
onProgress: any,
): Promise<UploadVideoToStream> {
const uploadRes = await generateUrl()
if (!uploadRes || !uploadRes.uploadURL) {
try {
const uploadRes = await generateUrl()
if (!uploadRes || !uploadRes.uploadURL) {
return {
success: false,
errorMessage: "Couldn't generate upload Url. E1",
}
}

return new Promise((resolve) => {
const xhr = new XMLHttpRequest()
xhr.upload.addEventListener('progress', (e) =>
onProgress(e.loaded / e.total),
)
xhr.addEventListener('load', () =>
resolve({ success: true, uid: uploadRes.uid }),
)
xhr.addEventListener('error', (e) =>
resolve({
success: false,
error: e,
errorMessage: 'Something went wrong while uploading file',
}),
)
xhr.addEventListener('abort', () =>
resolve({ success: false, errorMessage: 'Upload cancelled by user' }),
)
xhr.open('POST', uploadRes.uploadURL, true)
const formData = new FormData()
formData.append('file', file)
xhr.send(formData)
})
} catch (e) {
return {
success: false,
errorMessage: "Couldn't generate upload Url",
errorMessage: "Couldn't generate upload Url. E2",
}
}

return new Promise((resolve) => {
const xhr = new XMLHttpRequest()
xhr.upload.addEventListener('progress', (e) =>
onProgress(e.loaded / e.total),
)
xhr.addEventListener('load', () =>
resolve({ success: true, uid: uploadRes.uid }),
)
xhr.addEventListener('error', (e) =>
resolve({
success: false,
error: e,
errorMessage: 'Something went wrong while uploading file',
}),
)
xhr.addEventListener('abort', () =>
resolve({ success: false, errorMessage: 'Upload cancelled by user' }),
)
xhr.open('POST', uploadRes.uploadURL, true)
const formData = new FormData()
formData.append('file', file)
xhr.send(formData)
})
}

export async function checkVideoStatus(uid: string): Promise<CheckVideoStatus> {
Expand Down
4 changes: 1 addition & 3 deletions packages/web-client/src/routes/(feed)/menu/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ onMount(() => {
</a>
{#if !$page.url.host.includes('ic0.app')}
<a
href={`https://${
import.meta.env.VITE_WEBCLIENT_CANISTER_ID
}.raw.ic0.app`}
href="https://vyatz-hqaaa-aaaam-qauea-cai.raw.ic0.app"
class="flex h-12 w-12 items-center justify-center rounded-full border-[1px] border-primary transition-colors duration-200 active:bg-primary">
<Icon name="ic-app" class="h-5 w-5" />
</a>
Expand Down
6 changes: 1 addition & 5 deletions packages/web-client/src/routes/refer-earn/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ $: loggedIn = $authState.isLoggedIn && !$loadingAuthStatus
$: link = !loggedIn
? ''
: $page.url.host.includes('ic0.app')
? `https://${
import.meta.env.VITE_WEBCLIENT_CANISTER_ID
}.raw.ic0.app/profile/${$userProfile.principal_id}?refId=${
$userProfile.principal_id
}&login=true`
? `https://vyatz-hqaaa-aaaam-qauea-cai.raw.ic0.app/profile/${$userProfile.principal_id}?refId=${$userProfile.principal_id}&login=true`
: `https://${$page.url.host}/profile/${$userProfile.principal_id}?refId=${$userProfile.principal_id}&login=true`
</script>

Expand Down
6 changes: 3 additions & 3 deletions packages/web-client/src/routes/upload/new/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ async function startUploading() {
uploadStatus = 'uploading'
const uploadRes = await uploadVideoToStream($fileToUpload, onProgress)
if (!uploadRes.success) {
hashtagError = 'Uploading failed. Please try again'
uploadStatus = 'to-upload'
uploadProgress.set(0)
Log('error', 'Could not start uploading', {
...uploadRes,
from: 'upload-new.startUploading',
Expand All @@ -84,9 +87,6 @@ async function startUploading() {
userId: $userProfile.principal_id,
user_canister_id: $authState.userCanisterId,
})
hashtagError = 'Uploading failed. Please try again'
uploadStatus = 'to-upload'
uploadProgress.set(0)
return
} else if (uploadRes.uid) {
checkVideoProcessingStatus(uploadRes.uid)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { tick } from 'svelte'
import { afterEach, expect, test } from 'vitest'
import Accordion from '../../../src/components/accordion/Accordion.svelte'
import Accordion from '@hnn/components/accordion/Accordion.svelte'

let host: HTMLElement

Expand Down
4 changes: 2 additions & 2 deletions packages/web-client/tests/vitest/components/avatar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, expect, test } from 'vitest'
import Avatar from '../../../src/components/avatar/Avatar.svelte'
import Avatar from '@hnn/components/avatar/Avatar.svelte'

let host: HTMLElement

Expand All @@ -8,7 +8,7 @@ afterEach(() => {
})

const imgSrc =
'https://hotornot.wtf/cdn-cgi/imagedelivery/abXI9nS4DYYtyR1yFFtziA/51434d58-119c-4dc6-ea36-079498eba400/public'
'https://imagedelivery.net/abXI9nS4DYYtyR1yFFtziA/51434d58-119c-4dc6-ea36-079498eba400/public'

test('[Component] Avatar', async () => {
host = document.createElement('div')
Expand Down
2 changes: 1 addition & 1 deletion packages/web-client/tests/vitest/components/button.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, expect, test } from 'vitest'
import Button from '../../../src/components/button/Button.svelte'
import Button from '@hnn/components/button/Button.svelte'

let host: HTMLElement

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { tick } from 'svelte'
import { afterEach, expect, test } from 'vitest'
import InputBox from '../../../src/components/input/InputBox.svelte'
import InputBox from '@hnn/components/input/InputBox.svelte'

let host: HTMLElement

Expand Down
2 changes: 1 addition & 1 deletion packages/web-client/tests/vitest/components/input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { tick } from 'svelte'
import { afterEach, expect, test } from 'vitest'
import Input from '../../../src/components/input/Input.svelte'
import Input from '@hnn/components/input/Input.svelte'

let host: HTMLElement

Expand Down
5 changes: 3 additions & 2 deletions packages/web-client/tests/vitest/e2e/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { describe, it, expect } from 'vitest'
import { HttpAgent } from '@dfinity/agent'
//@ts-ignore
import canisterIds from '../../../../hot-or-not-backend-canister/.dfx/local/canister_ids.json'
import { createActor as createUserIndexActor } from '../../../declarations/user_index/index.js'
import { createActor as createIndividualUserActor } from '../../../declarations/individual_user_template/index.js'
import { createActor as createUserIndexActor } from '@hnn/declarations/user_index/index.js'
import { createActor as createIndividualUserActor } from '@hnn/declarations/individual_user_template/index.js'
import fetch from 'isomorphic-fetch'
import { identity } from './identity'

Expand Down Expand Up @@ -63,6 +63,7 @@ describe('e2e test suite', () => {
const posts = await Promise.all(
cloudflareVideoUid.map((video_uid, i) =>
individualUserActor.add_post_v2({
is_nsfw: false,
description: `Test post #${i + 1}`,
hashtags: ['test', 'video', `${i + 1}`],
video_uid,
Expand Down
1 change: 1 addition & 0 deletions packages/web-client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"./vite.config.ts",
"./src/**/*.js",
"./src/**/*.ts",
"./tests/**/*.ts",
"./hooks/**/*.ts",
"./src/**/*.svelte",
"../components/**/*.ts",
Expand Down

0 comments on commit 9b07338

Please sign in to comment.