Advertisement
Guest User

Untitled

a guest
Dec 4th, 2013
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. config = require './config.js'
  2.  
  3. # Filesystem
  4. fs = require 'fs'
  5.  
  6. # Crypto
  7. crypto = require 'crypto'
  8.  
  9. # Request
  10. request = require 'request'
  11.  
  12. # Express
  13. express = require 'express'
  14. application = express()
  15.  
  16. # Express add bodyparser middleware
  17. application.use \
  18.     express.bodyParser \
  19.         uploadDir: 'tmp/'
  20.  
  21. # Application logic here
  22.  
  23. # Compute the signature for vuforia
  24. compute_signature = (data, verb, request_path, today)->
  25.     md5_hash = crypto.createHash 'md5'
  26.     md5_hash.update(JSON.stringify data)
  27.  
  28.     string_to_sign =
  29.         # Add the verb
  30.         verb + "\n" +
  31.         # Content-MD5
  32.         md5_hash.digest('hex') + "\n" +
  33.         # Content-type
  34.         "application/json" + "\n" +
  35.         # Date in RFC 2616 format / rfc1123-date
  36.         today.toUTCString() + "\n" +
  37.         # Request path
  38.         request_path
  39.  
  40.     console.log 'String to sign: --------------------'
  41.     console.log string_to_sign
  42.     console.log '------------------------------------'
  43.  
  44.     hmac_sha1 = crypto.createHmac('sha1', config.server_secret_key).update(string_to_sign).digest('hex')
  45.  
  46.     signature = new Buffer(hmac_sha1).toString('base64')
  47.  
  48.     return signature
  49.  
  50. # Main function to post to vuforia
  51. vuforia_request_completed = (error, response, body)->
  52.     console.log 'Vuforia completed: ------------------------------------'
  53.     console.log "Error: #{error}"
  54.     #console.log "Response: "
  55.     #console.log response
  56.     console.log "Body: "
  57.     console.log body
  58.     console.log "-------------------------------------------------------"
  59.  
  60.  
  61. vuforia_send = (req)->
  62.     # Read the image into a base64 encoded string
  63.     req.body.image_base64 = fs.readFileSync \
  64.         req.files.image.path
  65.         , encoding: 'base64'
  66.  
  67.     # Create the data to be posted to vuforia
  68.     req.send_data =
  69.         name: req.body.name
  70.         width: req.body.width
  71.         image: req.body.image_base64
  72.         active_flag: true
  73.         application_metadata: req.body.application_metadata
  74.  
  75.     today = new Date()
  76.  
  77.     signature = compute_signature req.send_data, 'POST', '/targets', today
  78.  
  79.     request_options =
  80.         url: config.urls.add_target
  81.         method: 'POST'
  82.         body: JSON.stringify(req.send_data)
  83.         headers:
  84.             'Authorization': "VWS #{config.server_access_key}:#{signature}"
  85.             'Content-type': 'application/json'
  86.  
  87.     request \
  88.         request_options
  89.         , vuforia_request_completed
  90.  
  91.     console.log "Vuforia request sent!"
  92.  
  93.     true
  94.  
  95. marker_set = (req, res)->
  96.         # http://jsfiddle.net/gg9tM/ - example post form
  97.  
  98.         res.response_data =
  99.             status: 'ok'
  100.             message: 'request unprocessed'
  101.  
  102.         vuforia_send req
  103.  
  104.         res.send(JSON.stringify res.response_data)
  105.  
  106.         # do the vuforia post
  107.  
  108.         true
  109.  
  110. # Set the routes
  111. ## Setting the markers
  112. application.post \
  113.     '/marker/set'
  114.     , marker_set
  115.  
  116. ## Checking markers status
  117. # todo: ...
  118.  
  119. application.listen config.port
  120. console.log "Application listening on port #{config.port}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement