feat: FINALLY i fixed image posting

This commit is contained in:
Xyphyn committed 2023-07-05 16:11:11 -07:00
1 parent dd30acf19d
commit 948ad6fb1c
2 files changed
+38 -14

No files matched your search

+16 -2
View File
@@ -23,10 +23,23 @@ export async function handle({ event, resolve }) {
try { try {
const url = buildUrl(event.url) const url = buildUrl(event.url)
let headers: Headers = event.request.headers
headers.delete('origin')
headers.delete('host')
if (
event.request.method == 'POST' &&
url.pathname == '/pictrs/image' &&
url.searchParams.get('auth')
) {
headers.set('cookie', `jwt=${url.searchParams.get('auth')}`)
}
const data = await fetch(url, { const data = await fetch(url, {
method: event.request.method, method: event.request.method,
headers: headers,
body: event.request.body, body: event.request.body,
headers: event.request.headers,
// @ts-ignore this works, idk why typescript complains // @ts-ignore this works, idk why typescript complains
duplex: 'half', duplex: 'half',
signal: AbortSignal.timeout(20 * 1000), signal: AbortSignal.timeout(20 * 1000),
@@ -60,12 +73,13 @@ export async function handle({ event, resolve }) {
status: data.status, status: data.status,
}) })
} catch (error) { } catch (error) {
console.log(error)
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
message: 'the proxy failed to fetch from server', message: 'the proxy failed to fetch from server',
}), }),
{ {
status: 200, status: 500,
} }
) )
} }
+22 -12
View File
@@ -29,24 +29,30 @@
} }
async function uploadImage( async function uploadImage(
image: FileList | null | undefined image: File | null | undefined
): Promise<string | undefined> { ): Promise<string | undefined> {
if (!image || !$authData) return if (!image || !$authData) return
try { const formData = new FormData()
const res = await getClient().uploadImage({ formData.append('images[]', image)
image: image[0],
const response = await fetch(
`${buildBaseUrl($authData.instance)}/pictrs/image?${new URLSearchParams({
auth: $authData.token, auth: $authData.token,
}) })}`,
{
if (!res.url) { method: 'POST',
throw new Error('Failed to upload image') body: formData,
} }
)
return res.url const json = await response.json()
} catch (err) {
throw new Error('Failed to upload image.') if (json.msg == 'ok') {
return `https://${$authData.instance}/pictrs/image/${json.files?.[0]?.file}`
} }
throw new Error(`error uploading image: ${response.status}`)
} }
async function submit() { async function submit() {
@@ -55,7 +61,7 @@
data.loading = true data.loading = true
try { try {
let image = data.image ? await uploadImage(data.image) : undefined let image = data.image ? await uploadImage(data.image[0]) : undefined
const post = await getClient().createPost({ const post = await getClient().createPost({
auth: $authData.token, auth: $authData.token,
@@ -82,6 +88,10 @@
'A cool photo I took before the destruction of mankind!', 'A cool photo I took before the destruction of mankind!',
'AITA for ending all contention and causing world peace?', 'AITA for ending all contention and causing world peace?',
'BREAKING NEWS: Police break into gun shop, find weapons', 'BREAKING NEWS: Police break into gun shop, find weapons',
'How do I make sure my pet rock stays alive?',
'My cat just invented a time machine, what do I do?',
'Unpopular opinion: world peace and global happiness would be beneficial to humanity',
'LPT: The smaller weights are easier to lift than the bigger weights in a gym',
] ]
const placeholder = const placeholder =