koa-transfer-file v0.1.2
koa-transfer-file
Package under development. Please lock the specific version in package.json or package-lock.json.
Transfer file stream without storing files.
This package is mainly used for the middle layer.
Featured
option
onDisk: (boolean, default true) It determines whether disk I/O is being used during transmission. ConvertingStreamtoBufferby array is unsafe when transferring big files. To avoid this problem, using file stream as a default. The temp file will be deleted after newReadablestream is built.maintain files' name: When sending files to another server, filenames will be changed into tmpName because of the new readable stream. Solved by adding property
nameto the readable stream, due to the packageform-datawill name the file byfilestream.nameorfilestream.pathwhen appending data.option
appendField: (boolean, default false) Append files toctx.request.bodyin order to keep it(formData) the same as before the request was sent.option
appendFile: (boolean, default true, deprecative) Highly recommandedfalse. Append all files in an array toctx.request.bodywith fieldname_files. The difference between ctx.request.body._files and ctx.request.files is that_fileshas been formatted for the puropse of transferring directly by request.The default value is only for compatibility with the old versions temporarily. It's innocent when you don't care about files' fieldname.
Install
npm install koa-transfer-fileUsage
The options almost same as busboy.
const Koa = require('koa');
const transfer = require('koa-transfer-file');
const app = new Koa();
const options = {
onDisk: true, // (boolean, default true)
limits: {
fileSize: 1024 * 5
}
}
app.use(transfer(options));Transfer
Transfer formData by request directly.
const request = require('request-promise');
app.use((ctx, next) => {
const formData = ctx.request.body;
request({
method: 'POST',
uri: 'http://localhost:3000',
formData
});
next();
});Or configure the formData's value manually when opts.onDisk=false.
const formData = {};
formData[file.fieldname] = {
value: file.value,
options: {
filename: file.filename,
contentType: file.mimetype
}
}Save
For each file of ctx.request.files:
- By default,
fileis a readable stream.
const rs = file;- When
opts.onDiskis set to false,file.valuecontains a Buffer.
const { Readable } = require('stream');
const rs = new Readable();
rs._read = () => {};
rs.push(file.value);Then save the readable stream to the file.
rs.pipe(fs.createWriteStream(file.filename))
.on('finish', () => console.log('saved'));;API
Opts
Please refer to Featured for description of the options.
File information
Files (<object[]>) can be got from ctx.request.files.
The properties of each file are as shown below.
Certain Properties
| Key | Desc |
|---|---|
| filename | original name of the file |
| fieldname | field name specified in the form |
| encoding | - |
| mimetype | - |
| truncated | stream is truncated or not (file reached limit size) |
opts.onDisk = true
| Key | Desc |
|---|---|
| name | alias of filename |
opts.onDisk = false
| Key | Desc |
|---|---|
| value | file data in buffer |