-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove web session authentication in API and share code relative to t…
…he stream controllers in concerns.
- Loading branch information
Showing
22 changed files
with
200 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/controllers/concerns/playable.rb → app/controllers/concerns/playable_concern.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Playable | ||
module PlayableConcern | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module StreamConcern | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :find_stream | ||
end | ||
|
||
def new | ||
send_local_file @stream.file_path, @stream.format, nginx_headers: { | ||
# Let nginx can get value of media_path dynamically in the nginx config | ||
# when use X-Accel-Redirect header to send file. | ||
"X-Media-Path" => Setting.media_path, | ||
"X-Accel-Redirect" => File.join("/private_media", @stream.file_path.sub(File.expand_path(Setting.media_path), "")) | ||
} | ||
end | ||
|
||
private | ||
|
||
def find_stream | ||
@stream = Stream.new(Song.find(params[:song_id])) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module TranscodedStreamConcern | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :find_stream | ||
before_action :find_cache | ||
end | ||
|
||
# Similar to send_file in rails, but let response_body to be a stream object. | ||
# The instance of Stream can respond to each() method. So the download can be streamed, | ||
# instead of read whole data into memory. | ||
def new | ||
# Because the module ActionController::Live contains methods that can block send file. | ||
# So can't simply include ActionController::Live in class, otherwise include this module only on this method. | ||
self.class.send(:include, ActionController::Live) | ||
|
||
response.headers["Content-Type"] = Mime[Stream::TRANSCODE_FORMAT] | ||
|
||
send_stream(filename: "#{@stream.name}.mp3") do |stream_response| | ||
File.open(@stream.transcode_cache_file_path, "w") do |file| | ||
@stream.each do |data| | ||
stream_response.write data | ||
file.write data | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def find_stream | ||
@stream = Stream.new(Song.find(params[:song_id])) | ||
end | ||
|
||
def find_cache | ||
return unless valid_cache? | ||
send_local_file @stream.transcode_cache_file_path, @stream.format, nginx_headers: { | ||
"X-Accel-Redirect" => File.join("/private_cache_media", @stream.transcode_cache_file_path.sub(Stream::TRANSCODE_CACHE_DIRECTORY.to_s, "")) | ||
} | ||
end | ||
|
||
def valid_cache? | ||
return unless File.exist?(@stream.transcode_cache_file_path) | ||
|
||
# Compare duration of cache file and original file to check integrity of cache file. | ||
# Because the different format of the file, the duration will have a little difference, | ||
# so the duration difference in two seconds are considered no problem. | ||
cache_file_tag = WahWah.open(@stream.transcode_cache_file_path) | ||
(@stream.duration - cache_file_tag.duration).abs <= 2 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class StreamController < ApplicationController | ||
include StreamConcern | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class TranscodedStreamController < ApplicationController | ||
include TranscodedStreamConcern | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
json.merge! song_json_builder(song) | ||
json.merge! song_json_builder(song, for_api: true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.