1 $:.unshift File.join(File.dirname(__FILE__), 'lib')
14 @lockfile ||= Lockfile.new "#{File.expand_path(@path)}.lock", :timeout => 4
19 yield @initial_content
20 @initial_content = nil
21 until (buf = @file.read(BUFFER_LEN)).nil?
22 yield @cipher.update(buf)
41 class Application < Sinatra::Base
42 set :upload_password, '0e5f7d398e6f9cd1f6bac5cc823e363aec636495'
44 def password_match?(password)
45 return TRUE if settings.upload_password.nil?
46 (not password.nil?) && Digest::SHA1.hexdigest(password) == settings.upload_password
49 GetText::bindtextdomain('coquelicot')
51 GetText::set_current_locale(params[:lang] || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en')
55 content_type 'text/css', :charset => 'utf-8'
64 "#{Coquelicot.gen_random_pass}"
67 get '/ready/:link' do |link|
68 link, pass = link.split '-' if link.include? '-'
70 file = Coquelicot.depot.get_file(link, nil)
71 rescue Errno::ENOENT => ex
74 @expire_at = file.expire_at
75 @base = request.url.gsub(/\/ready\/[^\/]*$/, '')
81 @url = "#{@base}/#{@name}"
85 post '/authenticate' do
86 pass unless request.xhr?
87 unless password_match? params[:upload_password] then
88 error 403, "Forbidden"
94 unless password_match? params[:upload_password] then
98 tmpfile = params[:file][:tempfile]
99 name = params[:file][:filename]
101 if tmpfile.nil? || name.nil? then
102 @error = "No file selected"
105 if params[:expire].nil? or params[:expire].to_i == 0 then
106 params[:expire] = Coquelicot.settings.default_expire
107 elsif params[:expire].to_i > Coquelicot.settings.maximum_expire then
110 expire_at = Time.now + 60 * params[:expire].to_i
111 one_time_only = params[:one_time] and params[:one_time] == 'true'
112 if params[:file_key].nil? or params[:file_key].empty?then
113 pass = Coquelicot.gen_random_pass
115 pass = params[:file_key]
117 src = params[:file][:tempfile]
118 link = Coquelicot.depot.add_file(
120 { "Expire-at" => expire_at.to_i,
121 "One-time-only" => one_time_only,
122 "Filename" => params[:file][:filename],
123 "Length" => src.stat.size,
124 "Content-Type" => params[:file][:type],
126 redirect "ready/#{link}-#{pass}" if params[:file_key].nil? or params[:file_key].empty?
127 redirect "ready/#{link}"
131 throw :halt, [410, haml(:expired)]
134 def send_stored_file(file)
135 last_modified file.created_at.httpdate
136 attachment file.meta['Filename']
137 response['Content-Length'] = "#{file.meta['Length']}"
138 response['Content-Type'] = file.meta['Content-Type'] || 'application/octet-stream'
139 throw :halt, [200, file]
142 def send_link(link, pass)
143 file = Coquelicot.depot.get_file(link, pass)
144 return false if file.nil?
145 return expired if file.expired?
147 if file.one_time_only?
149 # unlocking done in file.close
151 rescue Lockfile::TimeoutLockError
152 error 409, "Download currently in progress"
155 send_stored_file(file)
158 get '/:link-:pass' do |link, pass|
159 link = Coquelicot.remap_base32_extra_characters(link)
160 pass = Coquelicot.remap_base32_extra_characters(pass)
161 not_found unless send_link(link, pass)
164 get '/:link' do |link|
165 link = Coquelicot.remap_base32_extra_characters(link)
166 not_found unless Coquelicot.depot.file_exists? link
171 post '/:link' do |link|
172 pass = params[:file_key]
173 return 403 if pass.nil? or pass.empty?
175 # send Forbidden even if file is not found
176 return 403 unless send_link(link, pass)
177 rescue Coquelicot::BadKey => ex
184 url = request.scheme + "://"
186 if request.scheme == "https" && request.port != 443 ||
187 request.scheme == "http" && request.port != 80
188 url << ":#{request.port}"
190 url << request.script_name
197 Coquelicot::Application.run! if __FILE__ == $0