@@ -10,85 +10,85 @@ const fbAdmin = require('firebase-admin');
10
10
const { Storage } = require ( '@google-cloud/storage' ) ;
11
11
12
12
const storage = new Storage ( {
13
- projectId : 'ionic-maps-api-1565705298126'
13
+ projectId : 'ionic-maps-api-1565705298126' ,
14
14
} ) ;
15
15
16
16
fbAdmin . initializeApp ( {
17
- credential : fbAdmin . credential . cert ( require ( '' ) )
17
+ credential : fbAdmin . credential . cert ( require ( '' ) ) ,
18
18
} ) ;
19
19
20
20
exports . 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
+ }
25
25
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
+ }
32
32
33
- let idToken ;
34
- idToken = req . headers . authorization . split ( 'Bearer ' ) [ 1 ] ;
33
+ let idToken ;
34
+ idToken = req . headers . authorization . split ( 'Bearer ' ) [ 1 ] ;
35
35
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 ;
39
39
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
+ } ) ;
45
45
46
- busboy . on ( 'field' , ( fieldname , value ) => {
47
- oldImagePath = decodeURIComponent ( value ) ;
48
- } ) ;
46
+ busboy . on ( 'field' , ( fieldname , value ) => {
47
+ oldImagePath = decodeURIComponent ( value ) ;
48
+ } ) ;
49
49
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
+ } ) ;
94
94
} ) ;
0 commit comments