Class: OpenS3
- Inherits:
-
Object
- Object
- OpenS3
- Defined in:
- lib/opens3/opens3.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #download_file(query) ⇒ Object
- #handle_url(data) ⇒ Object
-
#initialize(path, token = nil) ⇒ OpenS3
constructor
A new instance of OpenS3.
- #send_error(type) ⇒ Object
- #send_file_list(bucket) ⇒ Object
- #send_server_info(request) ⇒ Object
- #upload_file(params, bucket) ⇒ Object
Constructor Details
#initialize(path, token = nil) ⇒ OpenS3
Returns a new instance of OpenS3.
3 4 5 6 7 |
# File 'lib/opens3/opens3.rb', line 3 def initialize(path, token=nil) @file_path = path @srv_token = token ? Digest::SHA512.hexdigest(token) : Digest::SHA512.hexdigest("OpenS3") puts "Token: #{@srv_token}" end |
Instance Method Details
#call(env) ⇒ Object
9 10 11 12 |
# File 'lib/opens3/opens3.rb', line 9 def call(env) handle_url(env) end |
#download_file(query) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/opens3/opens3.rb', line 103 def download_file(query) query = Rack::Utils.parse_nested_query(query) file_name = query['name'] expiry = Time.at(query['expire'].to_i) if expiry < Time.now or expiry.nil? send_error(:expired_link) else if query['bucket'].nil? send_error(:bucket_not_specified) else if !query['name'].nil? if Dir.exists?("#{@file_path}/#{query['bucket']}") files = Dir["#{@file_path}/#{query['bucket']}/*/"].map { |a| File.basename(a) } files.each do |d| = YAML::load(File.open("#{@file_path}/#{query['bucket']}/#{d}/meta")) if ['filename'] == file_name @good_folder = d @good_meta = end end if @good_folder.nil? send_error(:file_not_found) else [200, {"Content-Type" => "#{@good_meta['type']}; charset=UTF-8", "Content-Disposition" => "attachment; filename=#{@good_meta['filename']}"}, File.open("#{@file_path}/#{query['bucket']}/#{@good_folder}/content")] end else send_error(:file_not_found) end else [200, {"Content-Type" => "application/json"}, "#{{:error=>true}.to_json}"] end end end end |
#handle_url(data) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/opens3/opens3.rb', line 14 def handle_url(data) req = Rack::Request.new(data) @path = req.path parsed_query = Rack::Utils.parse_nested_query(req.query_string) get_token = parsed_query['token'] post_token = req.params['token'] if @path == '/' [200, {"Content-Type" => "text/html"}, %{<!DOCTYPE html5><html><head><title></title></head><body><form action="http://localhost:8000/upload" enctype="multipart/form-data" method="post">Select file: <input name="file" type="file" /><br /><input type="text" name="bucket" placeholder="bucket name"/><input type="submit" value="Send" /><input type="text" name="token"></form></body></html>}] elsif @path == '/info' send_server_info(req) else if req.post? if post_token == @srv_token params = req.params['file'] bucket = req.params['bucket'] if !params.nil? case @path when '/upload' upload_file(params, bucket) else send_error(:wrong_path) end else send_error(:no_file) end else send_error(:token_error) end elsif req.get? if get_token == @srv_token case @path when '/file' download_file(req.query_string) when '/list' send_file_list(req.query_string) else send_error(:wrong_path) end else send_error(:token_error) end end end end |
#send_error(type) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/opens3/opens3.rb', line 140 def send_error(type) case type when :wrong_path [404, {"Content-Type" => "application/json"}, "#{{:error=>true, :type=>100}.to_json}"] when :file_not_found [404, {"Content-Type" => "application/json"}, "#{{:error=>true, :type=>101}.to_json}"] when :file_already_exists [200, {"Content-Type" => "application/json"}, "#{{:error=>true, :type=>102}.to_json}"] when :bucket_not_found [404, {"Content-Type" => "application/json"}, "#{{:error=>true, :type=>103}.to_json}"] when :bucket_not_specified [400, {"Content-Type" => "application/json"}, "#{{:error=>true, :type=>104}.to_json}"] when :expired_link [200, {"Content-Type" => "application/json"}, "#{{:error=>true, :type=>105}.to_json}"] when :token_error [403, {"Content-Type" => "application/json"}, "#{{:error=>true, :type=>106}.to_json}"] when :no_file [200, {"Content-Type" => "application/json"}, "#{{:error=>true, :type=>107}.to_json}"] end end |
#send_file_list(bucket) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/opens3/opens3.rb', line 83 def send_file_list(bucket) query = Rack::Utils.parse_nested_query(bucket) if query['bucket'].nil? send_error(:bucket_not_specified) else if Dir.exists?("#{@file_path}/#{query['bucket']}") files = Dir["#{@file_path}/#{query['bucket']}/*/"].map { |a| File.basename(a) } file_names = Array.new files.each do |d| = YAML::load(File.open("#{@file_path}/#{query['bucket']}/#{d}/meta")) file_names.push(['filename']) end [200, {"Content-Type" => "application/json"}, "#{file_names.to_json}"] else send_error(:bucket_not_found) end end end |
#send_server_info(request) ⇒ Object
161 162 163 |
# File 'lib/opens3/opens3.rb', line 161 def send_server_info(request) [200, {"Content-Type" => "application/json"}, "#{{:hostname=>request.host_with_port}.to_json}"] end |
#upload_file(params, bucket) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/opens3/opens3.rb', line 66 def upload_file(params, bucket) if bucket.nil? or bucket.empty? send_error(:bucket_not_specified) else f = RackFile.new(params[:tempfile], params[:filename], params[:type], bucket, @file_path).save if f @response = {:saved=>true, :url=>URI.escape("/file?name=#{params[:filename]}&bucket=#{bucket}")} [200, {"Content-Type" => "application/json"}, "#{@response.to_json}"] else send_error(:file_already_exists) end end end |