Make the tests work.
This commit is contained in:
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
/.bundle/
|
||||
/.yardoc
|
||||
/_yardoc/
|
||||
/coverage/
|
||||
/doc/
|
||||
/pkg/
|
||||
/spec/reports/
|
||||
/tmp/
|
||||
8
.rubocop.yml
Normal file
8
.rubocop.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
AllCops:
|
||||
TargetRubyVersion: 3.0
|
||||
|
||||
Style/StringLiterals:
|
||||
EnforcedStyle: double_quotes
|
||||
|
||||
Style/StringLiteralsInInterpolation:
|
||||
EnforcedStyle: double_quotes
|
||||
5
CHANGELOG.md
Normal file
5
CHANGELOG.md
Normal file
@@ -0,0 +1,5 @@
|
||||
## [Unreleased]
|
||||
|
||||
## [0.1.0] - 2024-10-01
|
||||
|
||||
- Initial release
|
||||
12
Gemfile
Normal file
12
Gemfile
Normal file
@@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
source "https://rubygems.org"
|
||||
|
||||
# Specify your gem's dependencies in moviehash.gemspec
|
||||
gemspec
|
||||
|
||||
gem "rake", "~> 13.0"
|
||||
|
||||
gem "minitest", "~> 5.16"
|
||||
|
||||
gem "rubocop", "~> 1.21"
|
||||
21
LICENSE.txt
Normal file
21
LICENSE.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2024 Dan Milne
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
35
README.md
Normal file
35
README.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# Moviehash
|
||||
|
||||
TODO: Delete this and the text below, and describe your gem
|
||||
|
||||
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/moviehash`. To experiment with that code, run `bin/console` for an interactive prompt.
|
||||
|
||||
## Installation
|
||||
|
||||
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
||||
|
||||
Install the gem and add to the application's Gemfile by executing:
|
||||
|
||||
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
||||
|
||||
If bundler is not being used to manage dependencies, install the gem by executing:
|
||||
|
||||
$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
||||
|
||||
## Usage
|
||||
|
||||
TODO: Write usage instructions here
|
||||
|
||||
## Development
|
||||
|
||||
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
||||
|
||||
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
||||
|
||||
## Contributing
|
||||
|
||||
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/moviehash.
|
||||
|
||||
## License
|
||||
|
||||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
||||
12
Rakefile
Normal file
12
Rakefile
Normal file
@@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "bundler/gem_tasks"
|
||||
require "minitest/test_task"
|
||||
|
||||
Minitest::TestTask.create
|
||||
|
||||
require "rubocop/rake_task"
|
||||
|
||||
RuboCop::RakeTask.new
|
||||
|
||||
task default: %i[test rubocop]
|
||||
11
bin/console
Executable file
11
bin/console
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "bundler/setup"
|
||||
require "moviehash"
|
||||
|
||||
# You can add fixtures and/or initialization code here to make experimenting
|
||||
# with your gem easier. You can also use a different console, if you like.
|
||||
|
||||
require "irb"
|
||||
IRB.start(__FILE__)
|
||||
8
bin/setup
Executable file
8
bin/setup
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
set -vx
|
||||
|
||||
bundle install
|
||||
|
||||
# Do any other automated setup that you need to do here
|
||||
95
lib/moviehash.rb
Normal file
95
lib/moviehash.rb
Normal file
@@ -0,0 +1,95 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "moviehash/version"
|
||||
require "net/http"
|
||||
require "uri"
|
||||
|
||||
module Moviehash
|
||||
class Error < StandardError; end
|
||||
class FileNotFoundError < Error; end
|
||||
class NetworkError < Error; end
|
||||
|
||||
CHUNK_SIZE = 64 * 1024 # in bytes
|
||||
|
||||
def self.compute_hash(url)
|
||||
data = url.start_with?("http") ? data_from_url(url) : data_from_file(url)
|
||||
|
||||
hash = data[:filesize]
|
||||
|
||||
hash = process_chunk(data.dig(:chunks, 0), hash)
|
||||
hash = process_chunk(data.dig(:chunks, 1), hash)
|
||||
|
||||
format("%016x", hash)
|
||||
end
|
||||
|
||||
def self.data_from_file(path)
|
||||
filesize = File.size(path)
|
||||
|
||||
data = { filesize: filesize, chunks: [] }
|
||||
|
||||
File.open(path, "rb") do |f|
|
||||
data[:chunks] << f.read(CHUNK_SIZE)
|
||||
f.seek([0, filesize - CHUNK_SIZE].max, IO::SEEK_SET)
|
||||
data[:chunks] << f.read(CHUNK_SIZE)
|
||||
end
|
||||
|
||||
data
|
||||
end
|
||||
|
||||
def self.data_from_url(url)
|
||||
uri = URI(url)
|
||||
http = Net::HTTP.new(uri.host, uri.port)
|
||||
http.use_ssl = (uri.scheme == "https")
|
||||
|
||||
# Get the file size
|
||||
response = http.request_head(uri.path)
|
||||
filesize = response["content-length"].to_i
|
||||
|
||||
data = { filesize: filesize, chunks: [] }
|
||||
|
||||
# Process the beginning of the file
|
||||
response = http.get(uri.path, { "Range" => "bytes=0-#{CHUNK_SIZE - 1}" })
|
||||
data[:chunks] << response.body
|
||||
|
||||
# Process the end of the file
|
||||
start_byte = [0, filesize - CHUNK_SIZE].max
|
||||
response = http.get(uri.path, { "Range" => "bytes=#{start_byte}-#{filesize - 1}" })
|
||||
data[:chunks] << response.body
|
||||
|
||||
data
|
||||
end
|
||||
|
||||
def self.process_chunk(chunk, hash)
|
||||
chunk.unpack("Q*").each do |n|
|
||||
hash = hash + n & 0xffffffffffffffff
|
||||
end
|
||||
|
||||
hash
|
||||
end
|
||||
end
|
||||
|
||||
def self.old_compute_hash(path)
|
||||
filesize = File.size(path)
|
||||
hash = filesize
|
||||
|
||||
format("%016x", hash)
|
||||
|
||||
# Read 64 kbytes, divide up into 64 bits and add each
|
||||
# to hash. Do for beginning and end of file.
|
||||
File.open(path, "rb") do |f|
|
||||
# Q = unsigned long long = 64 bit
|
||||
f.read(CHUNK_SIZE).unpack("Q*").each do |n|
|
||||
hash = hash + n & 0xffffffffffffffff # to remain as 64 bit number
|
||||
end
|
||||
format("%016x", hash)
|
||||
f.seek([0, filesize - CHUNK_SIZE].max, IO::SEEK_SET)
|
||||
|
||||
# And again for the end of the file
|
||||
f.read(CHUNK_SIZE).unpack("Q*").each do |n|
|
||||
hash = hash + n & 0xffffffffffffffff
|
||||
end
|
||||
format("%016x", hash)
|
||||
end
|
||||
|
||||
format("%016x", hash)
|
||||
end
|
||||
5
lib/moviehash/version.rb
Normal file
5
lib/moviehash/version.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Moviehash
|
||||
VERSION = "0.1.0"
|
||||
end
|
||||
41
moviehash.gemspec
Normal file
41
moviehash.gemspec
Normal file
@@ -0,0 +1,41 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "lib/moviehash/version"
|
||||
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = "moviehash"
|
||||
spec.version = Moviehash::VERSION
|
||||
spec.authors = ["Dan Milne"]
|
||||
spec.email = ["d@nmilne.com"]
|
||||
|
||||
spec.summary = "TODO: Write a short summary, because RubyGems requires one."
|
||||
spec.description = "TODO: Write a longer description or delete this line."
|
||||
spec.homepage = "TODO: Put your gem's website or public repo URL here."
|
||||
spec.license = "MIT"
|
||||
spec.required_ruby_version = ">= 3.0.0"
|
||||
|
||||
spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
||||
|
||||
spec.metadata["homepage_uri"] = spec.homepage
|
||||
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
||||
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
||||
|
||||
# Specify which files should be added to the gem when it is released.
|
||||
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
||||
gemspec = File.basename(__FILE__)
|
||||
spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls|
|
||||
ls.readlines("\x0", chomp: true).reject do |f|
|
||||
(f == gemspec) ||
|
||||
f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
|
||||
end
|
||||
end
|
||||
spec.bindir = "exe"
|
||||
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
||||
spec.require_paths = ["lib"]
|
||||
|
||||
# Uncomment to register a new dependency of your gem
|
||||
spec.add_dependency "webrick"
|
||||
|
||||
# For more information and examples about making a new gem, check out our
|
||||
# guide at: https://bundler.io/guides/creating_gem.html
|
||||
end
|
||||
4
sig/moviehash.rbs
Normal file
4
sig/moviehash.rbs
Normal file
@@ -0,0 +1,4 @@
|
||||
module Moviehash
|
||||
VERSION: String
|
||||
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
|
||||
end
|
||||
6
test/test_helper.rb
Normal file
6
test/test_helper.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
||||
require "moviehash"
|
||||
|
||||
require "minitest/autorun"
|
||||
72
test/test_moviehash.rb
Normal file
72
test/test_moviehash.rb
Normal file
@@ -0,0 +1,72 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
require "webrick"
|
||||
require "debug"
|
||||
|
||||
class TestMoviehash < Minitest::Test
|
||||
def setup
|
||||
@file_path = File.expand_path("test/files", Dir.pwd) # Set absolute path
|
||||
@port = find_available_port
|
||||
|
||||
@webrick = WEBrick::HTTPServer.new(
|
||||
Port: @port,
|
||||
AccessLog: [],
|
||||
Logger: WEBrick::Log.new(File::NULL)
|
||||
)
|
||||
|
||||
@webrick.mount "/files", WEBrick::HTTPServlet::FileHandler, @file_path
|
||||
|
||||
@thread = Thread.new { @webrick.start }
|
||||
|
||||
# Give the server a moment to start
|
||||
sleep 0.1
|
||||
end
|
||||
|
||||
def teardown
|
||||
@webrick.shutdown
|
||||
@thread.join
|
||||
end
|
||||
|
||||
def test_cases
|
||||
{
|
||||
"breakdance.avi" => "8e245d9679d31e12",
|
||||
"dummy.bin" => "61f7751fc2a72bfb"
|
||||
}
|
||||
end
|
||||
|
||||
def test_that_it_has_a_version_number
|
||||
refute_nil ::Moviehash::VERSION
|
||||
end
|
||||
|
||||
def test_hash_calc
|
||||
# wget https://static.opensubtitles.org/addons/avi/dummy.rar ; unrar x dummy.rar
|
||||
# wget https://static.opensubtitles.org/addons/avi/breakdance.avi
|
||||
|
||||
test_cases.each do |file, hash|
|
||||
assert_equal hash, Moviehash.compute_hash("test/files/#{file}")
|
||||
end
|
||||
end
|
||||
|
||||
def test_hash_calc_url
|
||||
File.expand_path("files", __dir__)
|
||||
# test_cases.each do |file, hash|
|
||||
# FakeWeb.register_uri(:get, "http://example.com/#{file}", :body => File.join(file_path, file) )
|
||||
# FakeWeb.register_uri(:head, "http://example.com/#{file}", :content_length => File.size( File.join(file_path, file)) )
|
||||
# end
|
||||
|
||||
test_cases.each do |file, hash|
|
||||
uri = URI.parse("http://localhost:#{@port}/files/#{file}")
|
||||
assert_equal hash, Moviehash.compute_hash(uri.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_available_port
|
||||
server = TCPServer.new("127.0.0.1", 0)
|
||||
port = server.addr[1]
|
||||
server.close
|
||||
port
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user