| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 18:27:39 +02:00
										 |  |  | /* eslint-disable import/no-unresolved */ | 
					
						
							|  |  |  | /* eslint-disable no-unused-vars */ | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  | // Public node modules.
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  | const AWS = require('aws-sdk'); | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-02-23 14:57:58 +01:00
										 |  |  |   provider: 'aws-s3', | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  |   name: 'Amazon Web Service S3', | 
					
						
							|  |  |  |   auth: { | 
					
						
							|  |  |  |     public: { | 
					
						
							|  |  |  |       label: 'Access API Token', | 
					
						
							|  |  |  |       type: 'text' | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     private: { | 
					
						
							|  |  |  |       label: 'Secret Access Token', | 
					
						
							|  |  |  |       type: 'text' | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     region: { | 
					
						
							|  |  |  |       label: 'Region', | 
					
						
							|  |  |  |       type: 'enum', | 
					
						
							|  |  |  |       values: [ | 
					
						
							|  |  |  |         'us-east-1', | 
					
						
							|  |  |  |         'us-east-2', | 
					
						
							|  |  |  |         'us-west-1', | 
					
						
							|  |  |  |         'us-west-2', | 
					
						
							|  |  |  |         'ca-central-1', | 
					
						
							|  |  |  |         'ap-south-1', | 
					
						
							|  |  |  |         'ap-northeast-1', | 
					
						
							|  |  |  |         'ap-northeast-2', | 
					
						
							|  |  |  |         'ap-northeast-3', | 
					
						
							|  |  |  |         'ap-southeast-1', | 
					
						
							|  |  |  |         'ap-southeast-2', | 
					
						
							|  |  |  |         'cn-north-1', | 
					
						
							|  |  |  |         'cn-northwest-1', | 
					
						
							|  |  |  |         'eu-central-1', | 
					
						
							|  |  |  |         'eu-west-1', | 
					
						
							|  |  |  |         'eu-west-2', | 
					
						
							|  |  |  |         'eu-west-3', | 
					
						
							|  |  |  |         'sa-east-1' | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     bucket: { | 
					
						
							|  |  |  |       label: 'Bucket', | 
					
						
							|  |  |  |       type: 'text' | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2018-02-27 16:53:06 +01:00
										 |  |  |   init: (config) => { | 
					
						
							| 
									
										
										
										
											2018-02-21 17:18:33 +01:00
										 |  |  |     // configure AWS S3 bucket connection
 | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  |     AWS.config.update({ | 
					
						
							| 
									
										
										
										
											2018-03-02 15:06:35 +01:00
										 |  |  |       accessKeyId: config.public, | 
					
						
							|  |  |  |       secretAccessKey: config.private, | 
					
						
							|  |  |  |       region: config.region | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const S3 = new AWS.S3({ | 
					
						
							|  |  |  |       apiVersion: '2006-03-01', | 
					
						
							|  |  |  |       params: { | 
					
						
							| 
									
										
										
										
											2018-03-02 15:06:35 +01:00
										 |  |  |         Bucket: config.bucket | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 17:10:25 +01:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  |       upload: (file) => { | 
					
						
							|  |  |  |         return new Promise((resolve, reject) => { | 
					
						
							| 
									
										
										
										
											2018-02-21 17:18:33 +01:00
										 |  |  |           // upload file on S3 bucket
 | 
					
						
							| 
									
										
										
										
											2018-07-20 10:16:50 -07:00
										 |  |  |           const path = file.path ? `${file.path}/` : ''; | 
					
						
							| 
									
										
										
										
											2018-02-22 14:43:10 +01:00
										 |  |  |           S3.upload({ | 
					
						
							| 
									
										
										
										
											2018-07-20 10:16:50 -07:00
										 |  |  |             Key: `${path}${file.hash}${file.ext}`, | 
					
						
							| 
									
										
										
										
											2018-02-22 14:43:10 +01:00
										 |  |  |             Body: new Buffer(file.buffer, 'binary'), | 
					
						
							| 
									
										
										
										
											2018-07-13 11:33:44 -07:00
										 |  |  |             ACL: 'public-read', | 
					
						
							|  |  |  |             ContentType: file.mime, | 
					
						
							| 
									
										
										
										
											2018-02-22 14:43:10 +01:00
										 |  |  |           }, (err, data) => { | 
					
						
							|  |  |  |             if (err) { | 
					
						
							|  |  |  |               return reject(err); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 14:43:10 +01:00
										 |  |  |             // set the bucket file url
 | 
					
						
							|  |  |  |             file.url = data.Location; | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 14:43:10 +01:00
										 |  |  |             resolve(); | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  |       delete: (file) => { | 
					
						
							| 
									
										
										
										
											2018-02-22 14:43:10 +01:00
										 |  |  |         return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |           // delete file on S3 bucket
 | 
					
						
							| 
									
										
										
										
											2018-07-20 10:16:50 -07:00
										 |  |  |           const path = file.path ? `${file.path}/` : ''; | 
					
						
							| 
									
										
										
										
											2018-10-12 18:43:58 +03:00
										 |  |  |           S3.deleteObject({ | 
					
						
							| 
									
										
										
										
											2018-08-23 11:43:05 -07:00
										 |  |  |             Key: `${path}${file.hash}${file.ext}` | 
					
						
							| 
									
										
										
										
											2018-02-22 14:43:10 +01:00
										 |  |  |           }, (err, data) => { | 
					
						
							|  |  |  |             if (err) { | 
					
						
							|  |  |  |               return reject(err); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 14:43:10 +01:00
										 |  |  |             resolve(); | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; |