egg-filestore
v1.0.5
Published
egg filestore for aliyun oss and file system, support multi instance
Downloads
2
Readme
egg-filestore
Install
$ npm i egg-filestore --save
Usage
// {app_root}/config/plugin.js
exports.filestore = {
enable: true,
package: 'egg-filestore',
};
Configuration
// {app_root}/config/config.default.js
exports.filestore = {
};
see config/config.default.js for more detail.
Single Clients Config
FileSystem
exports.filestore = {
type: 'FileSystem',
prefixUrl: '/source/upload',
directory: '~/uploads' // default '~/uploads'
}
AliOss
exports.filestore = {
type: 'AliOss',
options: { // ali-oss config
accessKeyId: 'access key',
accessKeySecret: 'access key secret',
bucket: 'you bucket',
region: 'oss-cn-hangzhou',
timeout: '60s',
},
category: 'uploads',
}
Multi Clients Config
exports.filestore = {
clients: {
instance1: {
type: 'AliOss',
options: {
accessKeyId: 'access key',
accessKeySecret: 'access key secret',
bucket: 'you bucket',
region: 'oss-cn-hangzhou',
timeout: '60s',
},
category: 'uploads',
},
instance2: {
type: 'FileSystem',
prefixUrl: '/source/upload',
directory: '~/uploads' // default '~/uploads'
},
},
}
Example
Single Clients
// app/controller/home.js
module.exports = app => {
return class HomeController extends app.Controller {
async create() {
const { ctx, app } = this;
const stream = await ctx.getFileStream();
await app.filestore.createFromStream({ stream, filename: stream.filename });
}
};
};
Multi Clients
// app/controller/home.js
module.exports = app => {
return class HomeController extends app.Controller {
async create() {
const { ctx, app } = this;
const stream = await ctx.getFileStream();
// get instanc
await app.filestore.get('instance1').createFromStream({ stream, filename: stream.filename });
}
};
};
Methods
createFromStream
// app/controller/home.js
module.exports = app => {
return class HomeController extends app.Controller {
async create() {
const { ctx, app } = this;
const stream = await ctx.getFileStream();
await app.filestore.createFromStream({ stream, filename: stream.filename });
}
};
};
createFromUrl
// app/controller/home.js
module.exports = app => {
return class HomeController extends app.Controller {
async createByUrl() {
const { app } = this;
const { url } = ctx.request.body;
const filename = path.basename(url);
const extname = path.extname(url).toLowerCase();
const sourceUrl = await app.filestore.createFromUrl({
url,
filename: filename + extname ,
});
}
};
};
destroy
// app/controller/home.js
module.exports = app => {
return class HomeController extends app.Controller {
async destory() {
const { ctx, app } = this;
const { url } = ctx.request.body;
const filename = path.basename(url);
const extname = path.extname(url).toLowerCase();
await app.filestore.destroy(filename + extname);
}
};
};
download
// app/controller/home.js
const getFileNameAndExtentions = function(filename) {
if (!filename) {
return {};
}
const nameArr = decodeURIComponent(filename).split('.');
const extname = nameArr.pop();
const name = nameArr.join('.');
return {
extname,
name,
};
}
module.exports = app => {
return class HomeController extends app.Controller {
async download() {
const { ctx, app } = this;
const filename = ctx.params.filename;
if (!filename) {
ctx.status = 404;
return;
}
const id = getFileNameAndExtentions(decodeURIComponent(filename)).name || filename;
const attachment = await ctx.service.upload.show(id);
if (!attachment) {
ctx.status = 404;
return;
}
return app.filestore.get('uploads').download({
filename,
ctx
});
}
};
};
Questions & Suggestions
Please open an issue here.