Skip to content

Commit 1a2bf9d

Browse files
committed
Add file share coffeescript example
1 parent e237346 commit 1a2bf9d

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

blueprint/examplecode.apib

+5
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ FORMAT: 1A
3333
<!-- include(code-examples/coffeescript/share-url.coffee) -->
3434

3535

36+
## Share a file
37+
38+
<!-- include(code-examples/coffeescript/share-file.coffee) -->
39+
40+
3641
<!-- include(code-examples/coffeescript/README.md) -->

code-examples/coffeescript/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ Make sure you have [NodeJS](https://www.nodejs.org) installed.
1111

1212
## Run
1313

14+
To share an URL:
15+
1416
coffee share-url.coffee
17+
18+
To share a local file:
19+
20+
coffee share-file.coffee myfile.png

code-examples/coffeescript/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"dependencies": {
99
"coffee-script": "~1.7.1",
1010
"express": "^4.12.4",
11-
"oauth": "^0.9.13"
11+
"form-data": "^0.2.0",
12+
"oauth": "^0.9.13",
13+
"uuid": "^2.0.1"
1214
}
1315
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
OAuth2 = require("oauth").OAuth2
2+
express = require "express"
3+
https = require "https"
4+
FormData = require "form-data"
5+
uuid = require "uuid"
6+
fs = require "fs"
7+
8+
unless process.argv.length > 2
9+
console.log "Usage: coffee share-file.coffee myfile.png"
10+
return
11+
fileToShare = process.argv[2]
12+
13+
# Some configurations needed to talk with The Grid API
14+
config =
15+
host: "api.thegrid.io"
16+
port: 443
17+
path: "/share"
18+
app_id: process.env.THEGRID_APP_ID or ""
19+
app_secret: process.env.THEGRID_APP_SECRET or ""
20+
scopes: ["share"]
21+
authorization_url: "https://passport.thegrid.io/login/authorize"
22+
token_url: "https://passport.thegrid.io/login/authorize/token"
23+
callback_url: "http://localhost:3000/authenticated"
24+
25+
26+
# The Grid API supports OAuth2 authentication
27+
oauth = new OAuth2(
28+
config.app_id
29+
config.app_secret
30+
""
31+
config.authorization_url
32+
config.token_url
33+
null
34+
)
35+
36+
shareFile = (filePath, accessToken, callback) ->
37+
formData = new FormData()
38+
# It's interesting to use a valid UUID to keep track of the shared item
39+
formData.append "url", "content://#{uuid.v4()}"
40+
formData.append "type", "image/jpeg"
41+
formData.append "content", fs.createReadStream(filePath)
42+
opts =
43+
host: config.host
44+
port: config.port
45+
path: config.path
46+
method: "POST"
47+
headers: formData.getHeaders()
48+
# Add your `accessToken` to headers
49+
opts.headers["Authorization"] = "Bearer #{accessToken}"
50+
51+
req = https.request opts
52+
formData.pipe req
53+
54+
req.on "response", (result) ->
55+
console.log "Finished sharing #{result.headers.location}"
56+
callback()
57+
58+
# Create an app that will redirect us to The Grid `config.authorization_url` and
59+
# then back to our `config.callback_url`
60+
app = express()
61+
62+
app.get "/", (req, res) ->
63+
oauthURL = oauth.getAuthorizeUrl
64+
redirect_uri: config.callback_url
65+
scope: config.scopes
66+
response_type: "code"
67+
68+
body = "<a href=\"#{oauthURL}\">Share <b>#{fileToShare}</b> on The Grid</a>"
69+
res.send body
70+
71+
app.get "/authenticated", (req, res) ->
72+
oauth.getOAuthAccessToken(
73+
req.query.code
74+
{"redirect_uri": config.callback_url, "grant_type": "authorization_code"}
75+
(err, accessToken, refreshToken, results) ->
76+
if err?
77+
res.send "Error: #{err}"
78+
if results.error?
79+
res.send "Error: #{JSON.stringify(results)}"
80+
# Receive the token for this session at `accessToken`
81+
shareFile fileToShare, accessToken, () =>
82+
res.send "File <b>#{fileToShare}</b> shared on The Grid!"
83+
)
84+
85+
app.listen 3000
86+
console.log "Share your file at http://localhost:3000"

0 commit comments

Comments
 (0)