|
2 | 2 | * fileUploader
|
3 | 3 | */
|
4 | 4 |
|
5 |
| - |
6 | 5 | module.exports = function(crowi) {
|
7 | 6 | 'use strict';
|
8 | 7 |
|
9 |
| - var aws = require('aws-sdk') |
10 |
| - , debug = require('debug')('crowi:lib:fileUploader') |
11 |
| - , Promise = require('bluebird') |
12 |
| - , Config = crowi.model('Config') |
13 |
| - , config = crowi.getConfig() |
14 |
| - , lib = {} |
15 |
| - ; |
16 |
| - |
17 |
| - lib.getAwsConfig = function() |
18 |
| - { |
19 |
| - return { |
20 |
| - accessKeyId: config.crowi['aws:accessKeyId'], |
21 |
| - secretAccessKey: config.crowi['aws:secretAccessKey'], |
22 |
| - region: config.crowi['aws:region'], |
23 |
| - bucket: config.crowi['aws:bucket'] |
24 |
| - }; |
25 |
| - }; |
26 |
| - |
27 |
| - // lib.deleteFile = function(filePath, callback) { |
28 |
| - // // TODO 実装する |
29 |
| - // }; |
30 |
| - // |
31 |
| - |
32 |
| - lib.uploadFile = function(filePath, contentType, fileStream, options) { |
33 |
| - var awsConfig = lib.getAwsConfig(); |
34 |
| - if (!Config.isUploadable(config)) { |
35 |
| - return new Promise.reject(new Error('AWS is not configured.')); |
36 |
| - } |
37 |
| - |
38 |
| - aws.config.update({ |
39 |
| - accessKeyId: awsConfig.accessKeyId, |
40 |
| - secretAccessKey: awsConfig.secretAccessKey, |
41 |
| - region: awsConfig.region |
42 |
| - }); |
43 |
| - var s3 = new aws.S3(); |
44 |
| - |
45 |
| - var params = {Bucket: awsConfig.bucket}; |
46 |
| - params.ContentType = contentType; |
47 |
| - params.Key = filePath; |
48 |
| - params.Body = fileStream; |
49 |
| - params.ACL = 'public-read'; |
50 |
| - |
51 |
| - return new Promise(function(resolve, reject) { |
52 |
| - s3.putObject(params, function(err, data) { |
53 |
| - if (err) { |
54 |
| - return reject(err); |
55 |
| - } |
56 |
| - |
57 |
| - return resolve(data); |
58 |
| - }); |
59 |
| - }); |
60 |
| - }; |
61 |
| - |
62 |
| - lib.generateS3FileUrl = function(filePath) { |
63 |
| - var awsConfig = lib.getAwsConfig(); |
64 |
| - var url = 'https://' + awsConfig.bucket +'.s3.amazonaws.com/' + filePath; |
65 |
| - |
66 |
| - return url; |
67 |
| - }; |
| 8 | + var debug = require('debug')('crowi:lib:fileUploader') |
| 9 | + , method = crowi.env.FILE_UPLOAD || 'aws' |
| 10 | + , lib = '../../local_modules/crowi-fileupload-' + method; |
68 | 11 |
|
69 |
| - return lib; |
| 12 | + return require(lib)(crowi); |
70 | 13 | };
|
0 commit comments