8 enable :inline_templates
10 set :upload_password, '0e5f7d398e6f9cd1f6bac5cc823e363aec636495'
15 def self.open(path, pass)
16 StoredFile.new(path, pass)
21 yield @initial_content
22 @initial_content = nil
23 until (buf = @file.read(BUFFER_LEN)).nil?
24 yield @cipher.update(buf)
31 def self.create(src, pass, meta)
33 clear_meta = { "Coquelicot" => COQUELICOT_VERSION,
34 "Salt" => Base64.encode64(salt).strip }
35 yield YAML.dump(clear_meta) + YAML_START
37 cipher = get_cipher(pass, salt, :encrypt)
38 yield cipher.update(YAML.dump(meta) + YAML_START)
40 while not (buf = src.read(BUFFER_LEN)).nil?
41 yield cipher.update(buf)
49 CIPHER = 'AES-256-CBC'
52 COQUELICOT_VERSION = "1.0"
54 def self.get_cipher(pass, salt, method)
55 hmac = OpenSSL::PKCS5.pbkdf2_hmac_sha1(pass, salt, 2000, 48)
56 cipher = OpenSSL::Cipher.new CIPHER
57 cipher.method(method).call
58 cipher.key = hmac[0..31]
59 cipher.iv = hmac[32..-1]
64 OpenSSL::Random::random_bytes(SALT_LEN)
67 def initialize(path, pass)
68 @file = File.open(path)
69 if YAML_START != (buf = @file.read(YAML_START.length)) then
70 raise "unknown file, read #{buf.inspect}"
73 init_decrypt_cipher pass
79 until YAML_START == (line = @file.readline) do
82 @meta = YAML.load(meta)
83 if @meta["Coquelicot"].nil? or @meta["Coquelicot"] != COQUELICOT_VERSION then
88 def init_decrypt_cipher(pass)
89 salt = Base64.decode64(@meta["Salt"])
90 @cipher = StoredFile::get_cipher(pass, salt, :decrypt)
95 buf = @file.read(BUFFER_LEN)
96 content = @cipher.update(buf)
97 raise "bad key" unless content.start_with? YAML_START
99 block = content.split(YAML_START, 3)
101 if block.length == 3 then
102 @initial_content = block[2]
103 @meta.merge! YAML.load(yaml)
107 until (buf = @file.read(BUFFER_LEN)).nil? do
108 block = @cipher.update(buf).split(YAML_START, 3)
110 break if block.length == 2
112 @initial_content = block[1]
113 @meta.merge! YAML.load(yaml)
117 @cipher.reset unless @cipher.nil?
122 def password_match?(password)
123 return TRUE if settings.upload_password.nil?
124 (not password.nil?) && Digest::SHA1.hexdigest(password) == settings.upload_password
127 def uploaded_file(file)
128 "#{options.depot_path}/#{file}"
132 content_type 'text/css', :charset => 'utf-8'
140 get '/ready/:name' do |name|
141 path = uploaded_file(name)
142 unless File.exists? path then
145 base = request.url.gsub(/\/ready\/[^\/]*$/, '')
146 @url = "#{base}/#{name}"
150 get '/:name' do |name|
151 path = uploaded_file(name)
152 unless File.exists? path then
155 file = StoredFile.open(path, 'XXXsecret')
156 last_modified File.mtime(path).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]
164 unless password_match? params[:upload_password] then
167 if params[:file] then
168 tmpfile = params[:file][:tempfile]
169 name = params[:file][:filename]
171 if tmpfile.nil? || name.nil? then
172 @error = "No file selected"
175 src = params[:file][:tempfile]
176 File.open(uploaded_file(name), 'w') do |dest|
180 { "Filename" => params[:file][:filename],
181 "Length" => src.stat.size,
182 "Content-Type" => params[:file][:type]
183 }) { |data| dest.write data }
185 redirect "ready/#{name}"
190 url = request.scheme + "://"
192 if request.scheme == "https" && request.port != 443 ||
193 request.scheme == "http" && request.port != 80
194 url << ":#{request.port}"
196 url << request.script_name
207 %base{ :href => base_href }
208 %link{ :rel => 'stylesheet', :href => "style.css", :type => 'text/css',
209 :media => "screen, projection" }
210 %script{ :type => 'text/javascript', :src => 'javascripts/jquery.min.js' }
211 %script{ :type => 'text/javascript', :src => 'javascripts/jquery.lightBoxFu.js' }
212 %script{ :type => 'text/javascript', :src => 'javascripts/jquery.uploadProgress.js' }
213 %script{ :type => 'text/javascript', :src => 'javascripts/coquelicot.js' }
222 %form#upload{ :enctype => 'multipart/form-data',
223 :action => 'upload', :method => 'post' }
225 %input{ :type => 'file', :name => 'file' }
227 %input{ :type => 'submit', :value => 'Send file' }
232 %a{ :href => @url }= @url
238 background-color: $green
243 text-decoration: underline
247 background-color: red
249 border: black solid 1px
257 background: url('images/ajax-loader.gif') no-repeat