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)
43 (class << self; Application; end)
46 @depot = Depot.new(settings.depot_path) if @depot.nil? || settings.depot_path != @depot.path
51 class Application < Sinatra::Base
52 set :app_file, __FILE__
53 set :upload_password, '0e5f7d398e6f9cd1f6bac5cc823e363aec636495'
54 set :default_expire, 60
55 set :maximum_expire, 60 * 24 * 30 # 1 month
56 set :gone_period, 10080
57 set :filename_length, 20
58 set :random_pass_length, 16
59 set :depot_path, Proc.new { File.join(root, 'files') }
61 def password_match?(password)
62 return TRUE if settings.upload_password.nil?
63 (not password.nil?) && Digest::SHA1.hexdigest(password) == settings.upload_password
66 GetText::bindtextdomain('coquelicot')
68 GetText::set_current_locale(params[:lang] || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en')
72 content_type 'text/css', :charset => 'utf-8'
81 "#{Coquelicot.gen_random_pass}"
84 get '/ready/:link' do |link|
85 link, pass = link.split '-' if link.include? '-'
87 file = Coquelicot.depot.get_file(link, nil)
88 rescue Errno::ENOENT => ex
91 @expire_at = file.expire_at
92 @base = request.url.gsub(/\/ready\/[^\/]*$/, '')
98 @url = "#{@base}/#{@name}"
102 post '/authenticate' do
103 pass unless request.xhr?
104 unless password_match? params[:upload_password] then
105 error 403, "Forbidden"
111 unless password_match? params[:upload_password] then
114 if params[:file] then
115 tmpfile = params[:file][:tempfile]
116 name = params[:file][:filename]
118 if tmpfile.nil? || name.nil? then
119 @error = "No file selected"
122 if tmpfile.lstat.size == 0 then
123 @error = "#{name} is empty"
126 if params[:expire].nil? or params[:expire].to_i == 0 then
127 params[:expire] = settings.default_expire
128 elsif params[:expire].to_i > settings.maximum_expire then
131 expire_at = Time.now + 60 * params[:expire].to_i
132 one_time_only = params[:one_time] and params[:one_time] == 'true'
133 if params[:file_key].nil? or params[:file_key].empty?then
134 pass = Coquelicot.gen_random_pass
136 pass = params[:file_key]
138 src = params[:file][:tempfile]
139 link = Coquelicot.depot.add_file(
141 { "Expire-at" => expire_at.to_i,
142 "One-time-only" => one_time_only,
143 "Filename" => params[:file][:filename],
144 "Length" => src.stat.size,
145 "Content-Type" => params[:file][:type],
147 redirect "ready/#{link}-#{pass}" if params[:file_key].nil? or params[:file_key].empty?
148 redirect "ready/#{link}"
152 throw :halt, [410, haml(:expired)]
155 def send_stored_file(file)
156 last_modified file.created_at.httpdate
157 attachment file.meta['Filename']
158 response['Content-Length'] = "#{file.meta['Length']}"
159 response['Content-Type'] = file.meta['Content-Type'] || 'application/octet-stream'
160 throw :halt, [200, file]
163 def send_link(link, pass)
164 file = Coquelicot.depot.get_file(link, pass)
165 return false if file.nil?
166 return expired if file.expired?
168 if file.one_time_only?
170 # unlocking done in file.close
172 rescue Lockfile::TimeoutLockError
173 error 409, "Download currently in progress"
176 send_stored_file(file)
179 get '/:link-:pass' do |link, pass|
180 link = Coquelicot.remap_base32_extra_characters(link)
181 pass = Coquelicot.remap_base32_extra_characters(pass)
182 not_found unless send_link(link, pass)
185 get '/:link' do |link|
186 link = Coquelicot.remap_base32_extra_characters(link)
187 not_found unless Coquelicot.depot.file_exists? link
192 post '/:link' do |link|
193 pass = params[:file_key]
194 return 403 if pass.nil? or pass.empty?
196 # send Forbidden even if file is not found
197 return 403 unless send_link(link, pass)
198 rescue Coquelicot::BadKey => ex
205 url = request.scheme + "://"
207 if request.scheme == "https" && request.port != 443 ||
208 request.scheme == "http" && request.port != 80
209 url << ":#{request.port}"
211 url << request.script_name
218 Coquelicot::Application.run! if __FILE__ == $0