File tree Expand file tree Collapse file tree 9 files changed +151
-5
lines changed Expand file tree Collapse file tree 9 files changed +151
-5
lines changed Original file line number Diff line number Diff line change @@ -26,9 +26,8 @@ module.exports = (context, req) => {
26
26
if ( ! f ) {
27
27
context . res = { status : 400 , body : "Must submit a file for processing!" } ;
28
28
} else {
29
- /* since the file is Base64-encoded, read the file and parse as "base64" */
30
- const b64 = fs . readFileSync ( f . path ) . toString ( ) ;
31
- const wb = XLSX . read ( b64 , { type :"base64" } ) ;
29
+ /* file is stored in a temp directory, so we can point to that and read it */
30
+ const wb = XLSX . read ( f . path , { type :"file" } ) ;
32
31
33
32
/* convert to specified output type -- default CSV */
34
33
const ext = ( fields . bookType || "csv" ) . toLowerCase ( ) ;
Original file line number Diff line number Diff line change
1
+ # Logs
2
+ logs
3
+ * .log
4
+ npm-debug.log *
5
+ yarn-debug.log *
6
+ yarn-error.log *
7
+ firebase-debug.log *
8
+
9
+ # Firebase cache
10
+ .firebase /
11
+
12
+ # Firebase config
13
+
14
+ # Uncomment this if you'd like others to create their own Firebase project.
15
+ # For a team working on the same Firebase project(s), it is recommended to leave
16
+ # it commented so all members can deploy to the same project(s) in .firebaserc.
17
+ .firebaserc
18
+
19
+ # Runtime data
20
+ pids
21
+ * .pid
22
+ * .seed
23
+ * .pid.lock
24
+
25
+ # Directory for instrumented libs generated by jscoverage/JSCover
26
+ lib-cov
27
+
28
+ # Coverage directory used by tools like istanbul
29
+ coverage
30
+
31
+ # nyc test coverage
32
+ .nyc_output
33
+
34
+ # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
35
+ .grunt
36
+
37
+ # Bower dependency directory (https://bower.io/)
38
+ bower_components
39
+
40
+ # node-waf configuration
41
+ .lock-wscript
42
+
43
+ # Compiled binary addons (http://nodejs.org/api/addons.html)
44
+ build /Release
45
+
46
+ # Dependency directories
47
+ node_modules /
48
+
49
+ # Optional npm cache directory
50
+ .npm
51
+
52
+ # Optional eslint cache
53
+ .eslintcache
54
+
55
+ # Optional REPL history
56
+ .node_repl_history
57
+
58
+ # Output of 'npm pack'
59
+ * .tgz
60
+
61
+ # Yarn Integrity file
62
+ .yarn-integrity
63
+
64
+ # dotenv environment variables file
65
+ .env
Original file line number Diff line number Diff line change
1
+ {}
Original file line number Diff line number Diff line change
1
+ node_modules /
Original file line number Diff line number Diff line change
1
+ const functions = require ( 'firebase-functions' ) ;
2
+ const Busboy = require ( 'busboy' ) ;
3
+ const XLSX = require ( 'xlsx' ) ;
4
+
5
+ // // Create and Deploy Your First Cloud Functions
6
+ // // https://firebase.google.com/docs/functions/write-firebase-functions
7
+ //
8
+ exports . helloWorld = functions . https . onRequest ( ( request , response ) => {
9
+ response . send ( "Hello from Firebase!" ) ;
10
+ } ) ;
11
+
12
+ exports . main = functions . https . onRequest ( ( req , res ) => {
13
+ var bb = new Busboy ( {
14
+ headers : {
15
+ 'content-type' : req . headers [ 'content-type' ]
16
+ }
17
+ } ) ;
18
+ let fields = { } ;
19
+ let files = { } ;
20
+ bb . on ( 'field' , ( fieldname , val ) => {
21
+ fields [ fieldname ] = val ;
22
+ } ) ;
23
+ bb . on ( 'file' , ( fieldname , file , filename ) => {
24
+ var buffers = [ ] ;
25
+ file . on ( 'data' , ( data ) => {
26
+ buffers . push ( data ) ;
27
+ } ) ;
28
+ file . on ( 'end' , ( ) => {
29
+ files [ fieldname ] = [ Buffer . concat ( buffers ) , filename ] ;
30
+ } ) ;
31
+ } ) ;
32
+ bb . on ( 'finish' , ( ) => {
33
+ let f = files [ Object . keys ( files ) [ 0 ] ] ;
34
+ const wb = XLSX . read ( f [ 0 ] , { type : "buffer" } ) ;
35
+ // Convert to CSV
36
+ res . send ( XLSX . utils . sheet_to_csv ( wb . Sheets [ wb . SheetNames [ 0 ] ] ) ) ;
37
+ } ) ;
38
+ bb . end ( req . body )
39
+ } ) ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " functions" ,
3
+ "description" : " Cloud Functions for Firebase" ,
4
+ "scripts" : {
5
+ "serve" : " firebase emulators:start --only functions" ,
6
+ "shell" : " firebase functions:shell" ,
7
+ "start" : " npm run shell" ,
8
+ "deploy" : " firebase deploy --only functions" ,
9
+ "logs" : " firebase functions:log"
10
+ },
11
+ "engines" : {
12
+ "node" : " 8"
13
+ },
14
+ "dependencies" : {
15
+ "busboy" : " ^0.3.1" ,
16
+ "firebase-admin" : " ^8.6.0" ,
17
+ "firebase-functions" : " ^3.3.0" ,
18
+ "xlsx" : " ^0.16.2"
19
+ },
20
+ "devDependencies" : {
21
+ "firebase-functions-test" : " ^0.1.6"
22
+ },
23
+ "private" : true
24
+ }
Original file line number Diff line number Diff line change @@ -9,8 +9,12 @@ aws: lambda-proxy
9
9
lambda-proxy :
10
10
cd LambdaProxy; mkdir -p node_modules; npm install xlsx busboy; sam local start-api; cd -
11
11
12
+ .PHONY : init-azure
13
+ init-azure :
14
+ cd AzureHTTPTrigger; mkdir -p node_modules; npm install xlsx formidable fs
15
+
12
16
.PHONY : azure
13
- azure :
17
+ azure : init-azure
14
18
func start
15
19
16
20
.PHONY : azure-server
Original file line number Diff line number Diff line change @@ -121,3 +121,14 @@ HTTP trigger that converts the submitted file to CSV.
121
121
122
122
When deploying on Azure, be sure to install the module from the remote console,
123
123
as described in the "Azure Functions JavaScript developer guide".
124
+
125
+ #### Firebase Functions
126
+
127
+ Firebase functions can be triggered via HTTP requests, similar to a REST API.
128
+ In the ` Firebase ` directory, the example function reads files sent through
129
+ HTTP and converts it to a CSV and sends the response in the form of a string.
130
+
131
+ To run this demo locally, run ` npm i -g firebase-tools ` to install the
132
+ Firebase CLI and ` npm i ` to install the dependencies, then ` firebase use --add `
133
+ to connect to an existing Firebase project. Run ` firebase emulators:start ` to
134
+ start the local server.
Original file line number Diff line number Diff line change 1
- { }
1
+ {
2
+ "version" : " 2.0"
3
+ }
You can’t perform that action at this time.
0 commit comments