1 $:.unshift File.join(File.dirname(__FILE__), 'lib')
10 set :upload_password, '0e5f7d398e6f9cd1f6bac5cc823e363aec636495'
12 def password_match?(password)
13 return TRUE if settings.upload_password.nil?
14 (not password.nil?) && Digest::SHA1.hexdigest(password) == settings.upload_password
17 GetText::bindtextdomain('coquelicot')
19 GetText::set_current_locale(params[:lang] || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en')
23 content_type 'text/css', :charset => 'utf-8'
32 "#{Coquelicot.gen_random_pass}"
35 get '/ready/:link' do |link|
36 link, pass = link.split '-' if link.include? '-'
38 file = Coquelicot.depot.get_file(link, nil)
39 rescue Errno::ENOENT => ex
42 @expire_at = file.expire_at
43 @base = request.url.gsub(/\/ready\/[^\/]*$/, '')
49 @url = "#{@base}/#{@name}"
53 post '/authenticate' do
54 pass unless request.xhr?
55 unless password_match? params[:upload_password] then
56 error 403, "Forbidden"
62 unless password_match? params[:upload_password] then
66 tmpfile = params[:file][:tempfile]
67 name = params[:file][:filename]
69 if tmpfile.nil? || name.nil? then
70 @error = "No file selected"
73 if params[:expire].nil? or params[:expire].to_i == 0 then
74 params[:expire] = Coquelicot.settings.default_expire
75 elsif params[:expire].to_i > Coquelicot.settings.maximum_expire then
78 expire_at = Time.now + 60 * params[:expire].to_i
79 one_time_only = params[:one_time] and params[:one_time] == 'true'
80 if params[:file_key].nil? or params[:file_key].empty?then
81 pass = Coquelicot.gen_random_pass
83 pass = params[:file_key]
85 src = params[:file][:tempfile]
86 link = Coquelicot.depot.add_file(
88 { "Expire-at" => expire_at.to_i,
89 "One-time-only" => one_time_only,
90 "Filename" => params[:file][:filename],
91 "Length" => src.stat.size,
92 "Content-Type" => params[:file][:type],
94 redirect "ready/#{link}-#{pass}" if params[:file_key].nil? or params[:file_key].empty?
95 redirect "ready/#{link}"
99 throw :halt, [410, haml(:expired)]
102 def send_stored_file(file)
103 last_modified file.created_at.httpdate
104 attachment file.meta['Filename']
105 response['Content-Length'] = "#{file.meta['Length']}"
106 response['Content-Type'] = file.meta['Content-Type'] || 'application/octet-stream'
107 throw :halt, [200, file]
113 @lockfile ||= Lockfile.new "#{File.expand_path(@path)}.lock", :timeout => 4
118 yield @initial_content
119 @initial_content = nil
120 until (buf = @file.read(BUFFER_LEN)).nil?
121 yield @cipher.update(buf)
134 empty! if @fully_sent
141 def send_link(link, pass)
142 file = Coquelicot.depot.get_file(link, pass)
143 return false if file.nil?
144 return expired if file.expired?
146 if file.one_time_only?
148 # unlocking done in file.close
150 rescue Lockfile::TimeoutLockError
151 error 409, "Download currently in progress"
154 send_stored_file(file)
157 get '/:link-:pass' do |link, pass|
158 link = Coquelicot.remap_base32_extra_characters(link)
159 pass = Coquelicot.remap_base32_extra_characters(pass)
160 not_found unless send_link(link, pass)
163 get '/:link' do |link|
164 link = Coquelicot.remap_base32_extra_characters(link)
165 not_found unless Coquelicot.depot.file_exists? link
170 post '/:link' do |link|
171 pass = params[:file_key]
172 return 403 if pass.nil? or pass.empty?
174 # send Forbidden even if file is not found
175 return 403 unless send_link(link, pass)
176 rescue Coquelicot::BadKey => ex
183 url = request.scheme + "://"
185 if request.scheme == "https" && request.port != 443 ||
186 request.scheme == "http" && request.port != 80
187 url << ":#{request.port}"
189 url << request.script_name