Skip to content

Commit cb2e0dc

Browse files
author
pedro
committed
feat:修复文件上传的大小缺失bug
1 parent cb1b74e commit cb2e0dc

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

app/extensions/file/local-uploader.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@ const { File } = require('lin-mizar');
33
const { config } = require('lin-mizar/lin/config');
44
const fs = require('fs');
55
const path = require('path');
6-
const { cloneDeep } = require('lodash');
76

87
class LocalUploader extends Uploader {
98
/**
10-
* 处理文件流
11-
* @param {object[]} files 文件流数组
9+
* 处理文件对象
10+
* { size, encoding, fieldname, filename, mimeType, data }
1211
*/
1312
async upload (files) {
1413
const arr = [];
15-
for (const stream of files) {
14+
for (const file of files) {
1615
// 由于stream的特性,当读取其中的数据时,它的buffer会被消费
1716
// 所以此处深拷贝一份计算md5值
18-
const tmpStream = cloneDeep(stream);
19-
const md5 = this.generateMd5(tmpStream);
17+
const md5 = this.generateMd5(file);
2018
const siteDomain = config.getItem('siteDomain', 'http://localhost');
2119
// 检查md5存在
2220
const exist = await File.findOne({
@@ -26,31 +24,30 @@ class LocalUploader extends Uploader {
2624
});
2725
if (exist) {
2826
arr.push({
29-
key: stream.fieldname,
27+
key: file.fieldname,
3028
id: exist.id,
3129
url: `${siteDomain}/assets/${exist.path}`
3230
});
3331
} else {
3432
const { absolutePath, relativePath, realName } = this.getStorePath(
35-
stream.filename
33+
file.filename
3634
);
3735
const target = fs.createWriteStream(absolutePath);
38-
await stream.pipe(target);
36+
await target.write(file.data);
3937
const ext = path.extname(realName);
40-
// stream.filename tream.filedname stream.mimeType stream.readableLength
4138
const saved = await File.createRecord(
4239
{
4340
path: relativePath,
4441
// type: 1,
4542
name: realName,
4643
extension: ext,
47-
size: stream._readableState.length,
44+
size: file.size,
4845
md5: md5
4946
},
5047
true
5148
);
5249
arr.push({
53-
key: stream.fieldname,
50+
key: file.fieldname,
5451
id: saved.id,
5552
url: `${siteDomain}/assets/${saved.path}`
5653
});

0 commit comments

Comments
 (0)