0.1.2 • Published 5 years ago

koa-transfer-file v0.1.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

koa-transfer-file

npm version Build Status codecov

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. Converting Stream to Buffer by array is unsafe when transferring big files. To avoid this problem, using file stream as a default. The temp file will be deleted after new Readable stream 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 name to the readable stream, due to the package form-data will name the file by filestream.name or filestream.path when appending data.

  • option appendField: (boolean, default false) Append files to ctx.request.body in order to keep it(formData) the same as before the request was sent.

  • option appendFile: (boolean, default true, deprecative) Highly recommanded false. Append all files in an array to ctx.request.body with fieldname _files. The difference between ctx.request.body._files and ctx.request.files is that _files has 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-file

Usage

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:

  1. By default, file is a readable stream.
const rs = file;
  1. When opts.onDisk is set to false, file.value contains 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

KeyDesc
filenameoriginal name of the file
fieldnamefield name specified in the form
encoding-
mimetype-
truncatedstream is truncated or not (file reached limit size)

opts.onDisk = true

KeyDesc
namealias of filename

opts.onDisk = false

KeyDesc
valuefile data in buffer
0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.5

5 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago