1 $:.unshift File.join(File.dirname(__FILE__), 'lib')
9 require 'coquelicot/configure'
10 require 'haml_gettext'
15 @lockfile ||= Lockfile.new "#{File.expand_path(@path)}.lock", :timeout => 4
20 yield @initial_content
21 @initial_content = nil
22 until (buf = @file.read(BUFFER_LEN)).nil?
23 yield @cipher.update(buf)
44 (class << self; Application; end)
47 @depot = Depot.new(settings.depot_path) if @depot.nil? || settings.depot_path != @depot.path
52 class Application < Sinatra::Base
53 set :app_file, __FILE__
54 include Coquelicot::Configure
56 GetText::bindtextdomain('coquelicot')
58 GetText::set_current_locale(params[:lang] || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en')
62 content_type 'text/css', :charset => 'utf-8'
71 "#{Coquelicot.gen_random_pass}"
74 get '/ready/:link' do |link|
75 link, pass = link.split '-' if link.include? '-'
77 file = Coquelicot.depot.get_file(link, nil)
78 rescue Errno::ENOENT => ex
81 @expire_at = file.expire_at
82 @base = request.url.gsub(/\/ready\/[^\/]*$/, '')
88 @url = "#{@base}/#{@name}"
92 post '/authenticate' do
93 pass unless request.xhr?
94 unless authenticate(params) then
95 error 403, "Forbidden"
101 # if JS is disabled upload_token might be nil
102 params['upload_token'] = JSON.parse(params['upload_token']) unless params['upload_token'].nil?
103 unless authenticate(params) then
106 if params[:file] then
107 tmpfile = params[:file][:tempfile]
108 name = params[:file][:filename]
110 if tmpfile.nil? || name.nil? then
111 @error = "No file selected"
114 if tmpfile.lstat.size == 0 then
115 @error = "#{name} is empty"
118 if params[:expire].nil? or params[:expire].to_i == 0 then
119 params[:expire] = settings.default_expire
120 elsif params[:expire].to_i > settings.maximum_expire then
123 expire_at = Time.now + 60 * params[:expire].to_i
124 one_time_only = params[:one_time] and params[:one_time] == 'true'
125 if params[:file_key].nil? or params[:file_key].empty?then
126 pass = Coquelicot.gen_random_pass
128 pass = params[:file_key]
130 src = params[:file][:tempfile]
131 link = Coquelicot.depot.add_file(
133 { "Expire-at" => expire_at.to_i,
134 "One-time-only" => one_time_only,
135 "Filename" => params[:file][:filename],
136 "Length" => src.stat.size,
137 "Content-Type" => params[:file][:type],
139 redirect "ready/#{link}-#{pass}" if params[:file_key].nil? or params[:file_key].empty?
140 redirect "ready/#{link}"
144 throw :halt, [410, haml(:expired)]
147 def send_stored_file(file)
148 last_modified file.created_at.httpdate
149 attachment file.meta['Filename']
150 response['Content-Length'] = "#{file.meta['Length']}"
151 response['Content-Type'] = file.meta['Content-Type'] || 'application/octet-stream'
152 throw :halt, [200, file]
155 def send_link(link, pass)
156 file = Coquelicot.depot.get_file(link, pass)
157 return false if file.nil?
158 return expired if file.expired?
160 if file.one_time_only?
162 # unlocking done in file.close
164 rescue Lockfile::TimeoutLockError
165 error 409, "Download currently in progress"
168 send_stored_file(file)
171 get '/:link-:pass' do |link, pass|
172 link = Coquelicot.remap_base32_extra_characters(link)
173 pass = Coquelicot.remap_base32_extra_characters(pass)
174 not_found unless send_link(link, pass)
177 get '/:link' do |link|
178 link = Coquelicot.remap_base32_extra_characters(link)
179 not_found unless Coquelicot.depot.file_exists? link
184 post '/:link' do |link|
185 pass = params[:file_key]
186 return 403 if pass.nil? or pass.empty?
188 # send Forbidden even if file is not found
189 return 403 unless send_link(link, pass)
190 rescue Coquelicot::BadKey => ex
197 url = request.scheme + "://"
199 if request.scheme == "https" && request.port != 443 ||
200 request.scheme == "http" && request.port != 80
201 url << ":#{request.port}"
203 url << request.script_name
208 Coquelicot.settings.auth_method
214 Coquelicot::Application.run! if __FILE__ == $0