1+ import { Storage } from '@google-cloud/storage' ;
12import * as AWS from 'aws-sdk' ;
23import * as EmailValidator from 'email-deep-validator' ;
34import * as fileType from 'file-type' ;
@@ -50,20 +51,54 @@ export const checkFile = async file => {
5051 return 'ok' ;
5152} ;
5253
53- /*
54- * Save binary data to amazon s3
54+ /**
55+ * Create AWS instance
5556 */
56- export const uploadFile = async ( file : { name : string ; path : string } ) : Promise < string > => {
57+ const createAWS = ( ) => {
5758 const AWS_ACCESS_KEY_ID = getEnv ( { name : 'AWS_ACCESS_KEY_ID' } ) ;
5859 const AWS_SECRET_ACCESS_KEY = getEnv ( { name : 'AWS_SECRET_ACCESS_KEY' } ) ;
5960 const AWS_BUCKET = getEnv ( { name : 'AWS_BUCKET' } ) ;
60- const AWS_PREFIX = getEnv ( { name : 'AWS_PREFIX' , defaultValue : '' } ) ;
61+
62+ if ( ! AWS_ACCESS_KEY_ID || ! AWS_SECRET_ACCESS_KEY || ! AWS_BUCKET ) {
63+ throw new Error ( 'AWS credentials are not configured' ) ;
64+ }
6165
6266 // initialize s3
63- const s3 = new AWS . S3 ( {
67+ return new AWS . S3 ( {
6468 accessKeyId : AWS_ACCESS_KEY_ID ,
6569 secretAccessKey : AWS_SECRET_ACCESS_KEY ,
6670 } ) ;
71+ } ;
72+
73+ /**
74+ * Create Google Cloud Storage instance
75+ */
76+ const createGCS = ( ) => {
77+ const GOOGLE_APPLICATION_CREDENTIALS = getEnv ( { name : 'GOOGLE_APPLICATION_CREDENTIALS' } ) ;
78+ const GOOGLE_PROJECT_ID = getEnv ( { name : 'GOOGLE_PROJECT_ID' } ) ;
79+ const BUCKET = getEnv ( { name : 'GOOGLE_CLOUD_STORAGE_BUCKET' } ) ;
80+
81+ if ( ! GOOGLE_PROJECT_ID || ! GOOGLE_APPLICATION_CREDENTIALS || ! BUCKET ) {
82+ throw new Error ( 'Google Cloud Storage credentials are not configured' ) ;
83+ }
84+
85+ // initializing Google Cloud Storage
86+ return new Storage ( {
87+ projectId : GOOGLE_PROJECT_ID ,
88+ keyFilename : GOOGLE_APPLICATION_CREDENTIALS ,
89+ } ) ;
90+ } ;
91+
92+ /*
93+ * Save binary data to amazon s3
94+ */
95+ export const uploadFileAWS = async ( file : { name : string ; path : string } ) : Promise < string > => {
96+ const AWS_BUCKET = getEnv ( { name : 'AWS_BUCKET' } ) ;
97+ const AWS_PREFIX = getEnv ( { name : 'AWS_PREFIX' , defaultValue : '' } ) ;
98+ const IS_PUBLIC = getEnv ( { name : 'FILE_SYSTEM_PUBLIC' , defaultValue : 'true' } ) ;
99+
100+ // initialize s3
101+ const s3 = createAWS ( ) ;
67102
68103 // generate unique name
69104 const fileName = `${ AWS_PREFIX } ${ Math . random ( ) } ${ file . name } ` ;
@@ -78,7 +113,7 @@ export const uploadFile = async (file: { name: string; path: string }): Promise<
78113 Bucket : AWS_BUCKET ,
79114 Key : fileName ,
80115 Body : buffer ,
81- ACL : ' public-read',
116+ ACL : IS_PUBLIC === 'true' ? ' public-read' : undefined ,
82117 } ,
83118 ( err , res ) => {
84119 if ( err ) {
@@ -90,7 +125,102 @@ export const uploadFile = async (file: { name: string; path: string }): Promise<
90125 ) ;
91126 } ) ;
92127
93- return response . Location ;
128+ return IS_PUBLIC === 'true' ? response . Location : fileName ;
129+ } ;
130+
131+ /*
132+ * Save file to google cloud storage
133+ */
134+ export const uploadFileGCS = async ( file : { name : string ; path : string ; type : string } ) : Promise < string > => {
135+ const BUCKET = getEnv ( { name : 'GOOGLE_CLOUD_STORAGE_BUCKET' } ) ;
136+ const IS_PUBLIC = getEnv ( { name : 'FILE_SYSTEM_PUBLIC' , defaultValue : 'true' } ) ;
137+
138+ // initialize GCS
139+ const storage = createGCS ( ) ;
140+
141+ // select bucket
142+ const bucket = storage . bucket ( BUCKET ) ;
143+
144+ // generate unique name
145+ const fileName = `${ Math . random ( ) } ${ file . name } ` ;
146+
147+ bucket . file ( fileName ) ;
148+
149+ const response : any = await new Promise ( ( resolve , reject ) => {
150+ bucket . upload (
151+ file . path ,
152+ {
153+ metadata : { contentType : file . type } ,
154+ public : IS_PUBLIC === 'true' ,
155+ } ,
156+ ( err , res ) => {
157+ if ( err ) {
158+ return reject ( err ) ;
159+ }
160+
161+ if ( res ) {
162+ return resolve ( res ) ;
163+ }
164+ } ,
165+ ) ;
166+ } ) ;
167+
168+ const { metadata, name } = response ;
169+
170+ return IS_PUBLIC === 'true' ? metadata . mediaLink : name ;
171+ } ;
172+
173+ /**
174+ * Read file from GCS, AWS
175+ */
176+ export const readFileRequest = async ( key : string ) : Promise < any > => {
177+ const UPLOAD_SERVICE_TYPE = getEnv ( { name : 'UPLOAD_SERVICE_TYPE' , defaultValue : 'AWS' } ) ;
178+
179+ if ( UPLOAD_SERVICE_TYPE === 'GCS' ) {
180+ const GCS_BUCKET = getEnv ( { name : 'GOOGLE_CLOUD_STORAGE_BUCKET' } ) ;
181+ const storage = createGCS ( ) ;
182+
183+ const bucket = storage . bucket ( GCS_BUCKET ) ;
184+
185+ const file = bucket . file ( key ) ;
186+
187+ // get a file buffer
188+ const [ contents ] = await file . download ( { } ) ;
189+
190+ return contents ;
191+ }
192+
193+ const AWS_BUCKET = getEnv ( { name : 'AWS_BUCKET' } ) ;
194+ const s3 = createAWS ( ) ;
195+
196+ return new Promise ( ( resolve , reject ) => {
197+ s3 . getObject (
198+ {
199+ Bucket : AWS_BUCKET ,
200+ Key : key ,
201+ } ,
202+ ( error , response ) => {
203+ if ( error ) {
204+ return reject ( error ) ;
205+ }
206+
207+ return resolve ( response . Body ) ;
208+ } ,
209+ ) ;
210+ } ) ;
211+ } ;
212+
213+ /*
214+ * Save binary data to amazon s3
215+ */
216+ export const uploadFile = async ( file ) : Promise < string > => {
217+ const UPLOAD_SERVICE_TYPE = getEnv ( { name : 'UPLOAD_SERVICE_TYPE' , defaultValue : 'AWS' } ) ;
218+
219+ if ( UPLOAD_SERVICE_TYPE === 'AWS' ) {
220+ return uploadFileAWS ( file ) ;
221+ }
222+
223+ return uploadFileGCS ( file ) ;
94224} ;
95225
96226/**
0 commit comments