feature/share-libs
rb 170 lines 4.34 KB
Raw
1 # Fix this later
2 # rubocop:disable Metrics/ClassLength
3 class AudioFile < ApplicationRecord
4 enum status: %i[
5 downloading
6 splitting
7 done
8 ]
9
10 belongs_to :song_provider
11
12 has_many :splitfire_results, dependent: :destroy
13 has_one :bass_backing_track, dependent: :destroy # Deprecated
14 has_one :karaoke_file, dependent: :destroy
15
16 has_one :bass_backing_track_file, dependent: :destroy
17 has_one :guitar_backing_track_file, dependent: :destroy
18 has_one :drum_backing_track, dependent: :destroy
19
20 # Internal use
21 has_many :vocal_file, dependent: :nullify
22 has_many :bass_file, dependent: :nullify
23 has_many :drum_file, dependent: :nullify
24 has_many :other_file, dependent: :nullify
25 # END
26
27 has_many :comments, as: :commentable
28
29 # Split processig status
30 has_one :audio_file_split_status, dependent: :destroy
31 # END
32
33 # Bass64 processig status
34 has_one :audio_file_bass64_status, dependent: :destroy
35 # END
36
37 # KaroKowe processig status
38 has_one :audio_file_karokowe_status, dependent: :destroy
39 # END
40
41 # KaroKowe processig status
42 has_one :audio_file_bonzo_status, dependent: :destroy
43 # END
44
45 # ActiveStorage attachments
46 has_one_attached :source_file
47
48 def is_valid_row?
49 source_file.attached? || (!youtube_video_url.nil? && !youtube_video_id.nil?)
50 end
51
52 def is_in_progress?
53 !audio_file_split_status.nil? && (audio_file_split_status.processing? || audio_file_split_status.youtube_converting?)
54 end
55
56 def should_show_result_content?
57 audio_file_split_status.youtube_converting? || splitfire_results.count.positive?
58 end
59
60 def output_filename
61 output = ''
62 output = '<i class="icon-youtube"></i> ' if !youtube_video_url.nil? && !youtube_video_id.nil?
63
64 if audio_file_split_status.youtube_converting?
65 output += 'Converting YouTube video to mp3'
66 elsif source_file.attached?
67 output += source_file.blob.filename.to_s
68 end
69
70 output
71 end
72
73 # ActiveAdmin title. See: https://stackoverflow.com/a/8429960/1137814
74 def name
75 song_provider&.name
76 end
77
78 def youtube_thumbnail
79 "https://img.youtube.com/vi/#{song_provider.provider_id}/mqdefault.jpg"
80 end
81
82 def splitfire_link
83 "https://#{host}/@#{user.username_or_id}/#{source_file.filename.to_s.downcase.parameterize}-#{id}"
84 end
85
86 def splitfire_karaoke_link
87 "https://#{host}/@#{user.username_or_id}/#{source_file.filename.to_s.downcase.parameterize}-#{id}/karaoke-version"
88 end
89
90 def splitfire_guitar_backing_track_link
91 "https://#{host}/@#{user.username_or_id}/#{source_file.filename.to_s.downcase.parameterize}-#{id}/guitar-backing-track-version"
92 end
93
94 def splitfire_bass_backing_track_link
95 "https://#{host}/@#{user.username_or_id}/#{source_file.filename.to_s.downcase.parameterize}-#{id}/bass-backing-track-version"
96 end
97
98 def splitfire_drum_backing_track_link
99 "https://#{host}/@#{user.username_or_id}/#{source_file.filename.to_s.downcase.parameterize}-#{id}/drum-backing-track-version"
100 end
101
102 def backing_track_slug
103 "#{source_file.filename.to_s.downcase.parameterize}-#{id}"
104 end
105
106 def karaoke_title
107 I18n.t('song_karaoke_title', filename: source_file.filename)
108 end
109
110 def guitar_backing_track_title
111 "#{source_file.filename} Guitar Backing Track" # TODO: Localize
112 end
113
114 def bass_backing_track_title
115 "#{source_file.filename} Bass Backing Track" # TODO: Localize
116 end
117
118 def drum_backing_track_title
119 "#{source_file.filename} Drum Backing Track" # TODO: Localize
120 end
121
122 def bass_file
123 splitfire_results.find do |result|
124 result.source_file.filename.to_s.start_with?('bass')
125 end
126 end
127
128 def vocals_file
129 splitfire_results.find do |result|
130 result.source_file.filename.to_s.start_with?('vocals')
131 end
132 end
133
134 def drums_file
135 splitfire_results.find do |result|
136 result.source_file.filename.to_s.start_with?('drums')
137 end
138 end
139
140 def other_file
141 splitfire_results.find do |result|
142 result.source_file.filename.to_s.start_with?('other')
143 end
144 end
145
146 def piano_file
147 splitfire_results.find do |result|
148 result.source_file.filename.to_s.start_with?('piano')
149 end
150 end
151
152 def attached?
153 source_file.attached?
154 end
155
156 def as_json(options = {})
157 options[:methods] = [:name]
158 super
159 end
160
161 def as_json_packed
162 as_json(only: %i[id status])
163 end
164
165 private
166
167 def host
168 Rails.env.production? ? 'splitfire.ai' : 'localhost:3002'
169 end
170 end