Guest User

Untitled

a guest
Jul 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. class UploadsController < ApplicationController
  2.  
  3. def finalize
  4. @file = StoredFile.find(params[:file])
  5. @file.upload_done!
  6. end
  7.  
  8. def authorize
  9. authorize_write_to_s3 do |stored_file, errors|
  10. success = stored_file.save
  11.  
  12. errors = []
  13. unless success
  14. errors += stored_file.errors.full_messages
  15. end
  16. [success, errors]
  17. end
  18. end
  19.  
  20. protected
  21.  
  22. def authorize_write_to_s3(https = false)
  23. raise "no block given" unless block_given?
  24.  
  25. bucket = S3Config.bucket
  26. access_key_id = S3Config.access_key_id
  27. acl = S3Config.acl
  28. content_type = params[:content_type].to_s
  29. file_size = params[:file_size].to_i
  30. unchecked_key = params[:file_name].to_s
  31.  
  32. stored_file = StoredFile.new do |file|
  33. file.easy_setup(unchecked_key, content_type)
  34. file.size = file_size
  35. end
  36.  
  37. success, errors = yield stored_file
  38.  
  39. if success
  40. content_type = stored_file.content_type
  41. full_key = stored_file.base_path
  42. expiration_date = 2.hours.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
  43.  
  44. # ['starts-with', '$Filename', ''],
  45.  
  46. policy = Base64.encode64(
  47. "{
  48. 'expiration': '#{expiration_date}',
  49. 'conditions': [
  50. {'bucket': '#{bucket}'},
  51. {'key': '#{full_key}'},
  52. {'acl': '#{acl}'},
  53. {'Content-Type': '#{content_type}'},
  54. {'success_action_status': '201'},
  55. ['content-length-range', #{file_size}, #{file_size}],
  56. ['starts-with', '$filename', ''],
  57. ]
  58. }").gsub(/\n|\r/, '')
  59.  
  60. signature = self.sign(policy, S3Config.secret_access_key)
  61. full_url = "#{https ? 'https' : 'http'}://#{S3Config.server}/"
  62.  
  63. respond_to do |wants|
  64. wants.json do
  65. render :json => {
  66. :ok => true,
  67. :url => full_url,
  68. :file => stored_file.id.to_s,
  69. :headers => [],
  70. :params => ActiveSupport::OrderedHash[*([
  71. ["key", full_key],
  72. ["AWSAccessKeyId", access_key_id],
  73. ["acl", acl],
  74. ["Content-Type", content_type],
  75. ["success_action_status", "201"],
  76. ["policy", policy],
  77. ["signature", signature],
  78. ["Filename", ""]
  79. ].flatten)]
  80. }
  81. end
  82. end
  83. else
  84. errors = [errors] unless errors.is_a? Array
  85. errors = ["Unknown Error"] if errors.blank?
  86.  
  87. respond_to do |wants|
  88. wants.json do
  89. render :json => {
  90. :ok => false,
  91. :alerts => errors
  92. }
  93. end
  94. end
  95. end
  96. end
  97.  
  98. def sign(message, secret)
  99. raise 'missing secret' unless secret
  100.  
  101. hmac = HMAC::SHA1.new(secret)
  102. hmac.update(message)
  103. hmaced = hmac.digest.to_s
  104. Base64.encode64(hmaced).chomp.gsub(/\n/,'')
  105. end
  106. end
Add Comment
Please, Sign In to add comment