feature/share-libs
rb 733 lines 19.1 KB
Raw
1 class MyController < ApplicationController
2 before_action :must_login
3
4 def must_login
5 unless user_signed_in?
6 flash[:success] = t('need_to_logged_in_my_profile')
7 redirect_to login_path
8 end
9
10 redirect_to current_user.splitfire_profile_link unless current_user.nil?
11 end
12
13 def index
14 @user = current_user
15
16 meta = Meta
17 .select(:title, :description)
18 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
19 .last
20
21 @meta_title = meta.title if meta
22 @meta_description = meta.description if meta
23 end
24
25 def edit
26 @user = current_user
27
28 meta = Meta
29 .select(:title, :description)
30 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
31 .last
32
33 @meta_title = meta.title if meta
34 @meta_description = meta.description if meta
35 end
36
37 def add_lyric
38 if request.post?
39 @lyric = Lyric.new(lyric_params)
40 if @lyric.save
41 redirect_to my_path
42 else
43 flash[:warning] = t('lyric_cannot_be_empty')
44 redirect_to add_lyric_path(@lyric.song_id)
45 end
46
47 return
48 end
49
50 @song = Song.find_by id: params[:song_id]
51 @lyric = Lyric.new(
52 song_id: @song.id,
53 user_id: current_user.id
54 )
55
56 @user = current_user
57
58 meta = Meta
59 .select(:title, :description)
60 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
61 .last
62
63 @meta_title = meta.title if meta
64 @meta_description = meta.description if meta
65 end
66
67 def edit_lyric
68 @lyric = Lyric.find_by(id: params[:id])
69
70 if request.patch?
71 if @lyric.update_attributes(lyric_params)
72 redirect_to my_path
73 else
74 flash[:warning] = t('lyric_cannot_be_empty')
75 redirect_to add_lyric_path(@lyric.song_id)
76 end
77
78 return
79 end
80
81 @song = @lyric.song
82
83 @user = current_user
84
85 meta = Meta
86 .select(:title, :description)
87 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
88 .last
89
90 @meta_title = meta.title if meta
91 @meta_description = meta.description if meta
92 end
93
94 def lyrics
95 @lyrics = Lyric
96 .select(:id, :views_count, :song_id, :user_id)
97 .where(user_id: current_user.id, is_published: false)
98 .all
99
100 @user = current_user
101
102 meta = Meta
103 .select(:title, :description)
104 .where(home: 'profile', locale: I18n.locale)
105 .first
106
107 @meta_title = meta.title if meta
108 @meta_description = meta.description if meta
109 end
110
111 def files
112 if request.post?
113 splitfire_process
114 return
115 end
116
117 if request.delete?
118 audio_file = AudioFile.find_by(id: params[:id])
119 audio_file.destroy
120
121 flash[:success] = 'File has been successfully deleted.'
122
123 respond_to do |format|
124 format.html { redirect_to my_files_path }
125 format.xml { head :ok }
126 end
127 end
128
129 @audio_files = AudioFile
130 .where(user_id: current_user.id)
131 .all
132 .order(created_at: :desc)
133
134 @audio_file = AudioFile.new
135
136 @user = current_user
137
138 meta = Meta
139 .select(:title, :description)
140 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
141 .last
142
143 @meta_title = meta.title if meta
144 @meta_description = meta.description if meta
145 end
146
147 def file_detail
148 @audio_file = AudioFile.find_by(
149 id: params[:id],
150 user_id: current_user.id
151 )
152
153 if @audio_file.nil?
154 redirect_to my_files_path
155 return
156 end
157
158 @user = current_user
159
160 meta = Meta
161 .select(:title, :description)
162 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
163 .last
164
165 @meta_title = meta.title if meta
166 @meta_description = meta.description if meta
167 end
168
169 def admin_files
170 @audio_files = AudioFile.all.order(created_at: :desc)
171
172 meta = Meta
173 .select(:title, :description)
174 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
175 .last
176
177 @meta_title = meta.title if meta
178 @meta_description = meta.description if meta
179 end
180
181 def splitfire
182 @user = current_user
183 @audio_files = AudioFile
184 .where(user_id: @user.id)
185 .all
186 .order(created_at: :desc)
187
188 @splitfire_settings = UserSetting.find_by(user_id: @user.id)
189 if @splitfire_settings.nil?
190 @splitfire_settings = UserSetting.new(
191 user_id: @user.id,
192 model: :'2stems',
193 frequency: :'11kHz'
194 )
195 @splitfire_settings.save
196 end
197
198 meta = Meta
199 .select(:title, :description)
200 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
201 .last
202
203 @meta_title = meta.title if meta
204 @meta_description = meta.description if meta
205 end
206
207 def splitfire_settings
208 return unless request.patch?
209
210 splitfire_setting = UserSetting.find_by(user_id: current_user.id)
211 splitfire_setting.update(
212 model: params[:splitfire_setting][:model],
213 frequency: params[:splitfire_setting][:frequency]
214 )
215
216 flash[:success] = 'Splitfile settings have been updated.'
217 respond_to do |format|
218 format.html { redirect_to my_splitfire_path }
219 format.xml { head :ok }
220 end
221 end
222
223 def splitfire_detail
224 @audio_file = AudioFile.find_by(
225 id: params[:id],
226 user_id: current_user.id
227 )
228
229 if @audio_file.nil?
230 redirect_to my_splitfire_path
231 return
232 end
233
234 @user = current_user
235
236 meta = Meta
237 .select(:title, :description)
238 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
239 .last
240
241 @meta_title = meta.title if meta
242 @meta_description = meta.description if meta
243 end
244
245 def splitfire_delete
246 splitfire_result = SplitfireResult.find_by(id: params[:id])
247 audio_file_id = splitfire_result.audio_file_id
248
249 splitfire_result.destroy
250
251 # Check if no result for the given audio_file_id...
252 splitfire_results = SplitfireResult.find_by(audio_file_id: audio_file_id)
253 if splitfire_results.nil?
254 # ... then set the AudioFile as :no_results.
255 AudioFile
256 .find_by(id: audio_file_id)
257 .audio_file_split_status
258 .no_results!
259 end
260
261 respond_to do |format|
262 format.html { redirect_to my_splitfire_detail_path(audio_file_id) }
263 format.xml { head :ok }
264 end
265 end
266
267 def splitfire_cancel
268 audio_file = AudioFile.find_by(id: params[:id])
269 audio_file.audio_file_split_status.cancelled!
270
271 respond_to do |format|
272 format.html { redirect_to my_splitfire_path }
273 format.xml { head :ok }
274 end
275 end
276
277 def splitfire_start
278 audio_file_id = params[:id]
279
280 # Change the status here because we want to see the updated status right away.
281 audio_file = AudioFile.find_by(id: audio_file_id)
282 if audio_file.audio_file_split_status.nil?
283 AudioFileSplitStatus.new(
284 audio_file_id: audio_file_id,
285 status: :processing,
286 processing_progress: 5
287 ).save
288 elsif status = AudioFileSplitStatus.find_by(audio_file_id: audio_file_id)
289 status.update(
290 status: :processing,
291 processing_progress: 5
292 )
293 end
294
295 # Send to the ActiveJob
296 SplitfireJob.perform_later(audio_file_id)
297
298 flash[:success] = 'Sit down and relax. SplitFire is doing its job!'
299 redirect_to my_splitfire_path
300 end
301
302 def bass64
303 @audio_files = AudioFile
304 .where(user_id: current_user.id)
305 .all
306 .order(created_at: :desc)
307
308 @audio_file = AudioFile.new
309
310 @user = current_user
311 meta = Meta
312 .select(:title, :description)
313 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
314 .last
315
316 @meta_title = meta.title if meta
317 @meta_description = meta.description if meta
318 end
319
320 def bass64_start
321 return unless request.post?
322
323 audio_file_id = params[:id]
324
325 # Change the status here because we want to see the updated status right away.
326 audio_file = AudioFile.find_by(id: audio_file_id)
327 if audio_file.audio_file_bass64_status.nil?
328 AudioFileBass64Status.new(audio_file_id: audio_file_id, status: :processing).save
329 elsif audio_file.audio_file_bass64_status.processing!
330 end
331
332 # Send to the ActiveJob
333 Bass64Job.perform_later(audio_file_id)
334
335 flash[:success] = 'Sit down and relax. Bass64 is doing its job!'
336 redirect_to my_bass64_path
337
338 nil
339 end
340
341 def bass64_cancel
342 return unless request.patch?
343
344 audio_file_id = params[:id]
345 audio_file = AudioFile.find_by(id: audio_file_id)
346 audio_file.audio_file_bass64_status.cancelled!
347
348 respond_to do |format|
349 format.html { redirect_to my_bass64_path }
350 format.xml { head :ok }
351 end
352 end
353
354 def bass64_detail
355 @audio_file = AudioFile.find_by(
356 id: params[:id],
357 user_id: current_user.id
358 )
359
360 if @audio_file.nil?
361 redirect_to my_bass64_path
362 return
363 end
364
365 @user = current_user
366
367 meta = Meta
368 .select(:title, :description)
369 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
370 .last
371
372 @meta_title = meta.title if meta
373 @meta_description = meta.description if meta
374 end
375
376 def bass64_delete
377 audio_file = BassBackingTrack.find_by(id: params[:id])
378 audio_file.destroy
379
380 # Check if no result for the given audio_file_id...
381 audio_file_results = BassBackingTrack.find_by(audio_file_id: audio_file.audio_file_id)
382 if audio_file_results.nil?
383 # ... then set the AudioFile as :no_results.
384 split_audio_file = AudioFile.find_by(id: audio_file.audio_file_id)
385 split_audio_file.audio_file_bass64_status.no_results!
386 end
387
388 respond_to do |format|
389 format.html { redirect_to my_bass64_path }
390 format.xml { head :ok }
391 end
392 end
393
394 def karokowe
395 @audio_files = current_user.audio_files.order(created_at: :desc)
396 @user = current_user
397
398 meta = Meta
399 .select(:title, :description)
400 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
401 .last
402
403 @meta_title = meta.title if meta
404 @meta_description = meta.description if meta
405 end
406
407 def karokowe_start
408 return unless request.post?
409
410 audio_file_id = params[:id]
411
412 # Change the status here because we want to see the updated status right away.
413 audio_file = AudioFile.find_by(id: audio_file_id)
414 if audio_file.audio_file_karokowe_status.nil?
415 AudioFileKarokoweStatus.new(audio_file_id: audio_file_id, status: :processing).save
416 elsif audio_file.audio_file_karokowe_status.processing!
417 end
418
419 # Send to the ActiveJob
420 KarokoweJob.perform_later(audio_file_id)
421
422 flash[:success] = 'Sit down and relax. We are generating the karaoke file for you!'
423 redirect_to my_karokowe_path
424
425 nil
426 end
427
428 def karokowe_cancel
429 return unless request.patch?
430
431 audio_file_id = params[:id]
432 audio_file = AudioFile.find_by(id: audio_file_id)
433 audio_file.audio_file_karokowe_status.cancelled!
434
435 respond_to do |format|
436 format.html { redirect_to my_karokowe_path }
437 format.xml { head :ok }
438 end
439 end
440
441 def karokowe_detail
442 @audio_file = AudioFile.find_by(
443 id: params[:id],
444 user_id: current_user.id
445 )
446
447 if @audio_file.nil?
448 redirect_to my_karokowe_path
449 return
450 end
451
452 @user = current_user
453
454 meta = Meta
455 .select(:title, :description)
456 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
457 .last
458
459 @meta_title = meta.title if meta
460 @meta_description = meta.description if meta
461 end
462
463 def karokowe_delete
464 audio_file = KaraokeFile.find_by(id: params[:id])
465 audio_file.destroy
466
467 # Check if no result for the given audio_file_id...
468 audio_file_results = KaraokeFile.find_by(audio_file_id: audio_file.audio_file_id)
469 if audio_file_results.nil?
470 # ... then set the AudioFile as :no_results.
471 split_audio_file = AudioFile.find_by(id: audio_file.audio_file_id)
472 split_audio_file.audio_file_karokowe_status.no_results!
473 end
474
475 respond_to do |format|
476 format.html { redirect_to my_karokowe_path }
477 format.xml { head :ok }
478 end
479 end
480
481 def bonzo
482 @audio_files = AudioFile
483 .where(user_id: current_user.id)
484 .all
485 .order(created_at: :desc)
486
487 @audio_file = AudioFile.new
488
489 @user = current_user
490 meta = Meta
491 .select(:title, :description)
492 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
493 .last
494
495 @meta_title = meta.title if meta
496 @meta_description = meta.description if meta
497 end
498
499 def bonzo_start
500 return unless request.post?
501
502 audio_file_id = params[:id]
503
504 # Change the status here because we want to see the updated status right away.
505 audio_file = AudioFile.find_by(
506 id: audio_file_id,
507 user_id: current_user.id
508 )
509 if audio_file.audio_file_bonzo_status.nil?
510 AudioFileBonzoStatus.new(
511 audio_file_id: audio_file_id,
512 status: :processing
513 ).save
514 elsif audio_file.audio_file_bonzo_status.processing!
515 end
516
517 # Send to the ActiveJob
518 BonzoJob.perform_later(audio_file_id)
519
520 flash[:success] = 'Sit down and relax. We are generating the drum backing track file for you!'
521 redirect_to my_bonzo_path
522
523 nil
524 end
525
526 def bonzo_detail
527 @audio_file = AudioFile.find_by(
528 id: params[:id],
529 user_id: current_user.id
530 )
531
532 if @audio_file.nil?
533 redirect_to my_bonzo_path
534 return
535 end
536
537 @user = current_user
538
539 meta = Meta
540 .select(:title, :description)
541 .where(home: 'profile', locale: [I18n.locale, I18n.default_locale])
542 .last
543
544 @meta_title = meta.title if meta
545 @meta_description = meta.description if meta
546 end
547
548 def bonzo_delete
549 audio_file = DrumBackingTrack.find_by(
550 id: params[:id]
551 )
552 audio_file.destroy
553
554 # Check if no result for the given audio_file_id...
555 audio_file_results = DrumBackingTrack.find_by(audio_file_id: audio_file.audio_file_id)
556 if audio_file_results.nil?
557 # ... then set the AudioFile as :no_results.
558 audio_file = AudioFile.find_by(id: audio_file.audio_file_id)
559 audio_file.audio_file_bonzo_status.no_results!
560 end
561
562 respond_to do |format|
563 format.html { redirect_to my_bonzo_path }
564 format.xml { head :ok }
565 end
566 end
567
568 private
569
570 def lyric_params
571 params
572 .require(:lyric)
573 .permit(:song_id, :lyric, :locale, :user_id)
574 end
575
576 def audio_file_params
577 params
578 .require(:audio_file)
579 .permit(:user_id, :source_file, :youtube_video_url, :is_public)
580 end
581
582 def splitfire_process
583 if request.post?
584
585 # A series of validation...
586
587 # ... both are empty
588 if params[:audio_file][:source_file].nil? && params[:audio_file][:youtube_video_url].empty?
589 flash[:danger] = "Jeez, it's 2021. Please use YouTube video link or use your own file."
590 redirect_to my_files_path
591 return
592 end
593
594 # ... both are not empty
595 if !params[:audio_file][:source_file].nil? && !params[:audio_file][:youtube_video_url].empty?
596 flash[:danger] = "OMG, don't be greedy. Please use YouTube video link OR use your own file."
597 redirect_to my_files_path
598 return
599 end
600
601 # ... YouTube link
602 if !params[:audio_file][:youtube_video_url].empty? && !valid_youtube_link?(params[:audio_file][:youtube_video_url])
603 flash[:danger] = "Jeez, you're so stubborn. Please use a valid YouTube link."
604 redirect_to my_files_path
605 return
606 end
607
608 # ... audio file
609 if params[:audio_file][:youtube_video_url].empty? && params[:audio_file][:source_file].nil?
610 flash[:danger] = "Really, what are you trying to do? Where's the file?"
611 redirect_to my_files_path
612 return
613 end
614
615 # ... happy path
616
617 # ... but still paranoid
618 if !params[:audio_file][:youtube_video_url].empty? && valid_youtube_link?(params[:audio_file][:youtube_video_url])
619
620 youtube_video_url_and_id = get_clean_youtube_url_and_video_id(params[:audio_file][:youtube_video_url])
621
622 if youtube_video_url_and_id.nil?
623 flash[:danger] = 'The URL looks strange.'
624 redirect_to my_files_path
625 end
626
627 length_string = `yt-dlp --get-duration #{youtube_video_url_and_id.first}`
628 length_strings = length_string.split(':')
629
630 if length_strings.count > 1 && length_strings.first.to_i >= 5
631 flash[:danger] = 'Less than 5 minutes, please. Live is too short. This video is too long.'
632 redirect_to my_files_path
633 return
634 end
635
636 # TODO: Check if we've already have this video id
637 unless AudioFile.find_by(youtube_video_id: youtube_video_url_and_id.last).nil?
638 flash[:warning] = "We've done this. Give us new link."
639 redirect_to my_files_path
640 return
641 end
642
643 audio_file = AudioFile.new(audio_file_params)
644 audio_file.assign_attributes(youtube_video_id: youtube_video_url_and_id.last)
645
646 if audio_file.save
647
648 AudioFileSplitStatus.new(
649 audio_file_id: audio_file.id,
650 status: :downloading,
651 processing_progress: 3
652 ).save
653
654 YoutubeToAudioJob.perform_later(audio_file.id)
655
656 flash[:success] = 'SplitFire is processing this YouTube video for you.'
657
658 return redirect_to my_splitfire_path
659 else
660 flash[:success] = 'I dont know what is happening. Sorry.'
661
662 return redirect_to my_files_path
663 end
664 end
665
666 # ... audio file
667 unless params[:audio_file][:source_file].nil?
668
669 audio_file = AudioFile.new(audio_file_params)
670
671 if audio_file.save
672
673 AudioFileSplitStatus.new(
674 audio_file_id: audio_file.id,
675 status: :processing,
676 processing_progress: 5
677 ).save
678
679 SplitfireDemoJob.perform_later(audio_file.id)
680
681 flash[:success] = 'File has been successfully uploaded. SplitFire is doing its job.'
682 redirect_to my_splitfire_path
683 return
684 else
685 flash[:success] = 'I dont know what is happening. Sorry.'
686 redirect_to my_files_path
687 return
688 end
689 end
690
691 end
692
693 @audio_file = AudioFile.new
694 @user = User.find_by_id(69) # Splitfire
695 end
696
697 def valid_youtube_link?(youtube_video_url)
698 uri = URI.parse(youtube_video_url)
699
700 # To make things simple, we only recognize youtube.com and youtu.be
701 if !uri.host.nil? && (uri.host.include?('youtube.com') || uri.host.include?('youtu.be'))
702 return youtube_video_url =~ %r{^https*://[www.]*youtu[.]{1}*be[.com]{4}*/[watch?v=]*([a-zA-Z0-9_-]*)}
703 end
704
705 false
706 end
707
708 # Return set
709 def get_clean_youtube_url_and_video_id(youtube_video_url)
710 uri = URI.parse(youtube_video_url)
711
712 if uri.host.include?('youtube.com')
713
714 split_query = uri.query.split('&')
715
716 query_video_id = split_query.find { |item| item.include?('v=') }
717
718 uri.query = query_video_id
719 video_id = query_video_id.split('v=').last
720 set = [uri.to_s, video_id]
721 return set
722
723 end
724
725 if uri.host.include?('youtu.be')
726 video_id = uri.to_s.split('/').last
727 set = [uri.to_s, video_id]
728 return set
729 end
730
731 nil
732 end
733 end