lib/gitdocs/server.rb in gitdocs-0.3.6 vs lib/gitdocs/server.rb in gitdocs-0.4.0

- old
+ new

@@ -1,9 +1,10 @@ require 'thin' require 'renee' require 'coderay' require 'uri' +require 'haml' module Gitdocs class Server def initialize(manager, *gitdocs) @manager = manager @@ -15,14 +16,14 @@ manager = @manager Thin::Server.start('127.0.0.1', port) do use Rack::Static, :urls => ['/css', '/js', '/img', '/doc'], :root => File.expand_path("../public", __FILE__) run Renee { if request.path_info == '/' - render! "home", :layout => 'app', :locals => {:gds => gds} + render! "home", :layout => 'app', :locals => {:conf => manager.config, :nav_state => "home" } else path 'settings' do - get.render! 'settings', :layout => 'app', :locals => {:conf => manager.config} + get.render! 'settings', :layout => 'app', :locals => {:conf => manager.config, :nav_state => "settings" } post do shares = manager.config.shares manager.config.global.update_attributes(request.POST['config']) request.POST['share'].each do |idx, share| if remote_branch = share.delete('remote_branch') @@ -43,24 +44,25 @@ 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} + locals = {:idx => idx, :parent => parent, :root => gd.root, :file_path => expanded_path, :nav_state => nil } mode, mime = request.params['mode'], `file -I #{ShellTools.escape(expanded_path)}`.strip - # puts "mode, mime: #{mode.inspect}, #{mime.inspect}" - if mode == 'save' # Saving + if mode == 'meta' # Meta + halt 200, { 'Content-Type' => 'application/json' }, gd.file_meta(file_path).to_json + elsif 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'] tempfile, filename = file[:tempfile], file[:filename] FileUtils.mv(tempfile.path, File.expand_path(filename, expanded_path)) redirect! "/" + idx.to_s + file_path + "/" + filename elsif !File.exist?(expanded_path) # edit for non-existent file render! "edit", :layout => 'app', :locals => locals.merge(:contents => "") - elsif File.directory?(expanded_path) - contents = Dir[File.join(gd.root, request.path_info, '*')] + elsif File.directory?(expanded_path) # list directory + contents = gd.dir_files(expanded_path) render! "dir", :layout => 'app', :locals => locals.merge(:contents => contents) elsif mode == 'delete' # delete file FileUtils.rm(expanded_path) redirect! "/" + idx.to_s + parent elsif mode == 'edit' && mime.match(%r{text/}) # edit file \ No newline at end of file