lib/gitdocs/server.rb in gitdocs-0.3.2 vs lib/gitdocs/server.rb in gitdocs-0.3.3
- old
+ new
@@ -1,8 +1,9 @@
require 'thin'
require 'renee'
require 'coderay'
+require 'uri'
module Gitdocs
class Server
def initialize(manager, *gitdocs)
@manager = manager
@@ -35,19 +36,20 @@
end
var :int do |idx|
gd = gds[idx]
halt 404 if gd.nil?
- file_path = request.path_info
+ file_path = URI.unescape(request.path_info)
file_ext = File.extname(file_path)
expanded_path = File.expand_path(".#{file_path}", gd.root)
halt 400 unless expanded_path[/^#{Regexp.quote(gd.root)}/]
parent = File.dirname(file_path)
parent = '' if parent == '/'
parent = nil if parent == '.'
locals = {:idx => idx, :parent => parent, :root => gd.root, :file_path => expanded_path}
- mode, mime = request.params['mode'], `file -I #{expanded_path}`.strip
+ mode, mime = request.params['mode'], `file -I #{ShellTools.escape(expanded_path)}`.strip
+ puts "mode, mime: #{mode.inspect}, #{mime.inspect}"
if mode == 'save' # Saving
File.open(expanded_path, 'w') { |f| f.print request.params['data'] }
redirect! "/" + idx.to_s + file_path
elsif mode == 'upload' # Uploading
halt 404 unless file = request.params['file']
@@ -63,14 +65,18 @@
FileUtils.rm(expanded_path)
redirect! "/" + idx.to_s + parent
elsif mode == 'edit' && mime.match(%r{text/}) # edit file
contents = File.read(expanded_path)
render! "edit", :layout => 'app', :locals => locals.merge(:contents => contents)
- elsif mode != 'raw' && mime.match(%r{text/}) # render file
+ elsif mode != 'raw' # render file
begin # attempting to render file
contents = '<div class="tilt">' + Tilt.new(expanded_path).render + '</div>'
rescue RuntimeError => e # not tilt supported
- contents = '<pre class="CodeRay">' + CodeRay.scan_file(expanded_path).encode(:html) + '</pre>'
+ contents = if mime.match(%r{text/})
+ '<pre class="CodeRay">' + CodeRay.scan_file(expanded_path).encode(:html) + '</pre>'
+ else
+ %|<embed class="inline-file" src="/#{idx}#{request.path_info}?mode=raw"></embed>|
+ end
end
render! "file", :layout => 'app', :locals => locals.merge(:contents => contents)
else # other file
run! Rack::File.new(gd.root)
end
\ No newline at end of file