Skip to content

Commit

Permalink
Copy media after save and reset
Browse files Browse the repository at this point in the history
  • Loading branch information
svevang committed Apr 25, 2023
1 parent d71cd27 commit f4052eb
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 14 deletions.
15 changes: 12 additions & 3 deletions app/models/episode_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def import
update!(status: AUDIO_SAVED)

create_or_update_episode!
# Set the feeder audio
episode.contents = audio_content_params
episode.image = image_contents_params

set_file_resources!

update!(status: EPISODE_SAVED)

episode.save!
Expand Down Expand Up @@ -101,6 +101,15 @@ def set_audio_metadata!
update!(audio: audio_files)
end

def set_file_resources!
episode.contents = audio_content_params
episode.image = image_contents_params
episode.save!
episode.images.reset
episode.contents.reset
episode.copy_media
end

def entry_audio_files(entry)
if config[:audio] && config[:audio][entry[:entry_id]]
{files: (config[:audio][entry[:entry_id]] || [])}
Expand Down
9 changes: 7 additions & 2 deletions app/models/podcast_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,12 @@ def feed_description(feed)
end

def update_images(feed)
podcast.default_feed.itunes_image = feed.itunes_image
podcast.default_feed.feed_image = feed.image
default_feed = podcast.default_feed

default_feed.itunes_image = feed.itunes_image if feed.itunes_image.present?
default_feed.feed_image = feed.image.url if feed.image.present?

default_feed.save!
end

attr_writer :distribution
Expand Down Expand Up @@ -271,6 +275,7 @@ def create_or_update_podcast!
update!(podcast: podcast)

update_images(feed)
podcast.copy_media

podcast
end
Expand Down
3 changes: 3 additions & 0 deletions test/factories/feed_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
after(:build) do |feed, _evaluator|
feed.feed_image = build(:feed_image)
feed.itunes_image = build(:itunes_image)

feed.feed_image.tasks.build
feed.itunes_image.tasks.build
end
end

Expand Down
Binary file added test/fixtures/99-300.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/models/concerns/porter_encoder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class TestEncoder
end

around do |test|
sns.reset
TestEncoder.stub :new_porter_sns_client, sns do
test.call
end
Expand Down
21 changes: 21 additions & 0 deletions test/models/episode_import_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
)
end

let(:sns) { SnsMock.new }

around do |test|
sns.reset
Task.stub :new_porter_sns_client, sns do
test.call
end
end

before do
stub_episode_requests
end
Expand Down Expand Up @@ -63,6 +72,18 @@
_(f.contents.first.status).must_equal "created"

_(f.images.count).must_equal 1

_(sns.messages.count).must_equal 2
_(sns.messages.map { |m| m["Job"]["Tasks"].length }).must_equal [2, 1]
_(sns.messages.map { |m| m["Job"]["Source"] })
.must_equal([
{"Mode" => "HTTP", "URL" => "https://dts.podtrac.com/redirect.mp3/media.blubrry.com/transistor/cdn-transistor.prx.org/wp-content/uploads/Smithsonian3_Transistor.mp3"},
{"Mode" => "HTTP", "URL" => "https://cdn-transistor.prx.org/shake.jpg"}
])

importer.reload
_(f.image.status).must_equal "created"
_(f.contents.map(&:status)).must_equal ["created"]
end

it "creates correctly for libsyn entries" do
Expand Down
33 changes: 25 additions & 8 deletions test/models/podcast_import_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
let(:podcast_url) { "http://feeds.prx.org/transistor_stem" }
let(:podcast) { create(:podcast) }
let(:importer) { PodcastImport.create(podcast: podcast, account_id: account_id, url: podcast_url) }
let(:sns) { SnsMock.new }

around do |test|
sns.reset
Task.stub :new_porter_sns_client, sns do
test.call
end
end

before do
stub_requests
Expand Down Expand Up @@ -45,14 +53,17 @@
"by radio and podcast powerhouse PRX, with support " \
"from the Sloan Foundation."

# TODO
# _(Portered.sns_client.messages.count).must_equal 2
# _(Portered.sns_client.messages[0]["Job"]["Id"]).must_equal images[0].to_global_id.to_s
# _(Portered.sns_client.messages[1]["Job"]["Id"]).must_equal images[1].to_global_id.to_s

# images must be processing
# images = importer.podcast.images
# images.count.must_equal 2
_(sns.messages.count).must_equal 2
_(sns.messages.map { |m| m["Job"]["Tasks"].length }).must_equal [1, 1]
_(sns.messages.map { |m| m["Job"]["Source"] })
.must_equal [
{"Mode" => "HTTP", "URL" => "http://cdn-transistor.prx.org/transistor300.png"},
{"Mode" => "HTTP", "URL" => "https://cdn-transistor.prx.org/transistor1400.jpg"}
]

importer.reload
_(importer.podcast.itunes_image.status).must_equal "created"
_(importer.podcast.feed_image.status).must_equal "created"
end

it "creates podcast episode imports using a config" do
Expand Down Expand Up @@ -373,4 +384,10 @@ def stub_requests

stub_request(:get, "https://cdn-transistor.prx.org/shake.jpg")
.to_return(status: 200, body: test_file("/fixtures/transistor1400.jpg"), headers: {})

stub_request(:get, "http://cdn-transistor.prx.org/transistor300.png")
.to_return(status: 200, body: test_file("/fixtures/transistor300.png"), headers: {})

stub_request(:get, "https://f.prxu.org/99pi/images/42384e27-3dd6-497f-991f-67fabb7e6e5b/99-300.png")
.to_return(status: 200, body: test_file("/fixtures/99-300.png"), headers: {})
end
8 changes: 7 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,20 @@ def stub_requests_to_prx_cms
end

class SnsMock
attr_accessor :message
attr_accessor :message, :messages

def publish(params)
self.message = JSON.parse(params[:message]).with_indifferent_access
messages << message

{
message_id: "whatever"
}
end

def reset
@messages = []
end
end

class StubToken < PrxAuth::Rails::Token
Expand Down

0 comments on commit f4052eb

Please sign in to comment.