Skip to content

Commit

Permalink
feat: saveRequestFiles support options (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
okoala authored and fengmk2 committed Aug 3, 2019
1 parent 7b444c0 commit 5d3ee0f
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ node_js:
- '8'
- '10'
- '12'
before_install:
- npm i npminstall -g
install:
- npm i npminstall && npminstall
- npminstall
script:
- npm run ci
after_script:
Expand Down
17 changes: 15 additions & 2 deletions app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,28 @@ module.exports = {
/**
* save request multipart data and files to `ctx.request`
* @function Context#saveRequestFiles
* @param {Object} options
* - {String} options.defCharset
* - {Object} options.limits
* - {Function} options.checkFile
*/
async saveRequestFiles() {
async saveRequestFiles(options) {
options = options || {};
const ctx = this;

const multipartOptions = {
autoFields: false,
};
if (options.defCharset) multipartOptions.defCharset = options.defCharset;
if (options.limits) multipartOptions.limits = options.limits;
if (options.checkFile) multipartOptions.checkFile = options.checkFile;

let storedir;

const requestBody = {};
const requestFiles = [];

const parts = ctx.multipart({ autoFields: false });
const parts = ctx.multipart(multipartOptions);
let part;
do {
try {
Expand Down
45 changes: 45 additions & 0 deletions test/dynamic-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const assert = require('assert');
const formstream = require('formstream');
const urllib = require('urllib');
const mock = require('egg-mock');
const rimraf = require('mz-modules/rimraf');

describe('test/dynamic-option.test.js', () => {
let app;
let server;
let host;
before(() => {
app = mock.app({
baseDir: 'apps/dynamic-option',
});
return app.ready();
});
before(() => {
server = app.listen();
host = 'http://127.0.0.1:' + server.address().port;
});
after(() => {
return rimraf(app.config.multipart.tmpdir);
});
after(() => app.close());
after(() => server.close());
beforeEach(() => app.mockCsrf());
afterEach(mock.restore);

it('should work with saveRequestFiles options', async () => {
const form = formstream();
form.buffer('file', Buffer.alloc(1 * 1024 * 1024), '1mb.js', 'application/octet-stream');

const headers = form.headers();
const res = await urllib.request(host + '/upload', {
method: 'POST',
headers,
stream: form,
});

assert(res.status === 413);
assert(res.data.toString().includes('Request_fileSize_limitError: Reach fileSize limit'));
});
});
1 change: 0 additions & 1 deletion test/file-mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ describe('test/file-mode.test.js', () => {
];
const shouldKeepDirs = [
path.join(app.config.multipart.tmpdir, moment().subtract(2, 'years').format('YYYY/MM/DD/HH')),
path.join(app.config.multipart.tmpdir, moment().subtract(8, 'days').format('YYYY/MM/DD/HH')),
path.join(app.config.multipart.tmpdir, moment().format('YYYY/MM/DD/HH')),
];
const currentMonth = new Date().getMonth();
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/apps/dynamic-option/app/controller/upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

module.exports = (app) => {
return class extends app.Controller {
async index(ctx) {
try {
const file = await ctx.saveRequestFiles({
limits: {
fileSize: 10000
}
});
ctx.body = file;
} finally {
await ctx.cleanupRequestFiles();
}
};
}
}
5 changes: 5 additions & 0 deletions test/fixtures/apps/dynamic-option/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = app => {
app.post('/upload', app.controller.upload.index);
};
8 changes: 8 additions & 0 deletions test/fixtures/apps/dynamic-option/app/views/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<form method="POST" action="/upload?_csrf={{ ctx.csrf | safe }}" enctype="multipart/form-data">
title: <input name="title" />
file1: <input name="file1" type="file" />
file2: <input name="file2" type="file" />
file3: <input name="file3" type="file" />
other: <input name="other" />
<button type="submit">上传</button>
</form>
7 changes: 7 additions & 0 deletions test/fixtures/apps/dynamic-option/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

exports.multipart = {
mode: 'stream',
};

exports.keys = 'multipart';
8 changes: 8 additions & 0 deletions test/fixtures/apps/dynamic-option/config/config.unittest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

exports.logger = {
consoleLevel: 'NONE',
coreLogger: {
// consoleLevel: 'DEBUG',
},
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/dynamic-option/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "dynamic-options-demo"
}

0 comments on commit 5d3ee0f

Please sign in to comment.