From 5c9572dd26d6966068770d0c5edb9a951f9cb749 Mon Sep 17 00:00:00 2001 From: aidewoode Date: Mon, 4 Jul 2022 17:31:43 +0800 Subject: [PATCH 1/3] Add basic implementation for listen media changes --- Gemfile | 3 +++ Gemfile.lock | 7 +++++++ Procfile.dev | 1 + lib/tasks/listen.rake | 11 +++++++++++ 4 files changed, 22 insertions(+) create mode 100644 lib/tasks/listen.rake diff --git a/Gemfile b/Gemfile index ae11e46f..f9edb568 100644 --- a/Gemfile +++ b/Gemfile @@ -61,6 +61,9 @@ gem "acts_as_list", "~> 1.0.2" gem "authlogic", "~> 6.4.1" gem "bcrypt", "~> 3.1.11" +# For sync on library changes +gem "listen", "~> 3.7.1" + # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index e42393ef..d4656da2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -151,6 +151,9 @@ GEM activesupport (>= 5.0.0) jsbundling-rails (1.0.0) railties (>= 6.0.0) + listen (3.7.1) + rb-fsevent (~> 0.10, >= 0.10.3) + rb-inotify (~> 0.9, >= 0.9.10) loofah (2.18.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) @@ -230,6 +233,9 @@ GEM zeitwerk (~> 2.5) rainbow (3.1.1) rake (13.0.6) + rb-fsevent (0.11.1) + rb-inotify (0.10.1) + ffi (~> 1.0) redis (4.5.1) redis-objects (1.5.1) redis (~> 4.2) @@ -326,6 +332,7 @@ DEPENDENCIES httparty (~> 0.17.0) jbuilder (~> 2.11.5) jsbundling-rails (~> 1.0.0) + listen (~> 3.7.1) memory_profiler (~> 0.9.13) pagy (~> 5.6.6) pg (~> 1.3.2) diff --git a/Procfile.dev b/Procfile.dev index 399e67fa..fa1fb170 100644 --- a/Procfile.dev +++ b/Procfile.dev @@ -4,3 +4,4 @@ web: rails s -p 3000 sidekiq: sidekiq css: yarn build:css --watch js: yarn build --watch +listen: rails listen diff --git a/lib/tasks/listen.rake b/lib/tasks/listen.rake new file mode 100644 index 00000000..683862e6 --- /dev/null +++ b/lib/tasks/listen.rake @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +task listen: :environment do + listener = Listen.to(File.expand_path(Setting.media_path)) do |modified, added, removed| + MediaSyncJob.perform_later + end + + listener.start + + sleep +end From eb9997c48d2605aa46d0db95b2dc2ed693d3406e Mon Sep 17 00:00:00 2001 From: aidewoode Date: Wed, 6 Jul 2022 17:34:31 +0800 Subject: [PATCH 2/3] Add selective options for sync --- Procfile.dev | 2 +- app/jobs/media_sync_job.rb | 4 +- app/models/media.rb | 47 ++++++++++---- app/models/media_file.rb | 19 ++++-- app/models/song.rb | 2 +- ...20705083238_add_file_path_hash_to_songs.rb | 15 +++++ db/schema.rb | 5 +- docker-compose.yml | 4 ++ lib/tasks/listen.rake | 11 ---- lib/tasks/listen_media_change.rake | 15 +++++ ...ached_transcoded_stream_controller_test.rb | 2 +- test/fixtures/songs.yml | 55 +++++++++++----- test/jobs/media_sync_job_test.rb | 2 +- test/models/album_test.rb | 6 +- test/models/media_test.rb | 65 +++++++++++++++++++ test/models/stream_test.rb | 2 +- test/system/songs_test.rb | 1 + test/test_helper.rb | 5 +- 18 files changed, 201 insertions(+), 61 deletions(-) create mode 100644 db/migrate/20220705083238_add_file_path_hash_to_songs.rb delete mode 100644 lib/tasks/listen.rake create mode 100644 lib/tasks/listen_media_change.rake diff --git a/Procfile.dev b/Procfile.dev index fa1fb170..b044d69a 100644 --- a/Procfile.dev +++ b/Procfile.dev @@ -4,4 +4,4 @@ web: rails s -p 3000 sidekiq: sidekiq css: yarn build:css --watch js: yarn build --watch -listen: rails listen +listen: rails listen_media_changes diff --git a/app/jobs/media_sync_job.rb b/app/jobs/media_sync_job.rb index ff378101..f4af851f 100644 --- a/app/jobs/media_sync_job.rb +++ b/app/jobs/media_sync_job.rb @@ -3,8 +3,8 @@ class MediaSyncJob < ApplicationJob before_enqueue :start_syncing after_perform :stop_syncing - def perform - Media.sync + def perform(type = :all, file_paths = []) + Media.sync(type, file_paths) end private diff --git a/app/models/media.rb b/app/models/media.rb index 1a0edca1..26e4eccb 100644 --- a/app/models/media.rb +++ b/app/models/media.rb @@ -16,15 +16,22 @@ def id end class << self - def sync - media_hashes = MediaFile.file_paths.map do |file_path| - @file_info = MediaFile.file_info(file_path) - @file_info[:md5_hash] if attach - rescue - next - end.compact - - clean_up(media_hashes) + def sync(type = :all, file_paths = []) + file_paths = MediaFile.file_paths if type == :all + return if file_paths.blank? + + case type + when :all + file_hashes = add_files(file_paths) + clean_up(file_hashes) + when :added + add_files(file_paths) + when :removed + remove_files(file_paths) + when :modified + remove_files(file_paths) + add_files(file_paths) + end end def syncing? @@ -38,6 +45,22 @@ def syncing=(is_syncing) private + def add_files(file_paths) + file_paths.map do |file_path| + @file_info = MediaFile.file_info(file_path) + @file_info[:md5_hash] if attach + rescue + next + end.compact + end + + def remove_files(file_paths) + file_path_hashes = file_paths.map { |file_path| MediaFile.get_md5_hash(file_path) } + Song.where(file_path_hash: file_path_hashes).destroy_all + + clean_up + end + def attach artist = Artist.find_or_create_by!(name: @file_info[:artist_name]) @@ -57,7 +80,7 @@ def attach end def song_info - @file_info.slice(:name, :tracknum, :duration, :file_path) + @file_info.slice(:name, :tracknum, :duration, :file_path, :file_path_hash) end def various_artists? @@ -65,8 +88,8 @@ def various_artists? albumartist.present? && (albumartist.casecmp("various artists").zero? || albumartist != @file_info[:artist_name]) end - def clean_up(media_hashes) - Song.where.not(md5_hash: media_hashes).destroy_all + def clean_up(file_hashes = []) + Song.where.not(md5_hash: file_hashes).destroy_all if file_hashes.present? # Clean up no content albums and artist. Album.where.missing(:songs).destroy_all diff --git a/app/models/media_file.rb b/app/models/media_file.rb index 7934f0d9..ff182a06 100644 --- a/app/models/media_file.rb +++ b/app/models/media_file.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true class MediaFile - SUPPORT_FORMATE = WahWah.support_formats.freeze + SUPPORTED_FORMATS = WahWah.support_formats.freeze class << self def file_paths media_path = File.expand_path(Setting.media_path) - Dir.glob("#{media_path}/**/*.{#{SUPPORT_FORMATE.join(",")}}", File::FNM_CASEFOLD) + Dir.glob("#{media_path}/**/*.{#{SUPPORTED_FORMATS.join(",")}}", File::FNM_CASEFOLD) end def format(file_path) @@ -22,7 +22,16 @@ def image(file_path) def file_info(file_path) tag_info = get_tag_info(file_path) - tag_info.merge(file_path: file_path, md5_hash: get_md5_hash(file_path)) + tag_info.merge( + file_path: file_path.to_s, + file_path_hash: get_md5_hash(file_path), + md5_hash: get_md5_hash(file_path, with_mtime: true) + ) + end + + def get_md5_hash(file_path, with_mtime: false) + string = "#{file_path}#{File.mtime(file_path) if with_mtime}" + Digest::MD5.base64digest(string) end private @@ -39,9 +48,5 @@ def get_tag_info(file_path) duration: tag.duration.round } end - - def get_md5_hash(file_path) - Digest::MD5.base64digest(file_path.to_s + File.mtime(file_path).to_s) - end end end diff --git a/app/models/song.rb b/app/models/song.rb index 3f88befc..550f7e84 100644 --- a/app/models/song.rb +++ b/app/models/song.rb @@ -3,7 +3,7 @@ class Song < ApplicationRecord include Searchable - validates :name, :file_path, :md5_hash, presence: true + validates :name, :file_path, :file_path_hash, :md5_hash, presence: true belongs_to :album, touch: true belongs_to :artist, touch: true diff --git a/db/migrate/20220705083238_add_file_path_hash_to_songs.rb b/db/migrate/20220705083238_add_file_path_hash_to_songs.rb new file mode 100644 index 00000000..6ee0c5e9 --- /dev/null +++ b/db/migrate/20220705083238_add_file_path_hash_to_songs.rb @@ -0,0 +1,15 @@ +class AddFilePathHashToSongs < ActiveRecord::Migration[7.0] + def change + add_column :songs, :file_path_hash, :string + add_index :songs, :file_path_hash + add_index :songs, :md5_hash + + reversible do |dir| + dir.up do + Song.find_each do |song| + song.update_column(:file_path_hash, MediaFile.get_md5_hash(song.file_path)) + end + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 6756ac79..cef37b8c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_05_31_070546) do +ActiveRecord::Schema[7.0].define(version: 2022_07_05_083238) do # These are extensions that must be enabled in order to support this database enable_extension "hstore" enable_extension "pg_trgm" @@ -68,8 +68,11 @@ t.datetime "updated_at", null: false t.bigint "album_id" t.bigint "artist_id" + t.string "file_path_hash" t.index ["album_id"], name: "index_songs_on_album_id" t.index ["artist_id"], name: "index_songs_on_artist_id" + t.index ["file_path_hash"], name: "index_songs_on_file_path_hash" + t.index ["md5_hash"], name: "index_songs_on_md5_hash" t.index ["name"], name: "index_songs_on_name" end diff --git a/docker-compose.yml b/docker-compose.yml index 57e016a8..d20bce92 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -42,6 +42,10 @@ services: ports: - 80:80 command: nginx -g 'pid /tmp/nginx.pid; daemon off;' + listener: + container_name: 'blackcandy_listener' + <<: *app_base + command: bundle exec rails listen_media_changes volumes: production_db_data: production_redis_data: diff --git a/lib/tasks/listen.rake b/lib/tasks/listen.rake deleted file mode 100644 index 683862e6..00000000 --- a/lib/tasks/listen.rake +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -task listen: :environment do - listener = Listen.to(File.expand_path(Setting.media_path)) do |modified, added, removed| - MediaSyncJob.perform_later - end - - listener.start - - sleep -end diff --git a/lib/tasks/listen_media_change.rake b/lib/tasks/listen_media_change.rake new file mode 100644 index 00000000..16643581 --- /dev/null +++ b/lib/tasks/listen_media_change.rake @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +task listen_media_changes: :environment do + supported_formates = MediaFile::SUPPORTED_FORMATS.map { |formate| %r{\.#{formate}$} } + + listener = Listen.to(File.expand_path(Setting.media_path), only: supported_formates) do |modified, added, removed| + MediaSyncJob.perform_later(:modified, modified) if modified.present? + MediaSyncJob.perform_later(:added, added) if added.present? + MediaSyncJob.perform_later(:removed, removed) if removed.present? + end + + listener.start + + sleep +end diff --git a/test/controllers/cached_transcoded_stream_controller_test.rb b/test/controllers/cached_transcoded_stream_controller_test.rb index fee27c37..47cdbaf4 100644 --- a/test/controllers/cached_transcoded_stream_controller_test.rb +++ b/test/controllers/cached_transcoded_stream_controller_test.rb @@ -17,7 +17,7 @@ class CachedTranscodedStreamControllerTest < ActionDispatch::IntegrationTest test "should set header for nginx send file" do get new_cached_transcoded_stream_url(song_id: songs(:flac_sample).id) - assert_equal "/private_cache_media/2/ZmFrZV9tZDU=_128.mp3", @response.get_header("X-Accel-Redirect") + assert_equal "/private_cache_media/2/Y0YxRW91S2cyUmtXMklNblpsUENIUT09_128.mp3", @response.get_header("X-Accel-Redirect") end test "should set correct content type header" do diff --git a/test/fixtures/songs.yml b/test/fixtures/songs.yml index 78c3202b..3d77e670 100644 --- a/test/fixtures/songs.yml +++ b/test/fixtures/songs.yml @@ -1,8 +1,19 @@ +<% mp3_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist1_album2.mp3') %> +<% flac_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist1_album1.flac') %> +<% ogg_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.ogg') %> +<% wav_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.wav') %> +<% opus_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.opus') %> +<% m4a_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist1_album1.m4a') %> +<% oga_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.oga') %> +<% wma_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.wma') %> +<% various_artists_file_path = Rails.root.join('test', 'fixtures', 'files', 'various_artists.mp3') %> + mp3_sample: id: 1 name: 'mp3_sample' - file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist1_album2.mp3') %> - md5_hash: 'fake_md5' + file_path: <%= mp3_file_path %> + file_path_hash: <%= MediaFile.get_md5_hash(mp3_file_path) %> + md5_hash: <%= MediaFile.get_md5_hash(mp3_file_path, with_mtime: true)%> artist: 'artist1' album: 'album2' duration: 8.0 @@ -10,8 +21,9 @@ mp3_sample: flac_sample: id: 2 name: 'flac_sample' - file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist1_album1.flac') %> - md5_hash: 'fake_md5' + file_path: <%= flac_file_path %> + file_path_hash: <%= MediaFile.get_md5_hash(flac_file_path) %> + md5_hash: <%= MediaFile.get_md5_hash(flac_file_path, with_mtime: true) %> artist: 'artist1' album: 'album1' duration: 8.0 @@ -19,8 +31,9 @@ flac_sample: ogg_sample: id: 3 name: 'ogg_sample' - file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.ogg') %> - md5_hash: 'fake_md5' + file_path: <%= ogg_file_path %> + file_path_hash: <%= MediaFile.get_md5_hash(ogg_file_path) %> + md5_hash: <%= MediaFile.get_md5_hash(ogg_file_path, with_mtime: true) %> artist: 'artist2' album: 'album3' duration: 8.0 @@ -28,8 +41,9 @@ ogg_sample: wav_sample: id: 4 name: 'wav_sample' - file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.wav') %> - md5_hash: 'fake_md5' + file_path: <%= wav_file_path %> + file_path_hash: <%= MediaFile.get_md5_hash(wav_file_path) %> + md5_hash: <%= MediaFile.get_md5_hash(wav_file_path, with_mtime: true) %> artist: 'artist2' album: 'album3' duration: 8.0 @@ -37,8 +51,9 @@ wav_sample: opus_sample: id: 5 name: 'opus_sample' - file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.opus') %> - md5_hash: 'fake_md5' + file_path: <%= opus_file_path %> + file_path_hash: <%= MediaFile.get_md5_hash(opus_file_path) %> + md5_hash: <%= MediaFile.get_md5_hash(opus_file_path, with_mtime: true) %> artist: 'artist2' album: 'album3' duration: 8.0 @@ -46,8 +61,9 @@ opus_sample: m4a_sample: id: 6 name: 'm4a_sample' - file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist1_album1.m4a') %> - md5_hash: 'fake_md5' + file_path: <%= m4a_file_path %> + file_path_hash: <%= MediaFile.get_md5_hash(m4a_file_path) %> + md5_hash: <%= MediaFile.get_md5_hash(m4a_file_path, with_mtime: true) %> artist: 'artist1' album: 'album1' duration: 8.0 @@ -55,8 +71,9 @@ m4a_sample: oga_sample: id: 7 name: 'oga_sample' - file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.oga') %> - md5_hash: 'fake_md5' + file_path: <%= oga_file_path %> + file_path_hash: <%= MediaFile.get_md5_hash(oga_file_path) %> + md5_hash: <%= MediaFile.get_md5_hash(oga_file_path, with_mtime: true) %> artist: 'artist2' album: 'album3' duration: 8.0 @@ -64,8 +81,9 @@ oga_sample: wma_sample: id: 8 name: 'wma_sample' - file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.wma') %> - md5_hash: 'fake_md5' + file_path: <%= wma_file_path %> + file_path_hash: <%= MediaFile.get_md5_hash(wma_file_path) %> + md5_hash: <%= MediaFile.get_md5_hash(wma_file_path, with_mtime: true) %> artist: 'artist2' album: 'album3' duration: 8.0 @@ -73,8 +91,9 @@ wma_sample: various_artists_sample: id: 9 name: 'various_artists_sample' - file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'various_artists.mp3') %> - md5_hash: 'fake_md5' + file_path: <%= various_artists_file_path %> + file_path_hash: <%= MediaFile.get_md5_hash(various_artists_file_path) %> + md5_hash: <%= MediaFile.get_md5_hash(various_artists_file_path, with_mtime: true) %> artist: 'artist1' album: 'album4' duration: 8.0 diff --git a/test/jobs/media_sync_job_test.rb b/test/jobs/media_sync_job_test.rb index 1f4e11d5..70f7a77a 100644 --- a/test/jobs/media_sync_job_test.rb +++ b/test/jobs/media_sync_job_test.rb @@ -9,7 +9,7 @@ class MediaSyncJobTest < ActiveJob::TestCase test "sync media" do mock = MiniTest::Mock.new - mock.expect(:call, true) + mock.expect(:call, true, [:all, []]) Setting.update(media_path: Rails.root.join("test/fixtures/files")) assert_not Media.syncing? diff --git a/test/models/album_test.rb b/test/models/album_test.rb index 5ca47995..3ff1c16d 100644 --- a/test/models/album_test.rb +++ b/test/models/album_test.rb @@ -18,9 +18,9 @@ class AlbumTest < ActiveSupport::TestCase album.songs.create!( [ - {name: "test_song_1", file_path: "fake_path", md5_hash: "fake_md5", tracknum: 2, artist: artist}, - {name: "test_song_2", file_path: "fake_path", md5_hash: "fake_md5", tracknum: 3, artist: artist}, - {name: "test_song_3", file_path: "fake_path", md5_hash: "fake_md5", tracknum: 1, artist: artist} + {name: "test_song_1", file_path: "fake_path", file_path_hash: "fake_path_hash", md5_hash: "fake_md5", tracknum: 2, artist: artist}, + {name: "test_song_2", file_path: "fake_path", file_path_hash: "fake_path_hash", md5_hash: "fake_md5", tracknum: 3, artist: artist}, + {name: "test_song_3", file_path: "fake_path", file_path_hash: "fake_path_hash", md5_hash: "fake_md5", tracknum: 1, artist: artist} ] ) diff --git a/test/models/media_test.rb b/test/models/media_test.rb index 29034945..0da778e7 100644 --- a/test/models/media_test.rb +++ b/test/models/media_test.rb @@ -147,4 +147,69 @@ class MediaTest < ActiveSupport::TestCase assert_equal 0, Album.count end end + + test "should add records and create associations when selectively added files" do + clear_media_data + + Media.sync(:added, [file_fixture("artist1_album2.mp3"), file_fixture("artist1_album1.flac")]) + + assert_equal 1, Artist.count + assert_equal 2, Album.count + assert_equal 2, Song.count + + Media.sync(:added, [file_fixture("artist1_album1.m4a")]) + + assert_equal Song.where(name: %w[flac_sample m4a_sample]).ids.sort, Album.find_by(name: "album1").songs.ids.sort + assert_equal Song.where(name: %w[mp3_sample flac_sample m4a_sample]).ids.sort, Artist.find_by(name: "artist1").songs.ids.sort + end + + test "should remove records when selectively removed files" do + Media.sync(:removed, [file_fixture("artist1_album2.mp3"), file_fixture("artist1_album1.flac")]) + + assert_nil Song.find_by(name: "mp3_sample") + assert_nil Song.find_by(name: "flac_sample") + assert_nil Album.find_by(name: "album2") + + Media.sync(:removed, [file_fixture("artist1_album1.m4a"), file_fixture("various_artists.mp3")]) + + assert_nil Song.find_by(name: "various_artists_sample") + assert_nil Song.find_by(name: "m4a_sample") + assert_nil Album.find_by(name: "album1") + assert_nil Artist.find_by(name: "artist1") + end + + test "should change associations when selectively modified album info on file" do + MediaFile.stub(:file_info, media_file_info_stub(file_fixture("artist1_album2.mp3"), album_name: "album1")) do + Media.sync(:modified, [file_fixture("artist1_album2.mp3")]) + + album1_songs_ids = Song.where(name: %w[flac_sample m4a_sample mp3_sample]).ids.sort + + assert_equal Album.where(name: "album1").ids.sort, Artist.find_by(name: "artist1").albums.ids.sort + assert_equal album1_songs_ids, Album.find_by(name: "album1").songs.ids.sort + end + end + + test "should change associations when selectively modified artist info on file" do + MediaFile.stub( + :file_info, + media_file_info_stub(file_fixture("artist1_album2.mp3"), artist_name: "artist2", albumartist_name: "artist2") + ) do + Media.sync(:modified, [file_fixture("artist1_album2.mp3")]) + + artist2_songs_ids = Song.where( + name: %w[mp3_sample ogg_sample wav_sample opus_sample oga_sample wma_sample] + ).ids.sort + + assert_equal Album.where(name: %w[album2 album3]).ids.sort, Artist.find_by(name: "artist2").albums.ids.sort + assert_equal artist2_songs_ids, Artist.find_by(name: "artist2").songs.ids.sort + end + end + + test "should change song attribute when selectively modified song info on file" do + MediaFile.stub(:file_info, media_file_info_stub(file_fixture("artist1_album2.mp3"), tracknum: 2)) do + assert_changes -> { Song.find_by(name: "mp3_sample").tracknum }, from: 1, to: 2 do + Media.sync(:modified, [file_fixture("artist1_album2.mp3")]) + end + end + end end diff --git a/test/models/stream_test.rb b/test/models/stream_test.rb index d04333c1..c4160270 100644 --- a/test/models/stream_test.rb +++ b/test/models/stream_test.rb @@ -91,7 +91,7 @@ class StreamTest < ActiveSupport::TestCase test "should get transcode cache file path" do stream = Stream.new(songs(:flac_sample)) - assert_equal "#{Stream::TRANSCODE_CACHE_DIRECTORY}/2/ZmFrZV9tZDU=_128.mp3", stream.transcode_cache_file_path + assert_equal "#{Stream::TRANSCODE_CACHE_DIRECTORY}/2/Y0YxRW91S2cyUmtXMklNblpsUENIUT09_128.mp3", stream.transcode_cache_file_path end test "should get file duration" do diff --git a/test/system/songs_test.rb b/test/system/songs_test.rb index 8b84968c..996677f1 100644 --- a/test/system/songs_test.rb +++ b/test/system/songs_test.rb @@ -9,6 +9,7 @@ class SongsSystemTest < ApplicationSystemTestCase Song.create( name: "song_test_#{n}", file_path: Rails.root.join("test/fixtures/files/artist1_album2.mp3"), + file_path_hash: "fake_path_hash", md5_hash: "fake_md5", artist_id: artists(:artist1).id, album_id: albums(:album1).id diff --git a/test/test_helper.rb b/test/test_helper.rb index 52fdf3a4..5c0fcf8d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -81,8 +81,9 @@ def binary_data(file_path) def media_file_info_stub(file_path, attributes = {}) proc do |media_file_path| file_info = MediaFile.send(:get_tag_info, media_file_path).merge( - file_path: media_file_path, - md5_hash: MediaFile.send(:get_md5_hash, media_file_path) + file_path: media_file_path.to_s, + file_path_hash: MediaFile.get_md5_hash(media_file_path), + md5_hash: MediaFile.get_md5_hash(media_file_path, with_mtime: true) ) media_file_path.to_s == file_path.to_s ? file_info.merge(**attributes, md5_hash: "new_md5_hash") : file_info From 071f4fbd90ad5405d3cde6fae5109e1385db1cb3 Mon Sep 17 00:00:00 2001 From: aidewoode Date: Thu, 7 Jul 2022 10:07:33 +0800 Subject: [PATCH 3/3] Fix CI error --- ...ached_transcoded_stream_controller_test.rb | 2 +- test/fixtures/songs.yml | 64 ++++++++----------- test/models/stream_test.rb | 2 +- 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/test/controllers/cached_transcoded_stream_controller_test.rb b/test/controllers/cached_transcoded_stream_controller_test.rb index 47cdbaf4..19b04c2d 100644 --- a/test/controllers/cached_transcoded_stream_controller_test.rb +++ b/test/controllers/cached_transcoded_stream_controller_test.rb @@ -17,7 +17,7 @@ class CachedTranscodedStreamControllerTest < ActionDispatch::IntegrationTest test "should set header for nginx send file" do get new_cached_transcoded_stream_url(song_id: songs(:flac_sample).id) - assert_equal "/private_cache_media/2/Y0YxRW91S2cyUmtXMklNblpsUENIUT09_128.mp3", @response.get_header("X-Accel-Redirect") + assert_equal "/private_cache_media/2/ZmxhY19zYW1wbGVfbWQ1X2hhc2g=_128.mp3", @response.get_header("X-Accel-Redirect") end test "should set correct content type header" do diff --git a/test/fixtures/songs.yml b/test/fixtures/songs.yml index 3d77e670..913aa84c 100644 --- a/test/fixtures/songs.yml +++ b/test/fixtures/songs.yml @@ -1,19 +1,9 @@ -<% mp3_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist1_album2.mp3') %> -<% flac_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist1_album1.flac') %> -<% ogg_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.ogg') %> -<% wav_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.wav') %> -<% opus_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.opus') %> -<% m4a_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist1_album1.m4a') %> -<% oga_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.oga') %> -<% wma_file_path = Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.wma') %> -<% various_artists_file_path = Rails.root.join('test', 'fixtures', 'files', 'various_artists.mp3') %> - mp3_sample: id: 1 name: 'mp3_sample' - file_path: <%= mp3_file_path %> - file_path_hash: <%= MediaFile.get_md5_hash(mp3_file_path) %> - md5_hash: <%= MediaFile.get_md5_hash(mp3_file_path, with_mtime: true)%> + file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist1_album2.mp3') %> + file_path_hash: 'mp3_sample_file_path_hash' + md5_hash: 'mp3_sample_md5_hash' artist: 'artist1' album: 'album2' duration: 8.0 @@ -21,9 +11,9 @@ mp3_sample: flac_sample: id: 2 name: 'flac_sample' - file_path: <%= flac_file_path %> - file_path_hash: <%= MediaFile.get_md5_hash(flac_file_path) %> - md5_hash: <%= MediaFile.get_md5_hash(flac_file_path, with_mtime: true) %> + file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist1_album1.flac') %> + file_path_hash: 'flac_sample_file_path_hash' + md5_hash: 'flac_sample_md5_hash' artist: 'artist1' album: 'album1' duration: 8.0 @@ -31,9 +21,9 @@ flac_sample: ogg_sample: id: 3 name: 'ogg_sample' - file_path: <%= ogg_file_path %> - file_path_hash: <%= MediaFile.get_md5_hash(ogg_file_path) %> - md5_hash: <%= MediaFile.get_md5_hash(ogg_file_path, with_mtime: true) %> + file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.ogg') %> + file_path_hash: 'ogg_sample_file_path_hash' + md5_hash: 'ogg_sample_md5_hash' artist: 'artist2' album: 'album3' duration: 8.0 @@ -41,9 +31,9 @@ ogg_sample: wav_sample: id: 4 name: 'wav_sample' - file_path: <%= wav_file_path %> - file_path_hash: <%= MediaFile.get_md5_hash(wav_file_path) %> - md5_hash: <%= MediaFile.get_md5_hash(wav_file_path, with_mtime: true) %> + file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.wav') %> + file_path_hash: 'wav_sample_file_path_hash' + md5_hash: 'wav_sample_md5_hash' artist: 'artist2' album: 'album3' duration: 8.0 @@ -51,9 +41,9 @@ wav_sample: opus_sample: id: 5 name: 'opus_sample' - file_path: <%= opus_file_path %> - file_path_hash: <%= MediaFile.get_md5_hash(opus_file_path) %> - md5_hash: <%= MediaFile.get_md5_hash(opus_file_path, with_mtime: true) %> + file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.opus') %> + file_path_hash: 'opus_sample_file_path_hash' + md5_hash: 'opus_sample_md5_hash' artist: 'artist2' album: 'album3' duration: 8.0 @@ -61,9 +51,9 @@ opus_sample: m4a_sample: id: 6 name: 'm4a_sample' - file_path: <%= m4a_file_path %> - file_path_hash: <%= MediaFile.get_md5_hash(m4a_file_path) %> - md5_hash: <%= MediaFile.get_md5_hash(m4a_file_path, with_mtime: true) %> + file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist1_album1.m4a') %> + file_path_hash: 'm4a_sample_file_path_hash' + md5_hash: 'm4a_sample_md5_hash' artist: 'artist1' album: 'album1' duration: 8.0 @@ -71,9 +61,9 @@ m4a_sample: oga_sample: id: 7 name: 'oga_sample' - file_path: <%= oga_file_path %> - file_path_hash: <%= MediaFile.get_md5_hash(oga_file_path) %> - md5_hash: <%= MediaFile.get_md5_hash(oga_file_path, with_mtime: true) %> + file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.oga') %> + file_path_hash: 'oga_sample_file_path_hash' + md5_hash: 'oga_sample_md5_hash' artist: 'artist2' album: 'album3' duration: 8.0 @@ -81,9 +71,9 @@ oga_sample: wma_sample: id: 8 name: 'wma_sample' - file_path: <%= wma_file_path %> - file_path_hash: <%= MediaFile.get_md5_hash(wma_file_path) %> - md5_hash: <%= MediaFile.get_md5_hash(wma_file_path, with_mtime: true) %> + file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'artist2_album3.wma') %> + file_path_hash: 'wma_sample_file_path_hash' + md5_hash: 'wma_sample_md5_hash' artist: 'artist2' album: 'album3' duration: 8.0 @@ -91,9 +81,9 @@ wma_sample: various_artists_sample: id: 9 name: 'various_artists_sample' - file_path: <%= various_artists_file_path %> - file_path_hash: <%= MediaFile.get_md5_hash(various_artists_file_path) %> - md5_hash: <%= MediaFile.get_md5_hash(various_artists_file_path, with_mtime: true) %> + file_path: <%= Rails.root.join('test', 'fixtures', 'files', 'various_artists.mp3') %> + file_path_hash: 'various_artists_sample_file_path_hash' + md5_hash: 'various_artists_sample_md5_hash' artist: 'artist1' album: 'album4' duration: 8.0 diff --git a/test/models/stream_test.rb b/test/models/stream_test.rb index c4160270..5f06dabc 100644 --- a/test/models/stream_test.rb +++ b/test/models/stream_test.rb @@ -91,7 +91,7 @@ class StreamTest < ActiveSupport::TestCase test "should get transcode cache file path" do stream = Stream.new(songs(:flac_sample)) - assert_equal "#{Stream::TRANSCODE_CACHE_DIRECTORY}/2/Y0YxRW91S2cyUmtXMklNblpsUENIUT09_128.mp3", stream.transcode_cache_file_path + assert_equal "#{Stream::TRANSCODE_CACHE_DIRECTORY}/2/ZmxhY19zYW1wbGVfbWQ1X2hhc2g=_128.mp3", stream.transcode_cache_file_path end test "should get file duration" do