@@ -10,85 +10,85 @@ const fbAdmin = require('firebase-admin');
1010const { Storage } = require ( '@google-cloud/storage' ) ;
1111
1212const storage = new Storage ( {
13- projectId : 'ionic-maps-api-1565705298126'
13+ projectId : 'ionic-maps-api-1565705298126' ,
1414} ) ;
1515
1616fbAdmin . initializeApp ( {
17- credential : fbAdmin . credential . cert ( require ( '' ) )
17+ credential : fbAdmin . credential . cert ( require ( '' ) ) ,
1818} ) ;
1919
2020exports . storeImage = functions . https . onRequest ( ( req , res ) => {
21- return cors ( req , res , ( ) => {
22- if ( req . method !== 'POST' ) {
23- return res . status ( 500 ) . json ( { message : 'Not allowed.' } ) ;
24- }
21+ return cors ( req , res , ( ) => {
22+ if ( req . method !== 'POST' ) {
23+ return res . status ( 500 ) . json ( { message : 'Not allowed.' } ) ;
24+ }
2525
26- if (
27- ! req . headers . authorization ||
28- ! req . headers . authorization . startsWith ( 'Bearer ' )
29- ) {
30- return res . status ( 401 ) . json ( { error : 'Unauthorized!' } ) ;
31- }
26+ if (
27+ ! req . headers . authorization ||
28+ ! req . headers . authorization . startsWith ( 'Bearer ' )
29+ ) {
30+ return res . status ( 401 ) . json ( { error : 'Unauthorized!' } ) ;
31+ }
3232
33- let idToken ;
34- idToken = req . headers . authorization . split ( 'Bearer ' ) [ 1 ] ;
33+ let idToken ;
34+ idToken = req . headers . authorization . split ( 'Bearer ' ) [ 1 ] ;
3535
36- const busboy = new Busboy ( { headers : req . headers } ) ;
37- let uploadData ;
38- let oldImagePath ;
36+ const busboy = new Busboy ( { headers : req . headers } ) ;
37+ let uploadData ;
38+ let oldImagePath ;
3939
40- busboy . on ( 'file' , ( fieldname , file , filename , encoding , mimetype ) => {
41- const filePath = path . join ( os . tmpdir ( ) , filename ) ;
42- uploadData = { filePath : filePath , type : mimetype , name : filename } ;
43- file . pipe ( fs . createWriteStream ( filePath ) ) ;
44- } ) ;
40+ busboy . on ( 'file' , ( fieldname , file , filename , encoding , mimetype ) => {
41+ const filePath = path . join ( os . tmpdir ( ) , filename ) ;
42+ uploadData = { filePath : filePath , type : mimetype , name : filename } ;
43+ file . pipe ( fs . createWriteStream ( filePath ) ) ;
44+ } ) ;
4545
46- busboy . on ( 'field' , ( fieldname , value ) => {
47- oldImagePath = decodeURIComponent ( value ) ;
48- } ) ;
46+ busboy . on ( 'field' , ( fieldname , value ) => {
47+ oldImagePath = decodeURIComponent ( value ) ;
48+ } ) ;
4949
50- busboy . on ( 'finish' , ( ) => {
51- const id = uuid ( ) ;
52- let imagePath = 'images/' + id + '-' + uploadData . name ;
53- if ( oldImagePath ) {
54- imagePath = oldImagePath ;
55- }
56-
57- return fbAdmin
58- . auth ( )
59- . verifyIdToken ( idToken )
60- . then ( decodedToken => {
61- console . log ( uploadData . type ) ;
62- return storage
63- . bucket ( 'ionic-maps-api-1565705298126.appspot.com' )
64- . upload ( uploadData . filePath , {
65- uploadType : 'media' ,
66- destination : imagePath ,
67- metadata : {
68- metadata : {
69- contentType : uploadData . type ,
70- firebaseStorageDownloadTokens : id
71- }
72- }
73- } ) ;
74- } )
75- . then ( ( ) => {
76- return res . status ( 201 ) . json ( {
77- imageUrl :
78- 'https://firebasestorage.googleapis.com/v0/b/' +
79- storage . bucket ( 'ionic-maps-api-1565705298126.appspot.com' ) . name +
80- '/o/' +
81- encodeURIComponent ( imagePath ) +
82- '?alt=media&token=' +
83- id ,
84- imagePath : imagePath
85- } ) ;
86- } )
87- . catch ( error => {
88- console . log ( error ) ;
89- return res . status ( 401 ) . json ( { error : 'Unauthorized!' } ) ;
90- } ) ;
91- } ) ;
92- return busboy . end ( req . rawBody ) ;
93- } ) ;
50+ busboy . on ( 'finish' , ( ) => {
51+ const id = uuid ( ) ;
52+ let imagePath = 'images/' + id + '-' + uploadData . name ;
53+ if ( oldImagePath ) {
54+ imagePath = oldImagePath ;
55+ }
56+
57+ return fbAdmin
58+ . auth ( )
59+ . verifyIdToken ( idToken )
60+ . then ( ( decodedToken ) => {
61+ console . log ( uploadData . type ) ;
62+ return storage
63+ . bucket ( 'ionic-maps-api-1565705298126.appspot.com' )
64+ . upload ( uploadData . filePath , {
65+ uploadType : 'media' ,
66+ destination : imagePath ,
67+ metadata : {
68+ metadata : {
69+ contentType : uploadData . type ,
70+ firebaseStorageDownloadTokens : id ,
71+ } ,
72+ } ,
73+ } ) ;
74+ } )
75+ . then ( ( ) => {
76+ return res . status ( 201 ) . json ( {
77+ imageUrl :
78+ 'https://firebasestorage.googleapis.com/v0/b/' +
79+ storage . bucket ( 'ionic-maps-api-1565705298126.appspot.com' ) . name +
80+ '/o/' +
81+ encodeURIComponent ( imagePath ) +
82+ '?alt=media&token=' +
83+ id ,
84+ imagePath : imagePath ,
85+ } ) ;
86+ } )
87+ . catch ( ( error ) => {
88+ console . log ( error ) ;
89+ return res . status ( 401 ) . json ( { error : 'Unauthorized!' } ) ;
90+ } ) ;
91+ } ) ;
92+ return busboy . end ( req . rawBody ) ;
93+ } ) ;
9494} ) ;
0 commit comments