ferver 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rspec +2 -1
- data/.rubocop.yml +13 -0
- data/.ruby-version +1 -1
- data/.travis.yml +6 -1
- data/Gemfile +2 -1
- data/README.md +11 -1
- data/Rakefile +1 -0
- data/bin/ferver +20 -9
- data/ferver.gemspec +22 -20
- data/lib/ferver.rb +9 -11
- data/lib/ferver/app.rb +5 -4
- data/lib/ferver/configuration.rb +18 -9
- data/lib/ferver/controller.rb +25 -30
- data/lib/ferver/errors.rb +5 -0
- data/lib/ferver/ferver_directory.rb +43 -0
- data/lib/ferver/file_list.rb +10 -34
- data/lib/ferver/found_file.rb +2 -1
- data/lib/ferver/version.rb +2 -1
- data/spec/configuration_spec.rb +16 -10
- data/spec/ferver_directory_spec.rb +78 -0
- data/spec/ferver_spec.rb +62 -97
- data/spec/file_list_spec.rb +40 -112
- data/spec/found_file_spec.rb +54 -0
- data/spec/spec_helper.rb +44 -29
- metadata +41 -24
- data/lib/ferver/directory_not_found_error.rb +0 -3
- data/lib/ferver/file_id_request.rb +0 -32
- data/spec/file_id_request_spec.rb +0 -65
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: afd7d5747292d7103cf9d23366c7c3873f3f6191
|
4
|
+
data.tar.gz: c3c46b4c00c3d4de4cfdda39e2872ea2dea74edd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02dd8ddfa1e9816e330dabdad1da70301a743589c8154a709122174ec5ec941c0acb9ec9bc34021b19a359253a1e496661894e2663de149e74171476fc70cb37
|
7
|
+
data.tar.gz: 601fa4f7a2e4c2b74cae5114d1c98662136e1dc4e2406cbe135648d693832991c690ce980661b77dfaedc1ffa9fe9690ef4c8611ead0748af55163737bb24d9e
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/.rubocop.yml
ADDED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.4.0
|
data/.travis.yml
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
language: ruby
|
2
2
|
sudo: false
|
3
3
|
rvm:
|
4
|
-
- 1.9.3
|
5
4
|
- 2.0.0
|
6
5
|
- 2.1.0
|
7
6
|
- 2.2.0
|
7
|
+
- 2.3.0
|
8
|
+
- 2.4.0
|
8
9
|
- ruby-head
|
9
10
|
|
11
|
+
env:
|
12
|
+
global:
|
13
|
+
- NOKOGIRI_USE_SYSTEM_LIBRARIES=true
|
14
|
+
|
10
15
|
addons:
|
11
16
|
code_climate:
|
12
17
|
repo_token: aaa2bcd71ac641474a13caabe294beeb26abcc7c4dd82f3395a7b9193bb20d02
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
[![Build Status](https://travis-ci.org/rob-murray/ferver.svg?branch=master)](https://travis-ci.org/rob-murray/ferver)
|
6
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.
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/rob-murray/ferver/badge.svg?branch=master&service=github)](https://coveralls.io/github/rob-murray/ferver?branch=master)
|
8
8
|
[![Dependency Status](https://gemnasium.com/rob-murray/ferver.svg)](https://gemnasium.com/rob-murray/ferver)
|
9
9
|
[![Gem Version](https://badge.fury.io/rb/ferver.svg)](http://badge.fury.io/rb/ferver)
|
10
10
|
|
@@ -58,6 +58,14 @@ $ ferver -a
|
|
58
58
|
|
59
59
|
> Note that zero size files will always be hidden.
|
60
60
|
|
61
|
+
##### Configure webserver
|
62
|
+
|
63
|
+
If required, you can configure the bind address or port number used by the webserver. By default this is `0.0.0.0` and port `4567` which means the server is accessible from outside your machine (if firewall permits). For example if you used the configuration below then it would only be accessible from local machine and on port `9999`.
|
64
|
+
|
65
|
+
```bash
|
66
|
+
$ ferver -p 9999 -b 127.0.0.1
|
67
|
+
````
|
68
|
+
|
61
69
|
##### Command line help
|
62
70
|
|
63
71
|
For a list of arguments just use the `--help` switch.
|
@@ -70,6 +78,8 @@ $ ferver -h
|
|
70
78
|
|
71
79
|
The **ferver** gem uses [Sinatra](http://www.sinatrarb.com/) and runs on default port configuration so just point your browser to `http://localhost:4567` to list the files.
|
72
80
|
|
81
|
+
> If you are unable to connect then please check any firewall settings or the configured bind address or port number!
|
82
|
+
|
73
83
|
#### HTML
|
74
84
|
|
75
85
|
List available files in your browser.
|
data/Rakefile
CHANGED
data/bin/ferver
CHANGED
@@ -1,22 +1,31 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
|
5
|
-
require
|
4
|
+
require "optparse"
|
5
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), "/../lib")
|
6
|
+
require "ferver"
|
6
7
|
|
7
8
|
options = {}
|
8
9
|
OptionParser.new do |opts|
|
9
|
-
opts.banner =
|
10
|
-
opts.separator
|
10
|
+
opts.banner = "Ferver: A simple web app to serve files over HTTP. Version: " + Ferver::VERSION
|
11
|
+
opts.separator ""
|
11
12
|
|
12
|
-
opts.on(
|
13
|
+
opts.on("-a", "--all", "Serve hidden files. False by default [optional]") do |a|
|
13
14
|
options[:hidden] = a
|
14
15
|
end
|
15
16
|
|
16
|
-
opts.on(
|
17
|
+
opts.on("-d", "--directory [DIRECTORY]", "Specify the path to the directory to serve files from [optional]") do |directory|
|
17
18
|
options[:directory] = directory
|
18
19
|
end
|
19
20
|
|
21
|
+
opts.on("-b", "--bind [IP or hostname]", "Set the hostname or IP address of the interface to listen on when running. Defaults to '0.0.0.0' [optional]") do |bind_address|
|
22
|
+
options[:bind_address] = bind_address
|
23
|
+
end
|
24
|
+
|
25
|
+
opts.on("-p", "--port [PORT]", "Set the port used by Ferver server. Defaults to 4567 [optional]") do |port|
|
26
|
+
options[:port] = port
|
27
|
+
end
|
28
|
+
|
20
29
|
opts.on("-h", "--help", "Displays help") do
|
21
30
|
puts opts
|
22
31
|
exit
|
@@ -24,10 +33,12 @@ OptionParser.new do |opts|
|
|
24
33
|
end.parse!
|
25
34
|
|
26
35
|
Ferver.configure do |config|
|
27
|
-
config.serve_hidden = options[:hidden]
|
28
|
-
config.directory_path = options[:directory]
|
36
|
+
config.serve_hidden = options[:hidden] if options[:hidden]
|
37
|
+
config.directory_path = options[:directory] if options[:directory]
|
29
38
|
end
|
30
39
|
Ferver::App.set :environment, :production
|
40
|
+
Ferver::App.set :bind, options[:bind_address] if options[:bind_address]
|
41
|
+
Ferver::App.set :port, options[:port] if options[:port]
|
31
42
|
|
32
43
|
# run!
|
33
44
|
Ferver::App.run!
|
data/ferver.gemspec
CHANGED
@@ -1,33 +1,35 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
5
|
+
require "ferver/version"
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
8
|
+
spec.name = "ferver"
|
8
9
|
spec.version = Ferver::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
15
|
-
spec.required_ruby_version =
|
10
|
+
spec.authors = ["Rob Murray"]
|
11
|
+
spec.email = ["robmurray17@gmail.com"]
|
12
|
+
spec.summary = "A simple web app to serve files over HTTP."
|
13
|
+
spec.description = "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."
|
14
|
+
spec.homepage = "https://github.com/rob-murray/ferver"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.required_ruby_version = ">= 2.0.0"
|
16
17
|
|
17
18
|
spec.files = `git ls-files -z`.split("\x0")
|
18
19
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
-
spec.require_paths = [
|
20
|
+
spec.require_paths = ["lib"]
|
20
21
|
|
21
22
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
23
|
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
26
|
-
spec.add_development_dependency
|
27
|
-
spec.add_development_dependency
|
28
|
-
spec.add_development_dependency
|
29
|
-
spec.add_development_dependency
|
30
|
-
spec.add_development_dependency
|
24
|
+
spec.add_development_dependency "bundler"
|
25
|
+
spec.add_development_dependency "nokogiri", "~> 1.6.0"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
spec.add_development_dependency "rack-test", "~> 0.6"
|
29
|
+
spec.add_development_dependency "rspec-html-matchers", "~> 0.6"
|
30
|
+
spec.add_development_dependency "coveralls", "~> 0.7"
|
31
|
+
spec.add_development_dependency "codeclimate-test-reporter"
|
32
|
+
spec.add_development_dependency "byebug"
|
31
33
|
|
32
|
-
spec.add_dependency
|
34
|
+
spec.add_dependency "sinatra", "~> 1.4"
|
33
35
|
end
|
data/lib/ferver.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
|
-
|
2
|
-
require_relative
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative "ferver/configuration"
|
3
|
+
require_relative "ferver/errors"
|
4
|
+
require_relative "ferver/app"
|
5
|
+
require_relative "ferver/controller"
|
6
|
+
require_relative "ferver/ferver_directory"
|
7
|
+
require_relative "ferver/file_list"
|
8
|
+
require_relative "ferver/found_file"
|
9
|
+
require_relative "ferver/version"
|
9
10
|
|
10
11
|
module Ferver
|
11
|
-
# By default, serve files from current location when the gem is called.
|
12
|
-
DEFAULT_FILE_SERVER_DIR_PATH = './'
|
13
|
-
|
14
12
|
class << self
|
15
13
|
attr_accessor :configuration
|
16
14
|
|
data/lib/ferver/app.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
|
-
|
2
|
-
require
|
3
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "sinatra"
|
3
|
+
require "sinatra/base"
|
4
|
+
require_relative "./controller"
|
4
5
|
|
5
6
|
module Ferver
|
6
7
|
class App < Sinatra::Application
|
7
8
|
set :app_file, __FILE__
|
8
9
|
|
9
10
|
configure do
|
10
|
-
set :views,
|
11
|
+
set :views, "./views"
|
11
12
|
end
|
12
13
|
|
13
14
|
use Controller
|
data/lib/ferver/configuration.rb
CHANGED
@@ -1,18 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Ferver
|
2
3
|
class Configuration
|
3
|
-
|
4
|
+
# By default, serve files from current location when the gem is called.
|
5
|
+
DEFAULT_FILE_SERVER_DIR_PATH = "./".freeze
|
4
6
|
|
5
7
|
# Return the absolute path to the directory Ferver is serving files from.
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
attr_accessor :directory_path
|
9
|
+
|
10
|
+
# Default to not serving hidden files.
|
11
|
+
attr_accessor :serve_hidden
|
12
|
+
|
13
|
+
# For testing
|
14
|
+
attr_writer :directory
|
15
|
+
|
16
|
+
alias serve_hidden? serve_hidden
|
17
|
+
|
18
|
+
def initialize(directory_path = DEFAULT_FILE_SERVER_DIR_PATH, serve_hidden = false)
|
19
|
+
@directory_path = directory_path
|
20
|
+
@serve_hidden = serve_hidden
|
9
21
|
end
|
10
22
|
|
11
|
-
|
12
|
-
|
13
|
-
def serve_hidden
|
14
|
-
@serve_hidden || false
|
23
|
+
def directory
|
24
|
+
@directory ||= FerverDirectory.new(self)
|
15
25
|
end
|
16
|
-
alias_method :serve_hidden?, :serve_hidden
|
17
26
|
end
|
18
27
|
end
|
data/lib/ferver/controller.rb
CHANGED
@@ -1,19 +1,20 @@
|
|
1
|
-
|
2
|
-
require
|
3
|
-
require
|
4
|
-
|
5
|
-
require_relative './configuration'
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "sinatra"
|
3
|
+
require "sinatra/base"
|
4
|
+
require "json"
|
6
5
|
|
7
6
|
module Ferver
|
8
7
|
class Controller < Sinatra::Base
|
9
8
|
before do
|
10
|
-
@ferver_list = FileList.new(
|
9
|
+
@ferver_list = FileList.new(Ferver.configuration.directory.found_files)
|
11
10
|
end
|
12
11
|
|
13
|
-
before
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
before "/files/:id" do
|
13
|
+
begin
|
14
|
+
@file = find_file! Integer(params[:id])
|
15
|
+
rescue ArgumentError
|
16
|
+
halt 400, "Bad request"
|
17
|
+
end
|
17
18
|
end
|
18
19
|
|
19
20
|
error Ferver::DirectoryNotFoundError do
|
@@ -21,13 +22,13 @@ module Ferver
|
|
21
22
|
end
|
22
23
|
|
23
24
|
# redirect to file list
|
24
|
-
get
|
25
|
-
redirect to(
|
25
|
+
get "/" do
|
26
|
+
redirect to("/files")
|
26
27
|
end
|
27
28
|
|
28
29
|
# list files
|
29
|
-
get
|
30
|
-
if
|
30
|
+
get "/files" do
|
31
|
+
if json_request?
|
31
32
|
content_type :json
|
32
33
|
|
33
34
|
ferver_list.map(&:name).to_json
|
@@ -39,36 +40,30 @@ module Ferver
|
|
39
40
|
end
|
40
41
|
|
41
42
|
# download file
|
42
|
-
get
|
43
|
-
send_file(
|
44
|
-
@file.path_to_file, disposition: 'attachment', filename: @file.name
|
45
|
-
)
|
43
|
+
get "/files/:id" do
|
44
|
+
send_file @file.path_to_file, disposition: String.new("attachment"), filename: @file.name
|
46
45
|
end
|
47
46
|
|
48
47
|
private
|
49
48
|
|
50
49
|
attr_reader :ferver_list
|
51
50
|
|
52
|
-
def
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
def valid_file_request?
|
57
|
-
file_id_request.valid?
|
51
|
+
def find_file!(file_id)
|
52
|
+
ferver_list.file_by_id(file_id)
|
53
|
+
rescue Ferver::FileNotFoundError => error
|
54
|
+
halt 404, error.message
|
58
55
|
end
|
59
56
|
|
60
|
-
def
|
61
|
-
|
62
|
-
rescue IndexError
|
63
|
-
halt 404, 'File requested not found.'
|
57
|
+
def json_request?
|
58
|
+
request.preferred_type.to_s == "application/json"
|
64
59
|
end
|
65
60
|
|
66
61
|
def current_ferver_path
|
67
|
-
Ferver.configuration.
|
62
|
+
Ferver.configuration.directory.path
|
68
63
|
end
|
69
64
|
|
70
65
|
def current_full_path
|
71
|
-
|
66
|
+
Ferver.configuration.directory.full_path
|
72
67
|
end
|
73
68
|
end
|
74
69
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Ferver
|
3
|
+
# A wrapper around the directory specified by config
|
4
|
+
class FerverDirectory
|
5
|
+
def initialize(configuration)
|
6
|
+
path = configuration.directory_path
|
7
|
+
raise ArgumentError, "No path is specified" if path.empty?
|
8
|
+
raise DirectoryNotFoundError unless Dir.exist?(path)
|
9
|
+
|
10
|
+
@configuration = configuration
|
11
|
+
end
|
12
|
+
|
13
|
+
def path
|
14
|
+
configuration.directory_path
|
15
|
+
end
|
16
|
+
|
17
|
+
def full_path
|
18
|
+
@_full_path ||= File.expand_path(path)
|
19
|
+
end
|
20
|
+
|
21
|
+
def found_files
|
22
|
+
find_files
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
attr_reader :configuration
|
28
|
+
|
29
|
+
# Iterate through files in specified dir for valid files
|
30
|
+
#
|
31
|
+
def find_files
|
32
|
+
[].tap do |results|
|
33
|
+
Dir.foreach(full_path) do |file_name|
|
34
|
+
next if file_name == "." || file_name == ".."
|
35
|
+
next if file_name =~ /^\./ && !configuration.serve_hidden?
|
36
|
+
|
37
|
+
found_file = FoundFile.new(full_path, file_name)
|
38
|
+
results << found_file if found_file.valid?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|