Sha256: b3604adc6e79c8276a2536f08bec81ee7c5b327802bba57f9e2230927dbf38cb

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require "json"
require "kconv"

class Search < Sinatra::Base
  helpers Sinatra::Streaming
  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(/\\/, "/")
    puts "str=#{str}"
    kind = q_hash["kind"].gsub(/\\/, "/")
    puts "kind=#{kind}"
    res = []
    str = str.gsub(/\\/, "/")
    dir = File.dirname(str)
    file = File.basename(str)
    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.directory?(path)
      path = path + "/*"
    else
      path = path + "*"
    end
    path.gsub!(/[\/]+/, "/")
    puts path
    Dir.glob(path, File::FNM_DOTMATCH).each do |file|
      data = {}
      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.sort { |a, b| a["value"] <=> b["value"] }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
browser_app_base-0.0.4 lib/template/server.rb
browser_app_base-0.0.3 lib/template/server.rb