-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from blackcandy-org/api
Move stream controller to API
- Loading branch information
Showing
11 changed files
with
159 additions
and
147 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
app/controllers/api/v1/cached_transcoded_stream_controller.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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
class CachedTranscodedStreamController < StreamController | ||
def new | ||
send_local_file @stream.transcode_cache_file_path | ||
end | ||
|
||
private | ||
|
||
def set_nginx_header | ||
response.headers["X-Accel-Redirect"] = File.join("/private_cache_media", @stream.transcode_cache_file_path.sub(Stream::TRANSCODE_CACHE_DIRECTORY.to_s, "")) | ||
end | ||
end | ||
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
class StreamController < ApiController | ||
before_action :find_stream | ||
|
||
def new | ||
if need_transcode? @stream.format | ||
redirect_to new_api_v1_transcoded_stream_path(song_id: params[:song_id]) | ||
else | ||
send_local_file @stream.file_path | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_nginx_header | ||
# Let nginx can get value of media_path dynamically in the nginx config, | ||
# when use X-Accel-Redirect header to send file. | ||
response.headers["X-Media-Path"] = Setting.media_path | ||
response.headers["X-Accel-Redirect"] = File.join("/private_media", @stream.file_path.sub(File.expand_path(Setting.media_path), "")) | ||
end | ||
|
||
def find_stream | ||
song = Song.find(params[:song_id]) | ||
@stream = Stream.new(song) | ||
end | ||
|
||
def nginx_senfile? | ||
Rails.configuration.action_dispatch.x_sendfile_header == "X-Accel-Redirect" | ||
end | ||
|
||
def send_local_file(file_path) | ||
if nginx_senfile? | ||
set_nginx_header | ||
|
||
send_file file_path | ||
return | ||
end | ||
|
||
# Use Rack::File to support HTTP range without nginx. see https://github.com/rails/rails/issues/32193 | ||
Rack::File.new(nil).serving(request, file_path).tap do |(status, headers, body)| | ||
self.status = status | ||
self.response_body = body | ||
|
||
headers.each { |name, value| response.headers[name] = value } | ||
|
||
response.headers["Content-Type"] = Mime[@stream.format] | ||
response.headers["Content-Disposition"] = "attachment" | ||
end | ||
end | ||
end | ||
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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
class TranscodedStreamController < StreamController | ||
include ActionController::Live | ||
|
||
before_action :find_cache | ||
|
||
# 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 | ||
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_cache | ||
if valid_cache? | ||
redirect_to new_api_v1_cached_transcoded_stream_path(song_id: params[:song_id]) | ||
end | ||
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 | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.