ferver 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +6 -1
- data/README.md +18 -10
- data/Rakefile +19 -17
- data/bin/ferver +19 -4
- data/ferver.gemspec +19 -19
- data/lib/ferver.rb +24 -2
- data/lib/ferver/app.rb +38 -97
- data/lib/ferver/configuration.rb +11 -0
- data/lib/ferver/directory_not_found_error.rb +4 -0
- data/lib/ferver/file_id_request.rb +28 -19
- data/lib/ferver/file_list.rb +48 -46
- data/lib/ferver/version.rb +1 -1
- data/lib/ferver/views/index.erb +28 -0
- data/spec/configuration_spec.rb +24 -0
- data/spec/ferver_spec.rb +132 -180
- data/spec/file_id_request_spec.rb +46 -67
- data/spec/file_list_spec.rb +107 -128
- data/spec/spec_helper.rb +10 -10
- metadata +49 -44
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d2f0cc442e5c638817dbd7465cac6033cbafa38
|
4
|
+
data.tar.gz: 3344fc4b12b37de5ec6e08da1694c093172a6bbe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98f244dec47ab5c2dd7cd9d1ac687115763a63872622d03dd602d002125d38169210af42d66ad1eb3b8bd53d0edb72e068cf2cd7a7363debd62be2f57ce7f1b1
|
7
|
+
data.tar.gz: a111d09816eefba4e1da78e7e5af7d7172d6526d5cb14988dca5b4d6d8d4f27c54a4005963b937ec4d10bead859cc9c536e2f20015342fc55f10b3fa7cf0c11a
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.1.1
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
#### Ferver: A simple web app to serve files over HTTP packaged as a Ruby gem.
|
4
4
|
|
5
|
-
[![Build Status](https://travis-ci.org/rob-murray/ferver.
|
6
|
-
[![Code Climate](https://codeclimate.com/github/rob-murray/ferver.png)](https://codeclimate.com/github/rob-murray/ferver)
|
7
|
-
[![Coverage Status](https://coveralls.io/repos/rob-murray/ferver/badge.png)](https://coveralls.io/r/rob-murray/ferver)
|
8
|
-
[![Dependency Status](https://gemnasium.com/rob-murray/ferver.
|
9
|
-
[![Gem Version](https://badge.fury.io/rb/ferver.
|
5
|
+
[![Build Status](https://travis-ci.org/rob-murray/ferver.svg?branch=master)](https://travis-ci.org/rob-murray/ferver)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/rob-murray/ferver.png)](https://codeclimate.com/github/rob-murray/ferver)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/rob-murray/ferver/badge.png)](https://coveralls.io/r/rob-murray/ferver)
|
8
|
+
[![Dependency Status](https://gemnasium.com/rob-murray/ferver.svg)](https://gemnasium.com/rob-murray/ferver)
|
9
|
+
[![Gem Version](https://badge.fury.io/rb/ferver.svg)](http://badge.fury.io/rb/ferver)
|
10
10
|
|
11
11
|
### Description
|
12
12
|
|
@@ -31,7 +31,7 @@ $ gem install ferver
|
|
31
31
|
|
32
32
|
### Usage
|
33
33
|
|
34
|
-
You can run **ferver** from any directory, just pass in the directory you want to serve files from or
|
34
|
+
You can run **ferver** from any directory, just pass in the directory you want to serve files from as a command line argument or leave blank to use the current directory.
|
35
35
|
|
36
36
|
##### Use the current directory
|
37
37
|
|
@@ -44,7 +44,15 @@ $ ferver
|
|
44
44
|
For exmple, to serve files from **/Users/rob/Projects/ferver/** directory pass the path in as below;
|
45
45
|
|
46
46
|
```bash
|
47
|
-
$ ferver /Users/rob/Projects/ferver/
|
47
|
+
$ ferver -d /Users/rob/Projects/ferver/
|
48
|
+
````
|
49
|
+
|
50
|
+
##### Command line help
|
51
|
+
|
52
|
+
For a list of arguments just use the `-h` switch.
|
53
|
+
|
54
|
+
```bash
|
55
|
+
$ ferver -h
|
48
56
|
````
|
49
57
|
|
50
58
|
### Accessing files
|
@@ -75,14 +83,14 @@ For example to download file appearing third in the list displayed earlier, requ
|
|
75
83
|
|
76
84
|
Please use the GitHub pull-request mechanism to submit contributions.
|
77
85
|
|
78
|
-
After cloning the repo, you can
|
86
|
+
After cloning the repo, you can run the web application without having to install the gem package by running a Rake task named `server`, or it's alias `s`.
|
79
87
|
|
80
88
|
```bash
|
81
|
-
$ rake
|
89
|
+
$ rake s
|
82
90
|
|
83
91
|
# or
|
84
92
|
|
85
|
-
$ rake
|
93
|
+
$ rake s /path/to/dir
|
86
94
|
```
|
87
95
|
|
88
96
|
### License
|
data/Rakefile
CHANGED
@@ -1,25 +1,27 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
2
|
require 'rspec/core'
|
3
3
|
require 'rspec/core/rake_task'
|
4
|
-
|
4
|
+
|
5
5
|
task :default => :spec
|
6
|
-
|
6
|
+
task :s => :server
|
7
|
+
|
7
8
|
desc "Run all specs in spec directory (excluding plugin specs)"
|
8
9
|
RSpec::Core::RakeTask.new(:spec)
|
9
10
|
|
10
|
-
desc
|
11
|
-
task :server do
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
require 'ferver'
|
17
|
-
|
18
|
-
# use the last argument (first is the rake task) as the file path to serve from
|
19
|
-
ferver_path = (ARGV.length == 2 && ARGV.last) || nil
|
20
|
-
Ferver::App.set :ferver_path, ferver_path
|
11
|
+
desc 'Run ferver locally from source'
|
12
|
+
task :server do
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler'
|
15
|
+
Bundler.setup
|
16
|
+
require 'ferver'
|
21
17
|
|
22
|
-
|
23
|
-
|
18
|
+
# use the last argument (first is the rake task) as the file path to serve from
|
19
|
+
ferver_path = (ARGV.length == 2 && ARGV.last) || nil
|
20
|
+
Ferver.configure do |config|
|
21
|
+
config.directory_path = ferver_path
|
22
|
+
end
|
23
|
+
Ferver::App.set :raise_errors, false
|
24
24
|
|
25
|
-
|
25
|
+
# run!
|
26
|
+
Ferver::App.run!
|
27
|
+
end
|
data/bin/ferver
CHANGED
@@ -1,13 +1,28 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
require 'optparse'
|
3
4
|
require 'sinatra'
|
4
5
|
require 'ferver'
|
5
6
|
|
7
|
+
options = {}
|
8
|
+
OptionParser.new do |opts|
|
9
|
+
opts.banner = 'Ferver: A simple web app to serve files over HTTP. Version: ' + Ferver::VERSION
|
10
|
+
opts.separator ''
|
6
11
|
|
7
|
-
|
8
|
-
|
9
|
-
|
12
|
+
opts.on('-d', '--directory [DIRECTORY]', 'Specify the path to the directory to serve files from [optional]') do |directory|
|
13
|
+
options[:directory] = directory
|
14
|
+
end
|
15
|
+
|
16
|
+
opts.on("-h", "--help", "Displays help") do
|
17
|
+
puts opts
|
18
|
+
exit
|
19
|
+
end
|
20
|
+
end.parse!
|
21
|
+
|
22
|
+
Ferver.configure do |config|
|
23
|
+
config.directory_path = options[:directory]
|
24
|
+
end
|
10
25
|
Ferver::App.set :environment, :production
|
11
26
|
|
12
27
|
# run!
|
13
|
-
Ferver::App.run!
|
28
|
+
Ferver::App.run!
|
data/ferver.gemspec
CHANGED
@@ -4,29 +4,29 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'ferver/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'ferver'
|
8
8
|
spec.version = Ferver::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary = %q
|
12
|
-
spec.description = %q
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
15
|
-
|
9
|
+
spec.authors = ['Rob Murray']
|
10
|
+
spec.email = ['robmurray17@gmail.com']
|
11
|
+
spec.summary = %q(A simple web app to serve files over HTTP.)
|
12
|
+
spec.description = %q(Ferver is a super, simple ruby gem to serve files over http; useful as a basic file server to quickly share files on your local network.)
|
13
|
+
spec.homepage = 'https://github.com/rob-murray/ferver'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
-
spec.require_paths = [
|
18
|
+
spec.require_paths = ['lib']
|
19
19
|
|
20
20
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
-
|
22
|
-
spec.add_development_dependency "bundler", "~> 1.5"
|
23
|
-
spec.add_development_dependency "rake", "~> 0"
|
24
|
-
spec.add_development_dependency "rspec", "~> 2.14.1"
|
25
|
-
spec.add_development_dependency "rack-test", "~> 0.6.2"
|
26
|
-
spec.add_development_dependency "mocha", "~> 1.0.0"
|
27
|
-
spec.add_development_dependency "spork", "~> 0.9.2"
|
28
|
-
spec.add_development_dependency "webrat", "~> 0.7.3"
|
29
|
-
spec.add_development_dependency "coveralls", "~> 0.7.0"
|
30
21
|
|
31
|
-
spec.
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
23
|
+
spec.add_development_dependency 'rake', '~> 0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
spec.add_development_dependency 'rack-test', '~> 0.6'
|
26
|
+
spec.add_development_dependency 'spork', '~> 0.9'
|
27
|
+
spec.add_development_dependency 'rspec-html-matchers', '~> 0.6'
|
28
|
+
spec.add_development_dependency 'coveralls', '~> 0.7'
|
29
|
+
spec.add_development_dependency 'codeclimate-test-reporter'
|
30
|
+
|
31
|
+
spec.add_dependency 'sinatra', '~> 1.4'
|
32
32
|
end
|
data/lib/ferver.rb
CHANGED
@@ -1,7 +1,29 @@
|
|
1
|
-
|
1
|
+
require_relative 'ferver/app'
|
2
|
+
require_relative 'ferver/configuration'
|
3
|
+
require_relative 'ferver/directory_not_found_error'
|
4
|
+
require_relative 'ferver/file_id_request'
|
5
|
+
require_relative 'ferver/file_list'
|
6
|
+
require_relative 'ferver/version'
|
2
7
|
|
3
8
|
module Ferver
|
4
9
|
autoload :App, 'ferver/app'
|
5
10
|
|
6
|
-
|
11
|
+
# By default, serve files from current location when the gem is called.
|
12
|
+
DEFAULT_FILE_SERVER_DIR_PATH = './'
|
13
|
+
|
14
|
+
class << self
|
15
|
+
attr_accessor :configuration
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configuration
|
19
|
+
@configuration ||= Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.reset
|
23
|
+
@configuration = Configuration.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.configure
|
27
|
+
yield(configuration)
|
28
|
+
end
|
7
29
|
end
|
data/lib/ferver/app.rb
CHANGED
@@ -1,126 +1,67 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
|
5
|
-
|
1
|
+
require 'sinatra'
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'json'
|
4
|
+
require_relative './directory_not_found_error'
|
5
|
+
require_relative './configuration'
|
6
6
|
|
7
7
|
module Ferver
|
8
8
|
class App < Sinatra::Base
|
9
|
-
|
10
|
-
# Config
|
11
|
-
set :inline_templates, true
|
12
9
|
set :app_file, __FILE__
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
before do
|
12
|
+
@ferver_list = FileList.new(current_ferver_path)
|
13
|
+
end
|
17
14
|
|
18
|
-
|
19
|
-
|
20
|
-
get "/" do
|
21
|
-
redirect to("/files")
|
15
|
+
error Ferver::DirectoryNotFoundError do
|
16
|
+
halt 500, "Ferver: Directory '#{current_ferver_path}' not found."
|
22
17
|
end
|
23
18
|
|
24
|
-
#
|
25
|
-
|
26
|
-
|
27
|
-
|
19
|
+
# redirect to file list
|
20
|
+
get '/' do
|
21
|
+
redirect to('/files')
|
22
|
+
end
|
28
23
|
|
29
|
-
|
24
|
+
# list files
|
25
|
+
get '/files' do
|
26
|
+
if request.preferred_type.to_s == 'application/json'
|
30
27
|
content_type :json
|
31
28
|
|
32
|
-
|
29
|
+
ferver_list.all.to_json
|
33
30
|
else
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
erb :file_list_view
|
31
|
+
erb :index, locals: { file_list: ferver_list.all,
|
32
|
+
ferver_path: File.expand_path(current_ferver_path),
|
33
|
+
file_count: ferver_list.size }
|
39
34
|
end
|
40
|
-
|
41
35
|
end
|
42
36
|
|
43
37
|
# download file
|
44
|
-
|
45
|
-
|
46
|
-
id_request = Ferver::FileIdRequest.new(params[:id])
|
38
|
+
get '/files/:id' do
|
39
|
+
halt(400, 'Bad request') unless valid_file_request?
|
47
40
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
file_name = @ferver_list.file_by_id(id_request.value)
|
52
|
-
file = FileList.path_for_file(get_current_ferver_path, file_name)
|
41
|
+
if ferver_list.file_id_is_valid?(file_id_request.value)
|
42
|
+
file_name = ferver_list.file_by_id(file_id_request.value)
|
43
|
+
file = FileList.path_for_file(current_ferver_path, file_name)
|
53
44
|
|
54
|
-
send_file(file, :
|
45
|
+
send_file(file, disposition: 'attachment', filename: File.basename(file))
|
55
46
|
else
|
56
47
|
status 404
|
57
48
|
end
|
58
49
|
end
|
59
50
|
|
60
|
-
|
61
|
-
# Find all files in `Ferver` directory.
|
62
|
-
# !Called before each request
|
63
|
-
#
|
64
|
-
before do
|
65
|
-
begin
|
66
|
-
@ferver_list = FileList.new(get_current_ferver_path)
|
67
|
-
rescue DirectoryNotFoundError
|
68
|
-
halt(500, "Ferver: Directory '#{get_current_ferver_path}' not found.")
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
51
|
private
|
74
52
|
|
75
|
-
|
76
|
-
# This can be specified in Sinatra configuration;
|
77
|
-
# i.e. `Ferver::App.set :ferver_path, ferver_path` or the default if nil
|
78
|
-
#
|
79
|
-
def get_current_ferver_path
|
80
|
-
@current_ferver_path ||= begin
|
81
|
-
path = nil
|
53
|
+
attr_reader :ferver_list
|
82
54
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
path = DEFAULT_FILE_SERVER_DIR_PATH
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
55
|
+
def file_id_request
|
56
|
+
@file_id_request ||= FileIdRequest.new(params[:id])
|
57
|
+
end
|
90
58
|
|
59
|
+
def valid_file_request?
|
60
|
+
file_id_request.valid?
|
61
|
+
end
|
62
|
+
|
63
|
+
def current_ferver_path
|
64
|
+
Ferver.configuration.directory_path
|
65
|
+
end
|
91
66
|
end
|
92
67
|
end
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
__END__
|
97
|
-
|
98
|
-
@@file_list_view
|
99
|
-
<!DOCTYPE html>
|
100
|
-
<html lang="en">
|
101
|
-
<head>
|
102
|
-
<meta charset="utf-8">
|
103
|
-
<title>File List · Ferver</title>
|
104
|
-
</head>
|
105
|
-
<body>
|
106
|
-
<h3>Files served:</h3>
|
107
|
-
<ul>
|
108
|
-
<% @file_list.each_with_index do |file_name, index| %>
|
109
|
-
|
110
|
-
<li><a href="/files/<%= index %>"><%= file_name %></a></li>
|
111
|
-
|
112
|
-
<% end %>
|
113
|
-
|
114
|
-
</ul>
|
115
|
-
|
116
|
-
<p><%= @file_count %> files served from: <%= @ferver_path %></p>
|
117
|
-
|
118
|
-
<hr>
|
119
|
-
|
120
|
-
<p>Served by <a href="https://github.com/rob-murray/ferver" title="Ferver: A simple Ruby app serving files over HTTP">Ferver</a> gem v<%= Ferver::VERSION %></p>
|
121
|
-
|
122
|
-
</body>
|
123
|
-
</html>
|
124
|
-
|
125
|
-
<html>
|
126
|
-
<body>
|
@@ -1,28 +1,37 @@
|
|
1
1
|
|
2
2
|
module Ferver
|
3
|
-
|
3
|
+
class FileIdRequest
|
4
|
+
attr_reader :value
|
4
5
|
|
5
|
-
|
6
|
+
def initialize(value = nil)
|
7
|
+
@is_valid = false
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
+
self.value = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def value=(value)
|
13
|
+
id = parse_value(value)
|
9
14
|
|
10
|
-
|
11
|
-
|
15
|
+
if id.nil?
|
16
|
+
@is_valid = false
|
17
|
+
else
|
18
|
+
@value = id
|
19
|
+
@is_valid = true
|
20
|
+
end
|
21
|
+
end
|
12
22
|
|
13
|
-
|
14
|
-
|
23
|
+
def valid?
|
24
|
+
@is_valid
|
25
|
+
end
|
15
26
|
|
16
|
-
|
17
|
-
@is_valid = false
|
18
|
-
else
|
19
|
-
@value = id
|
20
|
-
@is_valid = true
|
21
|
-
end
|
22
|
-
end
|
27
|
+
private
|
23
28
|
|
24
|
-
|
25
|
-
|
26
|
-
|
29
|
+
def parse_value(value)
|
30
|
+
begin
|
31
|
+
Integer(value)
|
32
|
+
rescue
|
33
|
+
nil
|
34
|
+
end
|
27
35
|
end
|
28
|
-
end
|
36
|
+
end
|
37
|
+
end
|