|
| 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