kopia lustrzana https://github.com/cloudflare/wildebeest
parse form data locally in specific cases
In order to work around an EW bug, use local form data parsing temporarly when receiving media.pull/135/head
rodzic
68526d7530
commit
43c6fd534f
|
@ -2,6 +2,7 @@
|
||||||
// can be url encoded, form data or JSON. However, not working for formData
|
// can be url encoded, form data or JSON. However, not working for formData
|
||||||
// containing binary data (like File).
|
// containing binary data (like File).
|
||||||
export async function readBody<T>(request: Request): Promise<T> {
|
export async function readBody<T>(request: Request): Promise<T> {
|
||||||
|
let form = null
|
||||||
const contentType = request.headers.get('content-type')
|
const contentType = request.headers.get('content-type')
|
||||||
if (contentType === null) {
|
if (contentType === null) {
|
||||||
throw new Error('invalid request')
|
throw new Error('invalid request')
|
||||||
|
@ -13,6 +14,25 @@ export async function readBody<T>(request: Request): Promise<T> {
|
||||||
contentType.includes('multipart/form-data') &&
|
contentType.includes('multipart/form-data') &&
|
||||||
contentType.includes('boundary')
|
contentType.includes('boundary')
|
||||||
) {
|
) {
|
||||||
|
form = await localFormDataParse(request)
|
||||||
|
} else {
|
||||||
|
form = await request.formData()
|
||||||
|
}
|
||||||
|
|
||||||
|
const out: any = {}
|
||||||
|
|
||||||
|
for (const [key, value] of form) {
|
||||||
|
out[key] = value
|
||||||
|
}
|
||||||
|
return out as T
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function localFormDataParse(request: Request): Promise<FormData> {
|
||||||
|
const contentType = request.headers.get('content-type')
|
||||||
|
if (contentType === null) {
|
||||||
|
throw new Error('invalid request')
|
||||||
|
}
|
||||||
|
|
||||||
console.log('will attempt local parse of form data')
|
console.log('will attempt local parse of form data')
|
||||||
const rBody = await request.text()
|
const rBody = await request.text()
|
||||||
const enc = new TextEncoder()
|
const enc = new TextEncoder()
|
||||||
|
@ -28,20 +48,7 @@ export async function readBody<T>(request: Request): Promise<T> {
|
||||||
form.append(part.name || 'null', value)
|
form.append(part.name || 'null', value)
|
||||||
}
|
}
|
||||||
|
|
||||||
const out: any = {}
|
return form
|
||||||
for (const [key, value] of form) {
|
|
||||||
out[key] = value
|
|
||||||
}
|
|
||||||
return out as T
|
|
||||||
} else {
|
|
||||||
const form = await request.formData()
|
|
||||||
const out: any = {}
|
|
||||||
|
|
||||||
for (const [key, value] of form) {
|
|
||||||
out[key] = value
|
|
||||||
}
|
|
||||||
return out as T
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// temporary code to deal with EW bug
|
// temporary code to deal with EW bug
|
||||||
|
|
|
@ -5,6 +5,7 @@ import * as media from 'wildebeest/backend/src/media/image'
|
||||||
import type { ContextData } from 'wildebeest/backend/src/types/context'
|
import type { ContextData } from 'wildebeest/backend/src/types/context'
|
||||||
import type { MediaAttachment } from 'wildebeest/backend/src/types/media'
|
import type { MediaAttachment } from 'wildebeest/backend/src/types/media'
|
||||||
import type { Person } from 'wildebeest/backend/src/activitypub/actors'
|
import type { Person } from 'wildebeest/backend/src/activitypub/actors'
|
||||||
|
import { localFormDataParse } from 'wildebeest/backend/src/utils/body'
|
||||||
|
|
||||||
export const onRequest: PagesFunction<Env, any, ContextData> = async ({ request, env, data }) => {
|
export const onRequest: PagesFunction<Env, any, ContextData> = async ({ request, env, data }) => {
|
||||||
return handleRequest(request, env.DATABASE, data.connectedActor, env.CF_ACCOUNT_ID, env.CF_API_TOKEN)
|
return handleRequest(request, env.DATABASE, data.connectedActor, env.CF_ACCOUNT_ID, env.CF_API_TOKEN)
|
||||||
|
@ -18,7 +19,22 @@ export async function handleRequest(
|
||||||
accountId: string,
|
accountId: string,
|
||||||
apiToken: string
|
apiToken: string
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
const formData = await request.formData()
|
const contentType = request.headers.get('content-type')
|
||||||
|
if (contentType === null) {
|
||||||
|
throw new Error('invalid request')
|
||||||
|
}
|
||||||
|
|
||||||
|
let formData = null
|
||||||
|
|
||||||
|
if (
|
||||||
|
contentType.includes('charset') &&
|
||||||
|
contentType.includes('multipart/form-data') &&
|
||||||
|
contentType.includes('boundary')
|
||||||
|
) {
|
||||||
|
formData = await localFormDataParse(request)
|
||||||
|
} else {
|
||||||
|
formData = await request.formData()
|
||||||
|
}
|
||||||
const domain = new URL(request.url).hostname
|
const domain = new URL(request.url).hostname
|
||||||
|
|
||||||
if (!formData.has('file')) {
|
if (!formData.has('file')) {
|
||||||
|
|
Ładowanie…
Reference in New Issue