Comparing version 0.2.15 to 0.3.0
@@ -11,2 +11,3 @@ { | ||
"promise": "^7.1.1", | ||
"raw-body": "^2.2.0", | ||
"url": "0.10.3" | ||
@@ -46,3 +47,3 @@ }, | ||
"name": "febs", | ||
"version": "0.2.15" | ||
"version": "0.3.0" | ||
} |
150
README.md
@@ -251,3 +251,3 @@ febs 库是一些常用的工具的合集; | ||
*/ | ||
febs.crypt.base64_encode(arrByte) | ||
febs.crypt.base64_encode(arrByte) `客户端` | ||
/** | ||
@@ -257,3 +257,16 @@ * @desc: base64解码. | ||
*/ | ||
febs.crypt.base64_decode(strBase64) | ||
febs.crypt.base64_decode(strBase64) `客户端` | ||
/** | ||
* @desc: 使用上次的解码的数据继续进行base64解码. | ||
* @return: | ||
{ | ||
c1, | ||
c2, | ||
c3, | ||
c4, | ||
data, // 字节数组 | ||
}. | ||
*/ | ||
febs.crypt.base64_decode(strBase64, c2 = 0, c3 = 0, c4 = 0) `服务端` | ||
``` | ||
@@ -333,3 +346,4 @@ # nav | ||
body, // 请求内容. | ||
timeout, // 超时 (ms), 默认为5000 | ||
timeout, // 超时 (ms), 默认为5000, | ||
credentials, // 携带了credentials='include'则服务器需设置Access-Control-Allow-Credentials | ||
} | ||
@@ -513,2 +527,5 @@ * @return: 返回 Promise; | ||
### upload | ||
#### multipart/form-data方式上传. | ||
```js | ||
@@ -584,2 +601,3 @@ /** | ||
<script type="text/javascript" charset="utf-8" src="/jquery/jquery.form.min.js"></script> | ||
<script type="text/javascript" charset="utf-8" src="/febs/febs.min.js"></script> | ||
@@ -607,1 +625,127 @@ <script type="text/javascript"> | ||
``` | ||
#### base64方式上传. | ||
客户端调用如下接口上传文件. | ||
```js | ||
/** | ||
* post方式上传文件. | ||
* 使用文件流片段的方式. 每个片段进行验证.速度稍慢 | ||
* @param cfg: object, 其中 | ||
* { | ||
* data: , // 上传到服务器的任意字符串数据,将在发送请求时发送. | ||
* fileBase64Str: , // 文件的base64格式字符串 | ||
* headerUrl: , // 上传开始前的header请求地址. | ||
* uploadUrl: , // 上传文件内容的url. | ||
* chunkSize: 1024*20, // 每次上传的块大小.默认20kb | ||
* finishCB: , // 上传完成后的回调. function(err, serverData) | ||
* // err: - 'no file' 未选择文件. | ||
* // - 'size too big' 文件太大. | ||
* // - 'check crc32 err' 计算本地文件hash值时错误. | ||
* // - 'ajax err' ajax上传时出错. | ||
* // serverData: 服务器返回的数据. 至少包含一个filename | ||
* progressCB: , // 上传进度的回调. function(percent) | ||
* } | ||
*/ | ||
febs.controls.uploadBase64(cfg); | ||
``` | ||
服务端调用如下接口接收文件. | ||
```js | ||
/** | ||
* 准备接收上传文件. | ||
* @param conditionCB: async function(filesize, data):string. | ||
* - filesize: 将要存储的文件大小. | ||
* - data: 用户上传的数据. | ||
* - return: 本地存储的文件路径, 返回null表示不存储. 存储的文件必须不存在. | ||
* @param session: 用于存储临时文件信息的session. 默认存储在'__uploadSegInfo'中. (在上传内容后验证使用) | ||
* @param sessionKey: 用于存储上传信息的session中的key, 默认为 '__uploadSegInfo' | ||
* @return Promise. | ||
* @resolve | ||
* - bool. 指明是否开始接收文件流. | ||
*/ | ||
febs.controls.uploadBase64.acceptHeader | ||
``` | ||
```js | ||
/** | ||
* 上传文件内容. | ||
* 发生错误会自动调用 cleanup | ||
* @param finishCB: async function(filename):string. | ||
* - filename: 本地存储的文件名. | ||
* - return: 返回给客户端的数据. 不能包含err数据. | ||
* 图片完成上传后, 请转存至其他路径, 稍后系统将清理临时文件. | ||
* @param session: 用于存储临时文件信息的session. 默认存储在'__uploadSegInfo'中. (在上传内容后验证使用) | ||
* @param sessionKey: 用于存储上传信息的session中的key, 默认为 '__uploadSegInfo' | ||
* @return Promise | ||
* @resolve | ||
*/ | ||
febs.controls.uploadBase64.accept | ||
``` | ||
```js | ||
/** | ||
* @desc: 在用户登出或其他中断传输中清除上传的数据. | ||
!!!清理完成后, 将使用 session[sessionKey] = undefined; 清理session. | ||
* @param session: 用于存储临时文件信息的session. 默认存储在'__uploadSegInfo'中. (在上传内容后验证使用) | ||
* @param sessionKey: 用于存储上传信息的session中的key, 默认为 '__uploadSegInfo' | ||
* @return: | ||
*/ | ||
febs.controls.uploadBase64.cleanup | ||
``` | ||
例子 | ||
后台: | ||
```js | ||
// 处理上传请求. | ||
exports.uploadByBase64Header = async function (ctx) { | ||
await febs.controls.uploadBase64.acceptHeader(ctx, | ||
async function(data, filesize){ | ||
return "/tmp/filename.jpg"; | ||
}, function(data){ // set upload sessoin info. | ||
ctx.session.uploadSegInfo = data; | ||
}); | ||
} | ||
// 处理上传片段. | ||
exports.uploadByBase64 = async function (ctx) { | ||
await febs.controls.uploadBase64.accept(ctx, | ||
async function(filename){ | ||
let img = sharp(filename); | ||
let info = await img.metadata(); | ||
return febs.utils.mergeMap(errCode.OK, { width: info.width, height: info.height }); | ||
}, function(){ // get upload session info. | ||
return ctx.session.uploadSegInfo; | ||
}, function(data){ // set upload sessoin info. | ||
ctx.session.uploadSegInfo = data; | ||
}, function() { // clear upload session info. | ||
ctx.session.uploadSegInfo = undefined; | ||
}); | ||
} | ||
``` | ||
前台: | ||
```js | ||
<script type="text/javascript" charset="utf-8" src="/jquery/jquery.min.js"></script> | ||
<script type="text/javascript" charset="utf-8" src="/febs/febs.min.js"></script> | ||
<script type="text/javascript"> | ||
febs.controls.uploadBase64({ | ||
data: {msg :'这是一个用户数据'}, | ||
fileBase64Str: base64Imagestr, | ||
headerUrl: '/api/mgr/uploadimgByBase64Header', | ||
uploadUrl: '/api/mgr/uploadimgByBase64', | ||
finishCB: function(err, serverData) { | ||
if (err) { | ||
console.log('err: '); | ||
console.log(err); | ||
console.log(serverData); | ||
} | ||
else { | ||
console.log('finish: '); | ||
console.log(serverData); | ||
} | ||
}, | ||
progressCB: function(percent) { | ||
console.log(Math.ceil(percent*100)+'%'); | ||
} | ||
}); | ||
</script> | ||
``` |
@@ -1,1 +0,2 @@ | ||
exports.upload = require('./upload'); | ||
exports.upload = require('./upload'); | ||
exports.uploadBase64 = require('./upload.base64'); |
@@ -156,3 +156,4 @@ 'use strict'; | ||
* 接收上传文件内容. | ||
* @param conditionCB: async function(filesize, filename, filemimeType):string. | ||
* @param conditionCB: async function(data, filesize, filename, filemimeType):string. | ||
* - data: 用户上传的数据. | ||
* - filesize: 将要存储的文件大小. | ||
@@ -159,0 +160,0 @@ * - filename: 上传的文件名. |
@@ -140,67 +140,85 @@ 'use strict'; | ||
var base64DecodeChars = new Array( | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, | ||
58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, | ||
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | ||
25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, | ||
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, | ||
-1, -1 | ||
); | ||
/** | ||
* @desc: base64解码. | ||
* @return: 字节数组. | ||
* @desc: 使用上次的解码的数据继续进行base64解码. | ||
* @return: | ||
{ | ||
c1, | ||
c2, | ||
c3, | ||
c4, | ||
data, // 字节数组 | ||
}. | ||
*/ | ||
exports.base64_decode = | ||
function (strBase64){ | ||
var c1, c2, c3, c4; | ||
var base64DecodeChars = new Array( | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
-1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, | ||
58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, | ||
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | ||
25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, | ||
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, | ||
-1, -1 | ||
); | ||
function (strBase64, c1 = 0, c2 = 0, c3 = 0, c4 = 0){ | ||
var i=0, len = strBase64.length, out = []; | ||
while (i < len){ | ||
do{ | ||
c1 = base64DecodeChars[strBase64.charCodeAt(i++) & 0xff] | ||
} while ( | ||
i < len && c1 == -1 | ||
); | ||
if (c1 == -1) break; | ||
// c1_loop: | ||
if (c2 != -1 && c3 != -1 && c4 != -1) { | ||
do{ | ||
c1 = base64DecodeChars[strBase64.charCodeAt(i++) & 0xff] | ||
} while ( | ||
i < len && c1 == -1 | ||
); | ||
do{ | ||
c2 = base64DecodeChars[strBase64.charCodeAt(i++) & 0xff] | ||
} while ( | ||
i < len && c2 == -1 | ||
); | ||
if (c1 == -1) break; | ||
} | ||
if (c2 == -1) break; | ||
// c2_loop: | ||
if (c3 != -1 && c4 != -1) { | ||
do{ | ||
c2 = base64DecodeChars[strBase64.charCodeAt(i++) & 0xff] | ||
} while ( | ||
i < len && c2 == -1 | ||
); | ||
out.push((c1 << 2) | ((c2 & 0x30) >> 4)); | ||
if (c2 == -1) break; | ||
do{ | ||
c3 = strBase64.charCodeAt(i++) & 0xff; | ||
if (c3 == 61) | ||
return out; | ||
out.push((c1 << 2) | ((c2 & 0x30) >> 4)); | ||
} | ||
c3 = base64DecodeChars[c3] | ||
} while ( | ||
i < len && c3 == -1 | ||
); | ||
// c3_loop: | ||
if (c4 != -1) { | ||
do{ | ||
c3 = strBase64.charCodeAt(i++) & 0xff; | ||
if (c3 == 61) | ||
return {data:out}; | ||
if (c3 == -1) break; | ||
c3 = base64DecodeChars[c3] | ||
} while ( | ||
i < len && c3 == -1 | ||
); | ||
out.push(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); | ||
if (c3 == -1) break; | ||
do{ | ||
c4 = strBase64.charCodeAt(i++) & 0xff; | ||
if (c4 == 61) return out; | ||
c4 = base64DecodeChars[c4] | ||
} while ( | ||
i < len && c4 == -1 | ||
); | ||
out.push(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); | ||
} | ||
if (c4 == -1) break; | ||
// c4_loop: | ||
do{ | ||
c4 = strBase64.charCodeAt(i++) & 0xff; | ||
if (c4 == 61) return {data:out}; | ||
c4 = base64DecodeChars[c4] | ||
} while ( | ||
i < len && c4 == -1 | ||
); | ||
out.push(((c3 & 0x03) << 6) | c4) | ||
if (c4 == -1) break; | ||
out.push(((c3 & 0x03) << 6) | c4) | ||
c1 = c2 = c3 = c4 = 0; | ||
} | ||
return out; | ||
return {data:out,c1,c2,c3,c4}; | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
554549
21
6394
745
9
10
+ Addedraw-body@^2.2.0
+ Addedbytes@3.1.2(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedunpipe@1.0.0(transitive)