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}"
54 unless password_match? params[:upload_password] then
58 tmpfile = params[:file][:tempfile]
59 name = params[:file][:filename]
61 if tmpfile.nil? || name.nil? then
62 @error = "No file selected"
65 if params[:expire].nil? or params[:expire].to_i == 0 then
66 params[:expire] = Coquelicot.settings.default_expire
67 elsif params[:expire].to_i > Coquelicot.settings.maximum_expire then
70 expire_at = Time.now + 60 * params[:expire].to_i
71 one_time_only = params[:one_time] and params[:one_time] == 'true'
72 if params[:file_key].nil? or params[:file_key].empty?then
73 pass = Coquelicot.gen_random_pass
75 pass = params[:file_key]
77 src = params[:file][:tempfile]
78 link = Coquelicot.depot.add_file(
80 { "Expire-at" => expire_at.to_i,
81 "One-time-only" => one_time_only,
82 "Filename" => params[:file][:filename],
83 "Length" => src.stat.size,
84 "Content-Type" => params[:file][:type],
86 redirect "ready/#{link}-#{pass}" if params[:file_key].nil? or params[:file_key].empty?
87 redirect "ready/#{link}"
91 throw :halt, [410, haml(:expired)]
94 def send_stored_file(file)
95 last_modified file.created_at.httpdate
96 attachment file.meta['Filename']
97 response['Content-Length'] = "#{file.meta['Length']}"
98 response['Content-Type'] = file.meta['Content-Type'] || 'application/octet-stream'
99 throw :halt, [200, file]
105 @lockfile ||= Lockfile.new "#{File.expand_path(@path)}.lock", :timeout => 4
110 yield @initial_content
111 @initial_content = nil
112 until (buf = @file.read(BUFFER_LEN)).nil?
113 yield @cipher.update(buf)
126 empty! if @fully_sent
133 def send_link(link, pass)
134 file = Coquelicot.depot.get_file(link, pass)
135 return false if file.nil?
136 return expired if file.expired?
138 if file.one_time_only?
140 # unlocking done in file.close
142 rescue Lockfile::TimeoutLockError
143 error 409, "Download currently in progress"
146 send_stored_file(file)
149 get '/:link-:pass' do |link, pass|
150 link = Coquelicot.remap_base32_extra_characters(link)
151 pass = Coquelicot.remap_base32_extra_characters(pass)
152 not_found unless send_link(link, pass)
155 get '/:link' do |link|
156 link = Coquelicot.remap_base32_extra_characters(link)
157 not_found unless Coquelicot.depot.file_exists? link
162 post '/:link' do |link|
163 pass = params[:file_key]
164 return 403 if pass.nil? or pass.empty?
166 # send Forbidden even if file is not found
167 return 403 unless send_link(link, pass)
168 rescue Coquelicot::BadKey => ex
175 url = request.scheme + "://"
177 if request.scheme == "https" && request.port != 443 ||
178 request.scheme == "http" && request.port != 80
179 url << ":#{request.port}"
181 url << request.script_name