-
-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |