Sha256: 8c397ba0727c7e0cbaef7840c386a0c1328041558e2bf18e8476ac6def85f16b

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

require 'json'

class FileSystemAPI < Sinatra::Base

  get '/metadata' do
  	path = params[:path]
    ret = {}

  	if Dir.exist?(path)
      d = Dir.new(path)
      ret = FileSystem.dir_info(d)
  	elsif File.exist?(path)
      f = File.new(path)
      ret = FileSystem.file_info(f)
    else
      halt 404
    end
    ret.to_json
  end

  get '/file' do
    send_file params[:path]
  end

end


class FileSystem

  def self.dir_info(dir, list_files = "true")
    ret = {}
    ret[:name] = File.basename(dir.path)
    ret[:path] = dir.path
    ret[:absolute_path] = File.absolute_path(dir.path)
    ret[:is_dir] = true
    if list_files
      files = []
      dir.each do |name|
        begin
          f = File.new("#{dir.path}/#{name}")
          files << file_info(f)
        rescue
          #puts "error"
          #puts e
        end
      end
      ret[:contents] = files
    end
    ret
  end

  def self.file_info(file)
    if Dir.exist?(file.path)
      d = Dir.new(file.path)
      return dir_info(d, false)
    end
    ret = {}
    ret[:name] = File.basename(file.path)
    ret[:path] = file.path
    ret[:absolute_path] = File.absolute_path(file.path)
    ret[:is_dir] = false
    ret[:bytes] = file.size
    ret[:client_mtime] = file.mtime
    ret[:modified] = file.ctime
    ret[:mime_type] = Rack::Mime.mime_type("." + file.path.split('.').last)
    ret[:binary] = File.binary?(ret[:absolute_path])
    ret
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
daftos-0.4.0 lib/apis/filesystem_api.rb
daftos-0.3.0 lib/apis/filesystem_api.rb