Skip to content

Commit

Permalink
feat: 云函数支持文件上传,支持 headers 参数传入;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jun 11, 2021
1 parent 762f92c commit f0c04ab
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
29 changes: 28 additions & 1 deletion src/lib/faas/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FunctionConsole } from "./console"
import { AxiosStatic } from 'axios'
import { Db } from 'less-api-database'
import { FileStorageInterface } from "../storage/interface"
import { IncomingHttpHeaders } from "node:http"


export type RequireFuncType = (module: 'crypto' | 'path' | 'querystring' | 'url' | 'lodash' | 'moment') => any
Expand Down Expand Up @@ -31,11 +32,13 @@ export interface RuntimeContext {

// ctx passed to function
export interface FunctionContext {
files?: File[]
headers?: IncomingHttpHeaders
query?: any,
body?: any,
params?: any,
auth?: any,
requestId?: string,
extra?: any,
method?: string
}

Expand Down Expand Up @@ -67,4 +70,28 @@ export interface CloudFunctionStruct {
created_by: number,
created_at: number
updated_at: number
}

/** Object containing file metadata and access information. */
interface File {
/** Name of the form field associated with this file. */
fieldname: string
/** Name of the file on the uploader's computer. */
originalname: string
/**
* Value of the `Content-Transfer-Encoding` header for this file.
* @deprecated since July 2015
* @see RFC 7578, Section 4.7
*/
encoding: string
/** Value of the `Content-Type` header for this file. */
mimetype: string
/** Size of the file in bytes. */
size: number
/** `DiskStorage` only: Directory to which this file has been uploaded. */
destination: string
/** `DiskStorage` only: Name of this file within `destination`. */
filename: string
/** `DiskStorage` only: Full path to the uploaded file. */
path: string
}
27 changes: 24 additions & 3 deletions src/router/function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@ import { checkPermission } from '../../lib/api/permission'
import { getLogger } from '../../lib/logger'
import { getCloudFunction, invokeFunction } from '../../lib/faas/invoke'
import { FunctionContext } from '../../lib/faas/types'
import * as multer from 'multer'
import * as path from 'path'
import * as uuid from 'uuid'


export const FunctionRouter = Router()
const logger = getLogger('admin:api')

// multer 上传配置
const uploader = multer({
storage: multer.diskStorage({
filename: (_req, file, cb) => {
const { ext } = path.parse(file.originalname)
cb(null, uuid.v4() + ext)
}
})
})

FunctionRouter.post('/invoke/:name', handleInvokeFunction)
FunctionRouter.all('/:name', handleInvokeFunction) // alias for /invoke/:name
FunctionRouter.post('/invoke/:name', uploader.any(), handleInvokeFunction)
FunctionRouter.all('/:name', uploader.any(), handleInvokeFunction) // alias for /invoke/:name

async function handleInvokeFunction(req: Request, res: Response) {
const requestId = req['requestId']
Expand Down Expand Up @@ -41,7 +54,15 @@ async function handleInvokeFunction(req: Request, res: Response) {
}

// 调用函数
const ctx: FunctionContext = { query: req.query, body: req.body, auth: req['auth'], requestId, method: req.method }
const ctx: FunctionContext = {
query: req.query,
files: req.files as any,
body: req.body,
headers: req.headers,
method: req.method,
auth: req['auth'],
requestId,
}
const result = await invokeFunction(func, ctx)

// 将云函数调用日志存储到数据库
Expand Down

0 comments on commit f0c04ab

Please sign in to comment.