Skip to content

Commit

Permalink
feat: 新增文件上传 built-in 云函数;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Jun 21, 2021
1 parent 405dfce commit 9834018
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
33 changes: 33 additions & 0 deletions init/functions/file-upload/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import * as path from 'path'

exports.main = async function (ctx) {

// 非登录用户不予上传
const uid = ctx.auth?.uid
if (!uid) {
return { error: 'unauthorized'}
}

// 文件不可为空
const files = ctx?.files
if (!files?.length) {
return { error: 'file cannot be empty' }
}

const file = files[0]

// namespace 只可为数字或字母组合,长度不得长于 32 位
const namespace = 'public'

// 存储上传文件
const storage = less.storage(namespace)

const filepath = path.join(file.destination, `${file.filename}`)
const info = await storage.saveFile(filepath)

// 不得暴露全路径给客户端
delete info.fullpath

return info
}
6 changes: 6 additions & 0 deletions init/functions/file-upload/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"label": "文件上传",
"name": "file-upload",
"description": "文件上传,请使用 postman/curl 等HTTP工具测试此函数;默认上传至 public 空间下,可自行定义上传逻辑",
"enableHTTP": true
}

0 comments on commit 9834018

Please sign in to comment.