From a5d891c4127d252d9145aca68723b87f7d9c68c3 Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Sun, 27 Oct 2024 22:38:44 +1100 Subject: [PATCH] Make the tests work. --- .gitignore | 8 ++++ .rubocop.yml | 8 ++++ CHANGELOG.md | 5 +++ Gemfile | 12 +++++ LICENSE.txt | 21 +++++++++ README.md | 35 +++++++++++++++ Rakefile | 12 +++++ bin/console | 11 +++++ bin/setup | 8 ++++ lib/moviehash.rb | 95 ++++++++++++++++++++++++++++++++++++++++ lib/moviehash/version.rb | 5 +++ moviehash.gemspec | 41 +++++++++++++++++ sig/moviehash.rbs | 4 ++ test/test_helper.rb | 6 +++ test/test_moviehash.rb | 72 ++++++++++++++++++++++++++++++ 15 files changed, 343 insertions(+) create mode 100644 .gitignore create mode 100644 .rubocop.yml create mode 100644 CHANGELOG.md create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100755 bin/console create mode 100755 bin/setup create mode 100644 lib/moviehash.rb create mode 100644 lib/moviehash/version.rb create mode 100644 moviehash.gemspec create mode 100644 sig/moviehash.rbs create mode 100644 test/test_helper.rb create mode 100644 test/test_moviehash.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9106b2a --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..762eebb --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,8 @@ +AllCops: + TargetRubyVersion: 3.0 + +Style/StringLiterals: + EnforcedStyle: double_quotes + +Style/StringLiteralsInInterpolation: + EnforcedStyle: double_quotes diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d1ba324 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +## [Unreleased] + +## [0.1.0] - 2024-10-01 + +- Initial release diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..ce3bc01 --- /dev/null +++ b/Gemfile @@ -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" diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..eaf1a3c --- /dev/null +++ b/LICENSE.txt @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d39bfc3 --- /dev/null +++ b/README.md @@ -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). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2bf771f --- /dev/null +++ b/Rakefile @@ -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] diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..83a476c --- /dev/null +++ b/bin/console @@ -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__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -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 diff --git a/lib/moviehash.rb b/lib/moviehash.rb new file mode 100644 index 0000000..eb64a4f --- /dev/null +++ b/lib/moviehash.rb @@ -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 diff --git a/lib/moviehash/version.rb b/lib/moviehash/version.rb new file mode 100644 index 0000000..dee2df6 --- /dev/null +++ b/lib/moviehash/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module Moviehash + VERSION = "0.1.0" +end diff --git a/moviehash.gemspec b/moviehash.gemspec new file mode 100644 index 0000000..ce648de --- /dev/null +++ b/moviehash.gemspec @@ -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 diff --git a/sig/moviehash.rbs b/sig/moviehash.rbs new file mode 100644 index 0000000..f4856a1 --- /dev/null +++ b/sig/moviehash.rbs @@ -0,0 +1,4 @@ +module Moviehash + VERSION: String + # See the writing guide of rbs: https://github.com/ruby/rbs#guides +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..2901fcf --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) +require "moviehash" + +require "minitest/autorun" diff --git a/test/test_moviehash.rb b/test/test_moviehash.rb new file mode 100644 index 0000000..4c5d5b4 --- /dev/null +++ b/test/test_moviehash.rb @@ -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