Skip to content

Commit 16e61ec

Browse files
committed
using new file streaming in 0.15
1 parent 4c87203 commit 16e61ec

File tree

3 files changed

+48
-66
lines changed

3 files changed

+48
-66
lines changed

examples/file/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ broker.createService({
4343
},
4444

4545
aliases: {
46+
"GET /:file": "file.get",
47+
4648
// File upload from HTML form
4749
"POST /": "multipart:file.save",
4850

examples/formidable/index.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ broker.createService({
4545
},
4646

4747
aliases: {
48+
"GET /:file": "file.get",
49+
4850
// File upload from AJAX or cURL
4951
"PUT /": "stream:file.save",
5052

@@ -91,15 +93,11 @@ broker.createService({
9193
const ctx = req.$ctx;
9294
const entries = Array.isArray(files.myfile) ? files.myfile : [files.myfile];
9395
const result = await Promise.all(entries.map(entry => {
94-
return ctx.call("file.save", fs.createReadStream(entry.path), {
95-
meta: {
96-
filename: entry.name,
97-
$params: {
98-
...req.params,
99-
...fields,
100-
}
101-
}
102-
});
96+
return ctx.call("file.save", {
97+
$filename: entry.name,
98+
...req.params,
99+
...fields,
100+
}, { stream: fs.createReadStream(entry.path) });
103101
}));
104102

105103
return this.sendResponse(req, res, Array.isArray(files.myfile) ? result : result[0]);

test/integration/index.spec.js

+39-57
Original file line numberDiff line numberDiff line change
@@ -3615,16 +3615,16 @@ describe("Test file uploading", () => {
36153615
actions: {
36163616
save(ctx) {
36173617
return new this.Promise((resolve) => {
3618-
if (ctx.params.pipe) {
3618+
if (ctx.stream && ctx.stream.pipe) {
36193619
const hash = crypto.createHash("sha256");
36203620

36213621
hash.on("readable", () => {
36223622
const data = hash.read();
36233623
if (data)
3624-
resolve({ hash: data.toString("base64"), meta: ctx.meta });
3624+
resolve({ hash: data.toString("base64"), params: ctx.params, meta: ctx.meta });
36253625
});
36263626

3627-
ctx.params.pipe(hash);
3627+
ctx.stream.pipe(hash);
36283628
} else {
36293629
resolve({ params: ctx.params, meta: ctx.meta });
36303630
}
@@ -3722,14 +3722,12 @@ describe("Test file uploading", () => {
37223722
.then(res => {
37233723
expect(res.statusCode).toBe(200);
37243724
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
3725-
expect(res.body).toEqual({ hash: origHashes["logo.png"], meta: {
3726-
$multipart: {},
3727-
$params: {},
3728-
encoding: "7bit",
3729-
fieldname: "myFile",
3730-
filename: "logo.png",
3731-
mimetype: "image/png"
3732-
} });
3725+
expect(res.body).toEqual({ hash: origHashes["logo.png"], params: {
3726+
$encoding: "7bit",
3727+
$fieldname: "myFile",
3728+
$filename: "logo.png",
3729+
$mimetype: "image/png"
3730+
}, meta: {} });
37333731

37343732
expect(onFilesLimitFn).toHaveBeenCalledTimes(0);
37353733
});
@@ -3738,21 +3736,18 @@ describe("Test file uploading", () => {
37383736
it("should send data field with multipart", () => {
37393737
return request(server)
37403738
.post("/upload")
3741-
.attach("myFile", assetsDir + "logo.png")
37423739
.field("name", "moleculer")
3740+
.attach("myFile", assetsDir + "logo.png")
37433741
.then(res => {
37443742
expect(res.statusCode).toBe(200);
37453743
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
3746-
expect(res.body).toEqual({ hash: origHashes["logo.png"], meta: {
3747-
$multipart: {
3748-
name: "moleculer"
3749-
},
3750-
$params: {},
3751-
encoding: "7bit",
3752-
fieldname: "myFile",
3753-
filename: "logo.png",
3754-
mimetype: "image/png"
3755-
} });
3744+
expect(res.body).toEqual({ hash: origHashes["logo.png"], params: {
3745+
name: "moleculer",
3746+
$encoding: "7bit",
3747+
$fieldname: "myFile",
3748+
$filename: "logo.png",
3749+
$mimetype: "image/png"
3750+
}, meta: {} });
37563751

37573752
expect(onFilesLimitFn).toHaveBeenCalledTimes(0);
37583753
});
@@ -3767,11 +3762,8 @@ describe("Test file uploading", () => {
37673762
expect(res.statusCode).toBe(200);
37683763
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
37693764
expect(res.body).toEqual([{
3770-
meta: {
3771-
$multipart: { name: "moleculer", more: "services" },
3772-
$params: { id: "f1234" }
3773-
},
3774-
params: {}
3765+
meta: {},
3766+
params: { id: "f1234", name: "moleculer", more: "services" }
37753767
}]);
37763768
});
37773769
});
@@ -3784,14 +3776,12 @@ describe("Test file uploading", () => {
37843776
.then(res => {
37853777
expect(res.statusCode).toBe(200);
37863778
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
3787-
expect(res.body).toEqual({ hash: origHashes["logo.png"], meta: {
3788-
$multipart: {},
3789-
$params: {},
3790-
encoding: "7bit",
3791-
fieldname: "myFile",
3792-
filename: "logo.png",
3793-
mimetype: "image/png"
3794-
} });
3779+
expect(res.body).toEqual({ hash: origHashes["logo.png"], params: {
3780+
$encoding: "7bit",
3781+
$fieldname: "myFile",
3782+
$filename: "logo.png",
3783+
$mimetype: "image/png"
3784+
} , meta: {} });
37953785
expect(onFilesLimitFn).toHaveBeenCalledTimes(1);
37963786
expect(onFilesLimitFn).toHaveBeenCalledWith(expect.any(Busboy), expect.any(Alias), service);
37973787
});
@@ -3806,22 +3796,18 @@ describe("Test file uploading", () => {
38063796
expect(res.statusCode).toBe(200);
38073797
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
38083798
expect(res.body).toEqual([
3809-
{ hash: origHashes["logo.png"], meta: {
3810-
$multipart: {},
3811-
$params: {},
3812-
encoding: "7bit",
3813-
fieldname: "myFile",
3814-
filename: "logo.png",
3815-
mimetype: "image/png"
3816-
} },
3817-
{ hash: origHashes["lorem.txt"], meta: {
3818-
$multipart: {},
3819-
$params: {},
3820-
encoding: "7bit",
3821-
fieldname: "myText",
3822-
filename: "lorem.txt",
3823-
mimetype: "text/plain"
3824-
} }
3799+
{ hash: origHashes["logo.png"], params: {
3800+
$encoding: "7bit",
3801+
$fieldname: "myFile",
3802+
$filename: "logo.png",
3803+
$mimetype: "image/png"
3804+
}, meta: {} },
3805+
{ hash: origHashes["lorem.txt"], params: {
3806+
$encoding: "7bit",
3807+
$fieldname: "myText",
3808+
$filename: "lorem.txt",
3809+
$mimetype: "text/plain"
3810+
}, meta: {} }
38253811
]);
38263812
});
38273813
});
@@ -3835,9 +3821,7 @@ describe("Test file uploading", () => {
38353821
.then(res => {
38363822
expect(res.statusCode).toBe(200);
38373823
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
3838-
expect(res.body).toEqual({ hash: origHashes["logo.png"], meta: {
3839-
$params: {},
3840-
} });
3824+
expect(res.body).toEqual({ hash: origHashes["logo.png"], params: {}, meta: {} });
38413825
});
38423826

38433827
});
@@ -3851,9 +3835,7 @@ describe("Test file uploading", () => {
38513835
.then(res => {
38523836
expect(res.statusCode).toBe(200);
38533837
expect(res.headers["content-type"]).toBe("application/json; charset=utf-8");
3854-
expect(res.body).toEqual({ hash: origHashes["logo.png"], meta: {
3855-
$params: { id: "f1234" },
3856-
} });
3838+
expect(res.body).toEqual({ hash: origHashes["logo.png"], params: { id: "f1234" }, meta: {} });
38573839
});
38583840
});
38593841

0 commit comments

Comments
 (0)