filesystem-explorer 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +3 -0
  3. data/Rakefile +40 -0
  4. data/app/assets/javascripts/filesystem_explorer/application.js +15 -0
  5. data/app/assets/stylesheets/filesystem_explorer/application.css +13 -0
  6. data/app/controllers/filesystem_explorer/application_controller.rb +53 -0
  7. data/app/helpers/filesystem_explorer/application_helper.rb +21 -0
  8. data/app/views/filesystem_explorer/application/_directory.html.erb +6 -0
  9. data/app/views/filesystem_explorer/application/_file.html.erb +6 -0
  10. data/app/views/filesystem_explorer/application/index.html.erb +28 -0
  11. data/app/views/filesystem_explorer/application/not_found.html.erb +15 -0
  12. data/config/routes.rb +5 -0
  13. data/lib/filesystem-explorer.rb +8 -0
  14. data/lib/filesystem_explorer/engine.rb +5 -0
  15. data/lib/filesystem_explorer/filesystem_item.rb +168 -0
  16. data/lib/filesystem_explorer/filesystem_route_options.rb +13 -0
  17. data/lib/filesystem_explorer/router.rb +51 -0
  18. data/lib/filesystem_explorer/version.rb +3 -0
  19. data/lib/tasks/filesystem_explorer_tasks.rake +4 -0
  20. data/test/dummy/README.rdoc +261 -0
  21. data/test/dummy/Rakefile +7 -0
  22. data/test/dummy/app/assets/javascripts/application.js +15 -0
  23. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  24. data/test/dummy/app/controllers/application_controller.rb +3 -0
  25. data/test/dummy/app/helpers/application_helper.rb +2 -0
  26. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  27. data/test/dummy/config/application.rb +59 -0
  28. data/test/dummy/config/boot.rb +10 -0
  29. data/test/dummy/config/database.yml +25 -0
  30. data/test/dummy/config/environment.rb +5 -0
  31. data/test/dummy/config/environments/development.rb +37 -0
  32. data/test/dummy/config/environments/production.rb +67 -0
  33. data/test/dummy/config/environments/test.rb +37 -0
  34. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  35. data/test/dummy/config/initializers/inflections.rb +15 -0
  36. data/test/dummy/config/initializers/mime_types.rb +5 -0
  37. data/test/dummy/config/initializers/secret_token.rb +7 -0
  38. data/test/dummy/config/initializers/session_store.rb +8 -0
  39. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  40. data/test/dummy/config/locales/en.yml +5 -0
  41. data/test/dummy/config/routes.rb +4 -0
  42. data/test/dummy/config.ru +4 -0
  43. data/test/dummy/db/development.sqlite3 +0 -0
  44. data/test/dummy/log/development.log +254 -0
  45. data/test/dummy/public/404.html +26 -0
  46. data/test/dummy/public/422.html +26 -0
  47. data/test/dummy/public/500.html +25 -0
  48. data/test/dummy/public/favicon.ico +0 -0
  49. data/test/dummy/script/rails +6 -0
  50. data/test/filesystem_explorer_test.rb +7 -0
  51. data/test/integration/navigation_test.rb +10 -0
  52. data/test/test_helper.rb +15 -0
  53. metadata +163 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Pedro Rodrigues
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = FilesystemExplorer
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'FilesystemExplorer'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,53 @@
1
+ module FilesystemExplorer
2
+ class ApplicationController < ::ApplicationController
3
+ ActionController::Streaming::X_SENDFILE_HEADER = 'X-Accel-Redirect'
4
+
5
+ attr_reader :route
6
+
7
+ helper_method :current_filesystem_explorer_path, :filesystem_explorer_root_path, :filesystem_explorer_root_name
8
+
9
+ before_filter :get_engine_configuration_options
10
+
11
+ def index
12
+ @path = FilesystemExplorer::FilesystemItem.new(route.path, %Q[#{params[:path]}#{".#{params[:format]}" if params[:format]}])
13
+ @path.parent.instance_exec { @is_parent = true } if @path.parent
14
+
15
+ render @path.exists? ? :index : :not_found
16
+ end
17
+
18
+ def download
19
+ @path = FilesystemExplorer::FilesystemItem.new(File.join('/', %Q[#{params[:path]}#{".#{params[:format]}" if params[:format]}]), root: route.path)
20
+ send_file @path.full_path, disposition: :attachment, x_sendfile: true
21
+ end
22
+
23
+ def current_filesystem_explorer_path ; return @path ; end
24
+
25
+ def filesystem_explorer_root_path(as = nil)
26
+ options = as.nil? ? route : get_engine_configuration_options_by_as(as)
27
+
28
+ return @filesystem_explorer_root_path ||= Rails.application.routes.url_helpers.send("#{options.as}_path")
29
+ end
30
+
31
+ def filesystem_explorer_root_name(as = nil)
32
+ options = as.nil? ? route : get_engine_configuration_options_by_as(as)
33
+
34
+ return @filesystem_explorer_root_name ||= options.as.to_s.humanize
35
+ end
36
+
37
+ private
38
+
39
+ def get_engine_configuration_options
40
+ $filesystem_explorer_route_options.each do |path, options|
41
+ @route = options and break if request.path =~ %r(^#{path})
42
+ end
43
+ end
44
+
45
+ def get_engine_configuration_options_by_as(as)
46
+ $filesystem_explorer_route_options.each do |path, options|
47
+ return options if options.as.to_sym == as.to_sym
48
+ end
49
+
50
+ return nil
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,21 @@
1
+ module FilesystemExplorer
2
+ module ApplicationHelper
3
+ def breakdown_path(path)
4
+ paths = [path]
5
+
6
+ while path = path.parent
7
+ paths.unshift path
8
+ end
9
+
10
+ paths
11
+ end
12
+
13
+ def method_missing(method, *args, &block)
14
+ if (method.to_s.end_with?('_path') || method.to_s.end_with?('_url')) && main_app.respond_to?(method)
15
+ main_app.send(method, *args)
16
+ else
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ <tr>
2
+ <td><i class="fa fa-folder"></i> <%= link_to directory.is_parent? ? '..' : directory.name, File.join(filesystem_explorer_root_path, u(directory.path)) %></td>
3
+ <td><%= directory.modified_at.strftime('%F %T') %></td>
4
+ <td></td>
5
+ <td></td>
6
+ </tr>
@@ -0,0 +1,6 @@
1
+ <tr>
2
+ <td><i class="fa fa-file-o"></i> <%= file.name %></td>
3
+ <td><%= file.modified_at.strftime('%F %T') %></td>
4
+ <td class="right"><%= number_to_human_size(file.size) %></td>
5
+ <td class="right"><%= link_to 'D', File.join(filesystem_explorer_root_path, file.path, 'download') %></td>
6
+ </tr>
@@ -0,0 +1,28 @@
1
+ <%
2
+ paths = breakdown_path(@path)
3
+ current = paths.pop
4
+ %>
5
+ <div class="row">
6
+ <div class="col-md-12">
7
+ <ol class="breadcrumb">
8
+ <%paths.each do |directory, index| %>
9
+ <li><%= link_to(directory.is_root? ? filesystem_explorer_root_name : directory.name, File.join(filesystem_explorer_root_path, directory.path)) %></li>
10
+ <% end %>
11
+ <li class="active"><%= current.is_root? ? filesystem_explorer_root_name : current.name %></li>
12
+ </ol>
13
+ </div>
14
+ </div>
15
+ <table class="table">
16
+ <thead>
17
+ <tr>
18
+ <th class="col-md-8">Name</th>
19
+ <th class="col-md-2">Modification date</th>
20
+ <th class="col-md-1">Size</th>
21
+ <th class="col-md-1"></th>
22
+ </tr>
23
+ </thead>
24
+ <tbody>
25
+ <%= render @path.parent if @path.parent %>
26
+ <%= render @path.children %>
27
+ </tbody>
28
+ </table>
@@ -0,0 +1,15 @@
1
+ <%
2
+ paths = breakdown_path(@path)
3
+ current = paths.pop
4
+ %>
5
+ <div class="row">
6
+ <div class="col-md-12">
7
+ <ol class="breadcrumb">
8
+ <% paths.each do |directory, index| %>
9
+ <li><%= link_to(directory.is_root? ? filesystem_explorer_root_name : directory.name, File.join(filesystem_explorer_root_path, directory.path)) %></li>
10
+ <% end %>
11
+ <li class="active"><%= current.is_root? ? filesystem_explorer_root_name : current.name %></li>
12
+ </ol>
13
+ </div>
14
+ </div>
15
+ <%= current.path %> not found!
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ FilesystemExplorer::Engine.routes.draw do
2
+ get '*path/download' => 'application#download', as: :file_download
3
+ get '*path' => 'application#index'
4
+ root to: 'application#index'
5
+ end
@@ -0,0 +1,8 @@
1
+ require "filesystem_explorer/engine"
2
+ require 'filesystem_explorer/filesystem_item'
3
+ require 'filesystem_explorer/filesystem_route_options'
4
+ require 'filesystem_explorer/router'
5
+
6
+ module FilesystemExplorer
7
+ def self.routes ; @routes ||= [] ; end
8
+ end
@@ -0,0 +1,5 @@
1
+ module FilesystemExplorer
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace FilesystemExplorer
4
+ end
5
+ end
@@ -0,0 +1,168 @@
1
+ module FilesystemExplorer
2
+ class FilesystemItem
3
+ attr_reader :root
4
+ attr_reader :full_path
5
+
6
+ def initialize(root, relative_path, options = {})
7
+ options ||= {}
8
+ root = [root].flatten
9
+
10
+ if root.count > 1
11
+ relative_path = nil if File.expand_path(relative_path).gsub(/^#{Dir.pwd}\/?/, '').gsub(/^\d+\/?/, '').strip =~ /^\/?$/
12
+
13
+ if relative_path.blank?
14
+ initialize_for_multi_root({ :roots => root })
15
+ else
16
+ root.each { |r| relative_path = relative_path.gsub(/^#{r}\/?/, '') }
17
+ initialize_for_multi_root_child({
18
+ :roots => root,
19
+ :index => options[:index] || (relative_path =~ /^(\d+)/ && $1.to_i),
20
+ :relative_path => relative_path
21
+ })
22
+ end
23
+ else
24
+ if relative_path.blank?
25
+ initialize_for_single_root({ :root => root.first })
26
+ else
27
+ initialize_for_single_root_child({
28
+ :root => root.first,
29
+ :relative_path => relative_path
30
+ })
31
+ end
32
+ end
33
+ end
34
+
35
+ def initialize_for_multi_root(options)
36
+ @root = options[:roots]
37
+ @full_paths = @root.map { |r| File.expand_path(r) }
38
+ @is_directory = @full_paths.all? { |p| File.directory?(p) }
39
+ @modified_at = nil
40
+ @size = nil
41
+ @is_root = true
42
+ @exists = @full_paths.all? { |p| File.exists?(p) }
43
+ @is_multi_root = true
44
+ end
45
+
46
+ def initialize_for_multi_root_child(options)
47
+ @root = options[:roots]
48
+ @index = options[:index]
49
+ @path = options[:relative_path]
50
+ @full_path = File.expand_path(File.join(@root[@index], options[:relative_path].gsub(/^#{options[:index]}\/?/, '')))
51
+ @exists = File.exists?(@full_path)
52
+ @name = File.basename(@full_path)
53
+ @is_directory = @exists && File.directory?(@full_path)
54
+ @modified_at = @exists && File.mtime(@full_path)
55
+ @size = @exists && File.size(@full_path)
56
+ @is_multi_root = true
57
+ @is_root = false
58
+ @parent = FilesystemItem.new(@root, build_path(@path, '..'), { :index => options[:index] })
59
+ end
60
+
61
+ def initialize_for_single_root(options)
62
+ @root = options[:root]
63
+ @full_path = File.expand_path(@root)
64
+ @path = '/'
65
+ @is_directory = File.directory?(@full_path)
66
+ @modified_at = File.mtime(@full_path)
67
+ @size = File.size(@full_path)
68
+ @is_root = true
69
+ @exists = File.exists?(@full_path)
70
+ @is_multi_root = false
71
+ end
72
+
73
+ def initialize_for_single_root_child(options)
74
+ @root = options[:root]
75
+ @path = options[:relative_path]
76
+ @full_path = File.expand_path(File.join(@root, @path))
77
+ @name = File.basename(@full_path)
78
+ @is_directory = File.directory?(@full_path)
79
+ @modified_at = File.mtime(@full_path)
80
+ @size = File.size(@full_path)
81
+ @is_root = false
82
+ @exists = File.exists?(@full_path)
83
+ @is_multi_root = false
84
+ @parent = FilesystemItem.new(@root, build_path(@path, '..'))
85
+ end
86
+
87
+ def is_directory? ; return @is_directory ; end
88
+
89
+ def path ; return @path ||= @full_path || (@full_paths && "") ; end
90
+ def url ; return @path.split('/').map { |p| URI::escape(p) }.join('/') ; end
91
+
92
+ def is_root? ; return @is_root ; end
93
+ def is_parent? ; return @is_parent || false ; end
94
+ def exists? ; return @exists ; end
95
+ def name ; return @name ; end
96
+ def modified_at(format = nil)
97
+ return @modified_at if format.blank? || @modified_at.blank?
98
+ return @modified_at.strftime(format)
99
+ end
100
+ def size ; return @size ; end
101
+
102
+ def parent
103
+ return nil if is_root?
104
+ @parent ||= FilesystemItem.new(@root, File.join(@full_path, '..'), { root: @root })
105
+ end
106
+
107
+ def children
108
+ return nil unless is_directory?
109
+ if @children.blank?
110
+ if @is_multi_root
111
+ if @is_root
112
+ load_multi_root_children
113
+ else
114
+ load_multi_root_child_children
115
+ end
116
+ else
117
+ load_single_root_children
118
+ end
119
+
120
+ sort_children
121
+ end
122
+
123
+ @children
124
+ end
125
+
126
+ def inspect ; return "[#{is_directory? ? 'D' : 'F'}] #{@full_path}" ; end
127
+
128
+ def to_partial_path ; is_directory? ? 'directory' : 'file' ; end
129
+
130
+ private
131
+
132
+ def load_multi_root_children
133
+ @children = @is_directory ? @full_paths.each_with_index.map do |r, index|
134
+ Dir[File.join(Shellwords.escape(r), '*')].map do |f|
135
+ FilesystemItem.new(@full_paths, f.gsub(/^#{r}/, "#{index}"), :index => index)
136
+ end
137
+ end.flatten : []
138
+ end
139
+
140
+ def load_multi_root_child_children
141
+ @children = @is_directory ? Dir[File.join(Shellwords.escape(@full_path), '*')].map do |f|
142
+ FilesystemItem.new(@root, f.gsub(/^#{@root[@index]}/, "#{@index}"), :index => @index)
143
+ end : []
144
+ end
145
+
146
+ def load_single_root_children
147
+ @children = @is_directory ? Dir[File.join(Shellwords.escape(@full_path), '*')].map do |f|
148
+ FilesystemItem.new(@root, f.gsub(/^#{@root}\/?/, ''))
149
+ end : []
150
+ end
151
+
152
+ def sort_children
153
+ @children.sort! do |a, b|
154
+ if a.is_directory? && !b.is_directory?
155
+ -1
156
+ elsif !a.is_directory? && b.is_directory?
157
+ 1
158
+ else
159
+ a.name.downcase <=> b.name.downcase
160
+ end
161
+ end
162
+ end
163
+
164
+ def build_path(*args)
165
+ File.expand_path(File.join(*args)).gsub(/^#{Dir.pwd}\/?/, '')
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,13 @@
1
+ module FilesystemExplorer
2
+ class FilesystemRouteOptions
3
+ %w(as url path).each do |method|
4
+ attr_reader :"#{method}"
5
+ instance_variable_set :"@#{method}", nil
6
+
7
+ define_method(method) do |value = nil|
8
+ instance_variable_set :"@#{method}", value if value
9
+ instance_variable_get :"@#{method}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ module ActionDispatch
2
+ module Routing
3
+ class Mapper
4
+ def filesystem_routes(&block)
5
+ config_file_path = Rails.root.join('config', 'filesystem_explorer.yml')
6
+
7
+ if File.exists?(config_file_path)
8
+ config_data = YAML.load_file(config_file_path)
9
+
10
+ config_data.each do |route|
11
+ filesystem_explorer path: route[:path] || route['path'], as: route[:as] || route['as'], url: route[:url] || route['url']
12
+ end
13
+ end
14
+
15
+ block.call($filesystem_explorer_route_options || {}) if block
16
+ end
17
+
18
+ def filesystem_explorer(options = {}, &block)
19
+ route_config = FilesystemExplorer::FilesystemRouteOptions.new
20
+
21
+ # Copy all applicable options from the parameter hash to the object
22
+ %w(path as url).each do |option|
23
+ route_config.send option, options[option.to_sym] if options[option.to_sym] && route_config.respond_to?(option)
24
+ end
25
+
26
+ route_config.instance_eval &block if block
27
+
28
+ $filesystem_explorer_route_options ||= {}
29
+ $filesystem_explorer_route_options[route_config.url] ||= route_config
30
+
31
+ FilesystemExplorer.routes << route_config
32
+
33
+ Rails.application.routes.draw do
34
+ # Download route
35
+ route_options = { "#{route_config.url}/*path/download" => "filesystem_explorer/application#download" }
36
+ route_options[:as] = :"#{route_config.as}_download" if route_config.as
37
+ get route_options
38
+
39
+ # Dynamic route
40
+ route_options = { "#{route_config.url}/*path" => "filesystem_explorer/application#index" }
41
+ get route_options
42
+
43
+ # Root route
44
+ route_options = { "#{route_config.url}" => "filesystem_explorer/application#index" }
45
+ route_options[:as] = :"#{route_config.as}" if route_config.as
46
+ get route_options
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module FilesystemExplorer
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :filesystem_explorer do
3
+ # # Task goes here
4
+ # end