Skip to content

feat: add new functions for mp image upload #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ components.d.ts

.wxt
.output
web-ext.config.ts
web-ext.config.ts
.wrangler
1 change: 1 addition & 0 deletions docker/latest/patch/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default defineConfig({
define: {
process,
},
envPrefix: [`VITE_`, `CF_`], // 允许 VITE_ 和 CF_ 前缀的变量
plugins: [
vue(),
UnoCSS(),
Expand Down
16 changes: 16 additions & 0 deletions functions/cgi-bin/material/add_material/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { parseFormDataRequest } from '../../../parseFormDataRequest'
import { jsonResponse, MP_HOST } from '../../../utils'

export const onRequestPost: PagesFunction = async (context) => {
const formData = (await parseFormDataRequest(context.request)) as FormData
const url = new URL(context.request.url)
const response = await fetch(
`${MP_HOST}${context.functionPath}${url.search}`,
{
method: `POST`,
body: formData,
},
)
const json = await response.json()
return jsonResponse(json)
}
5 changes: 5 additions & 0 deletions functions/cgi-bin/media/uploadimg/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { onRequestPost as post } from '../../material/add_material/index'

export const onRequestPost: PagesFunction = async (context) => {
return post(context)
}
13 changes: 13 additions & 0 deletions functions/cgi-bin/stable_token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { jsonResponse, MP_HOST } from '../utils'

export const onRequestPost: PagesFunction = async (context) => {
const response = await fetch(
`${MP_HOST}${context.functionPath}`,
{
method: `POST`,
body: context.request.body,
},
)
const json = await response.json()
return jsonResponse(json)
}
40 changes: 40 additions & 0 deletions functions/parseFormDataRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// eslint-disable-next-line ts/ban-ts-comment
// @ts-nocheck
// @see https://github.com/cloudflare/images.pages.dev/blob/main/functions/utils/parseFormDataRequest.ts
import { parseMultipart } from '@ssttevee/multipart-parser'

const RE_MULTIPART
= /^multipart\/form-data;\s*boundary=(?:"((?:[^"]|\\")+)"|([^\s;]+))$/

function getBoundary(request: Request): string | undefined {
const contentType = request.headers.get(`Content-Type`)
if (!contentType)
return

const matches = RE_MULTIPART.exec(contentType)
if (!matches)
return

return matches[1] || matches[2]
}

export async function parseFormDataRequest(request: Request): Promise<FormData | undefined> {
const boundary = getBoundary(request)
if (!boundary || !request.body)
return

const parts = await parseMultipart(request.body, boundary)

const formData = new FormData()

for (const { name, data, filename, contentType } of parts) {
formData.append(
name,
filename
? new File([data], filename, { type: contentType })
: new TextDecoder().decode(data),
)
}

return formData
}
8 changes: 8 additions & 0 deletions functions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "esnext",
"lib": ["esnext"],
"module": "esnext",
"types": ["@cloudflare/workers-types"]
}
}
8 changes: 8 additions & 0 deletions functions/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const MP_HOST = `https://api.weixin.qq.com`

export function jsonResponse(value: any, init: ResponseInit = {}) {
return new Response(JSON.stringify(value), {
headers: { 'Content-Type': `application/json`, ...init.headers },
...init,
})
}
Loading