| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 18:27:39 +02:00
										 |  |  | /* 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 = { | 
					
						
							| 
									
										
										
										
											2020-02-27 19:34:14 +01:00
										 |  |  |   init(config) { | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  |     const S3 = new AWS.S3({ | 
					
						
							|  |  |  |       apiVersion: '2006-03-01', | 
					
						
							| 
									
										
										
										
											2020-02-27 19:34:14 +01:00
										 |  |  |       ...config, | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 17:10:25 +01:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2020-02-27 19:34:14 +01:00
										 |  |  |       upload(file, customParams = {}) { | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  |         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}/` : ''; | 
					
						
							| 
									
										
										
										
											2019-07-18 19:28:52 +02:00
										 |  |  |           S3.upload( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               Key: `${path}${file.hash}${file.ext}`, | 
					
						
							| 
									
										
										
										
											2020-01-24 15:39:14 +01:00
										 |  |  |               Body: Buffer.from(file.buffer, 'binary'), | 
					
						
							| 
									
										
										
										
											2019-07-18 19:28:52 +02:00
										 |  |  |               ACL: 'public-read', | 
					
						
							|  |  |  |               ContentType: file.mime, | 
					
						
							| 
									
										
										
										
											2020-02-27 19:34:14 +01:00
										 |  |  |               ...customParams, | 
					
						
							| 
									
										
										
										
											2019-07-18 19:28:52 +02:00
										 |  |  |             }, | 
					
						
							|  |  |  |             (err, data) => { | 
					
						
							|  |  |  |               if (err) { | 
					
						
							|  |  |  |                 return reject(err); | 
					
						
							|  |  |  |               } | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 19:28:52 +02:00
										 |  |  |               // set the bucket file url
 | 
					
						
							|  |  |  |               file.url = data.Location; | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 19:28:52 +02:00
										 |  |  |               resolve(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           ); | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2020-02-27 19:34:14 +01:00
										 |  |  |       delete(file, customParams = {}) { | 
					
						
							| 
									
										
										
										
											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}/` : ''; | 
					
						
							| 
									
										
										
										
											2019-07-18 19:28:52 +02:00
										 |  |  |           S3.deleteObject( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               Key: `${path}${file.hash}${file.ext}`, | 
					
						
							| 
									
										
										
										
											2020-02-27 19:34:14 +01:00
										 |  |  |               ...customParams, | 
					
						
							| 
									
										
										
										
											2019-07-18 19:28:52 +02:00
										 |  |  |             }, | 
					
						
							|  |  |  |             (err, data) => { | 
					
						
							|  |  |  |               if (err) { | 
					
						
							|  |  |  |                 return reject(err); | 
					
						
							|  |  |  |               } | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 19:28:52 +02:00
										 |  |  |               resolve(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           ); | 
					
						
							| 
									
										
										
										
											2018-02-22 14:43:10 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-07-18 19:28:52 +02:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-07-18 19:28:52 +02:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2018-02-20 15:57:34 +01:00
										 |  |  | }; |