Class: SC::Rack::Filesystem

Inherits:
Object
  • Object
show all
Defined in:
lib/sproutcore/rack/filesystem.rb

Overview

Sends and modifies files in the local file system. Uses Rack::Rack::File to transfer the file, which makes this work with rack/sendfile and other middleware. Some code originally adapted from Rack::Rack::File.

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ Filesystem

Returns a new instance of Filesystem.



22
23
24
# File 'lib/sproutcore/rack/filesystem.rb', line 22

def initialize(project)
  @project = project
end

Instance Method Details

#call(env) ⇒ Object

Accepts the following commands

GET /foo/bar/blah

  • send blah back to the client

POST /foo/bar/blah with no “action” body parameter

  • also sends contents of blah back to the client (defeats caching)

POST to /foo/bar/blah with “action=save” and “file” body parameters

  • create a file named “blah” with contents of file OR overwrite “blah” with contents of file; create /foo/bar if needed

POST to /foo/bar/blah with “action=touch” body parameter

  • create an empty file named “blah” OR update the timestamp of blah; create /foo/bar if needed

POST to /foo/bar/blah with “action=makedir” body parameter

  • create a directory named “blah” and create parent directories if needed

POST to /foo/bar/blah with “action=append” and “file” body parameters

  • append the contents of the file body parameter to the blah file; return error if “blah” doesn’t exist

POST to /foo/bar/blah with “action=remove” body parameter

  • delete a file or directory named “blah”; will fail if directory is not empty



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sproutcore/rack/filesystem.rb', line 60

def call(env)
  regex = /^\/sproutcore\/fs/
  params = ::Rack::Request.new(env).params
  
  path = env["PATH_INFO"]
  return [404, {}, "not found"] unless path =~ regex
  
  path = path.sub regex, '' # remove path prefix
  action = params['action']
  
  case action
  when nil
    send_file(path)
  when 'save'
    save_file(path,params)
  when 'append'
    append_file(path,params)
  when 'touch'
    touch_file(path)
  when 'makedir'
    make_directory(path)
  when 'remove'
    remove_path(path)
  else
    forbidden("Unknown action #{params['action']}")
  end
rescue StandardError
  return forbidden("Cannot #{action} #{path} due to #{$!.message}")
end

#root_dirObject



26
27
28
29
30
31
# File 'lib/sproutcore/rack/filesystem.rb', line 26

def root_dir
  unless @root_dir
    @root_dir = @project.project_root
  end
  return @root_dir
end