lib/template/server.rb in browser_app_base-0.0.2 vs lib/template/server.rb in browser_app_base-0.0.3

- old
+ new

@@ -1,40 +1,57 @@ -require 'json' -require 'kconv' +require "json" +require "kconv" class Search < Sinatra::Base helpers Sinatra::Streaming - get '' do + get "" do q_hash = {} puts request.query_string request.query_string.split("&").each do |q| work = q.split("=") if work[1] != nil q_hash[work[0]] = CGI.unescape work[1].toutf8 else q_hash[work[0]] = "" end end - str = q_hash["path"].gsub(/\\/,"/") + str = q_hash["path"].gsub(/\\/, "/") puts "str=#{str}" + kind = q_hash["kind"].gsub(/\\/, "/") + puts "kind=#{kind}" res = [] str = str.gsub(/\\/, "/") dir = File.dirname(str) - dir = "c:/" if dir == nil file = File.basename(str) - file = "/" if file == nil + puts "dir=#{dir}" + puts "file=#{file}" + + kernel = Facter.value(:kernel) + if kernel == "windows" + dir = "c:/" if dir == nil + dir = "c:/" if dir == "/" + elsif kernel == "Linux" + dir = "/" if dir == nil + else + dir = "c:/" if dir == nil + dir = "c:/" if dir == "/" + end + path = "#{dir}/#{file}" - if File.exists?(path) + if File.directory?(path) path = path + "/*" else path = path + "*" end + path.gsub!(/[\/]+/, "/") puts path - Dir.glob(path).each do |file| + Dir.glob(path, File::FNM_DOTMATCH).each do |file| data = {} - data["label"] = File.basename(file) - data["value"] = file + next if File.basename(file) == "." + next if kind == "dir" and !File.directory?(file) + data["label"] = File.expand_path(file) + data["value"] = File.expand_path(file) res.push data end - JSON.generate res + JSON.generate res.sort { |a, b| a["value"] <=> b["value"] } end end