Skip to content

A platform for community discussion. Free, open, simple.

DEV: Update mediaconvert to use a subdirectory for output (#36049)

Blake Erickson390cfb782032
diff --git a/app/jobs/regular/check_video_conversion_status.rb b/app/jobs/regular/check_video_conversion_status.rb
index 0eecb7f70de..2caf7a3cfa2 100644
--- a/app/jobs/regular/check_video_conversion_status.rb
+++ b/app/jobs/regular/check_video_conversion_status.rb
@@ -16,7 +16,7 @@ module Jobs
case status
when :complete
- if adapter.handle_completion(args[:job_id], args[:output_path], args[:new_sha1])
+ if adapter.handle_completion(args[:job_id], args[:new_sha1])
# Successfully completed
Rails.logger.info(
"Completed video conversion for upload ID #{upload.id} and job ID #{args[:job_id]}",
diff --git a/app/services/video_conversion/aws_media_convert_adapter.rb b/app/services/video_conversion/aws_media_convert_adapter.rb
index 08514eaeb1e..32de2f55217 100644
--- a/app/services/video_conversion/aws_media_convert_adapter.rb
+++ b/app/services/video_conversion/aws_media_convert_adapter.rb
@@ -11,11 +11,6 @@ module VideoConversion
begin
new_sha1 = SecureRandom.hex(20)
- # Use FileStore::BaseStore logic to generate the path
- # Create a temporary upload object to leverage the path generation logic
- temp_upload = build_temp_upload_for_path_generation(new_sha1)
- output_path = Discourse.store.get_path_for_upload(temp_upload).sub(/\.mp4$/, "")
-
# Extract the path from the URL
# The URL format is: //bucket.s3.dualstack.region.amazonaws.com/path/to/file
# or: //bucket.s3.region.amazonaws.com/path/to/file
@@ -32,7 +27,8 @@ module VideoConversion
end
input_path = "s3://#{s3_upload_bucket}/#{path}"
- settings = build_conversion_settings(input_path, output_path)
+
+ settings = build_conversion_settings(input_path, new_sha1)
begin
response =
@@ -43,7 +39,6 @@ module VideoConversion
user_metadata: {
"upload_id" => @upload.id.to_s,
"new_sha1" => new_sha1,
- "output_path" => output_path,
},
)
@@ -54,7 +49,6 @@ module VideoConversion
upload_id: @upload.id,
job_id: response.job.id,
new_sha1: new_sha1,
- output_path: output_path,
original_filename: @upload.original_filename,
user_id: @upload.user_id,
adapter_type: "aws_mediaconvert",
@@ -117,21 +111,26 @@ module VideoConversion
end
end
- def handle_completion(job_id, output_path, new_sha1)
+ def handle_completion(job_id, new_sha1)
s3_store = FileStore::S3Store.new
- path = "#{output_path}.mp4"
- object = s3_store.object_from_path(path)
+ temp_path = File.join(SiteSetting.mediaconvert_output_subdirectory, "#{new_sha1}.mp4")
- return false if !object&.exists?
+ temp_object = find_temp_file(s3_store, temp_path)
+ return false if !temp_object
begin
- url = "//#{s3_store.s3_bucket}.s3.dualstack.#{SiteSetting.s3_region}.amazonaws.com/#{path}"
+ filesize = temp_object.size
+ final_path = get_final_upload_path(new_sha1)
+ destination_path, etag = copy_file_to_final_location(s3_store, temp_path, final_path)
+ return false if !destination_path
- # Set the correct ACL based on the original upload's security status
- # This ensures the optimized video has the same permissions as the original
- s3_store.update_file_access_control(path, @upload.secure?)
+ update_file_acl(s3_store, final_path, destination_path)
+ remove_temp_file(s3_store, temp_path)
- optimized_video = create_optimized_video_record(output_path, new_sha1, object.size, url)
+ url = build_file_url(s3_store, destination_path)
+ output_path = get_output_path_for_record(final_path)
+ optimized_video =
+ create_optimized_video_record(output_path, new_sha1, filesize, url, etag: etag)
if optimized_video
update_posts_with_optimized_video
@@ -147,6 +146,9 @@ module VideoConversion
env: {
upload_id: @upload.id,
job_id: job_id,
+ temp_path: temp_path,
+ error_class: e.class.name,
+ error_message: e.message,
},
)
false
@@ -159,6 +161,110 @@ module VideoConversion
SiteSetting.video_conversion_enabled && SiteSetting.mediaconvert_role_arn.present?
end
+ def find_temp_file(s3_store, temp_path)
+ temp_object = s3_store.s3_helper.object(temp_path)
+ if !temp_object.exists?
+ Rails.logger.error(
+ "MediaConvert temp file not found at #{temp_path} for upload #{@upload.id}",
+ )
+ return nil
+ end
+
+ temp_object
+ end
+
+ def get_final_upload_path(new_sha1)
+ temp_upload = build_temp_upload_for_path_generation(new_sha1)
+ Discourse.store.get_path_for_upload(temp_upload)
+ end
+
+ def copy_file_to_final_location(s3_store, temp_path, final_path)
+ s3_helper = s3_store.s3_helper
+
+ destination_path = get_s3_path(final_path)
+
+ copy_options = s3_store.default_s3_options(secure: @upload.secure?)
+
+ begin
+ destination_path, etag = s3_helper.copy(temp_path, destination_path, options: copy_options)
+
+ destination_object = s3_helper.object(destination_path)
+ if !destination_object.exists?
+ Rails.logger.error(
+ "MediaConvert copy completed but destination file not found at #{destination_path} for upload #{@upload.id}",
+ )
+ [nil, nil]
+ end
+
+ [destination_path, etag]
+ rescue Aws::S3::Errors::NotFound => e
+ Rails.logger.error(
+ "MediaConvert copy failed - source or destination not found: #{e.message} (source: #{temp_path}, destination: #{destination_path}) for upload #{@upload.id}",
+ )
+ [nil, nil]
+ rescue => e
+ Rails.logger.error(
+ "MediaConvert copy failed: #{e.class.name} - #{e.message} (source: #{temp_path}, destination: #{destination_path}) for upload #{@upload.id}",
+ )
+ raise
+ end
+ end
+
+ def update_file_acl(s3_store, final_path, destination_path)
+ # get_s3_path adds multisite prefix if needed; s3_helper will add bucket folder path
+ final_path_for_acl = get_s3_path(final_path)
+
+ begin
+ s3_store.update_file_access_control(final_path_for_acl, @upload.secure?)
+ rescue Aws::S3::Errors::NotFound => e
+ Rails.logger.error(
+ "MediaConvert file not found when updating access control at #{final_path_for_acl} (full path: #{destination_path}) for upload #{@upload.id}: #{e.message}",
+ )
+ raise
+ end
+ end
+
+ def remove_temp_file(s3_store, temp_path)
+ s3_helper = s3_store.s3_helper
+ begin
+ s3_helper.remove(temp_path, false)
+ rescue => e
+ # Log but don't fail if deletion fails - file will be cleaned up later
+ Rails.logger.warn("Failed to delete temporary MediaConvert file #{temp_path}: #{e.message}")
+ end
+ end
+
+ def build_file_url(s3_store, destination_path)
+ # Ensure path has leading slash for URL
+ path_with_slash =
+ destination_path.start_with?("/") ? destination_path : "/#{destination_path}"
+ # Use s3_store.absolute_base_url which handles dualstack, region, and custom endpoints
+ "#{s3_store.absolute_base_url}#{path_with_slash}"
+ end
+
+ def get_output_path_for_record(final_path)
+ # Remove .mp4 extension for the record path
+ get_s3_path(final_path).sub(/\.mp4$/, "")
+ end
+
+ def get_s3_path(path)
+ return path if path.blank?
+
+ if Rails.configuration.multisite
+ multisite_prefix = build_multisite_path
+ # Prevent double-prepending if path already includes the multisite prefix
+ return path if path.start_with?(multisite_prefix)
+ File.join(multisite_prefix, path)
+ else
+ path
+ end
+ end
+
+ def build_multisite_path
+ path = File.join("uploads", RailsMultisite::ConnectionManagement.current_db)
+ "#{path}/"
+ end
+
def build_temp_upload_for_path_generation(new_sha1)
# Create a temporary upload object to leverage FileStore::BaseStore path generation
# This object is only used for path generation and won't be saved to the database
@@ -227,14 +333,24 @@ module VideoConversion
end
def self.s3_upload_bucket
- SiteSetting.Upload.s3_upload_bucket
+ # MediaConvert needs just the bucket name, not the folder path
+ # If s3_upload_bucket is "bucket-name/folder", we need just "bucket-name"
+ bucket_name, _folder_path =
+ S3Helper.get_bucket_and_folder_path(SiteSetting.Upload.s3_upload_bucket)
+ bucket_name
end
def build_conversion_settings(input_path, output_path)
self.class.build_conversion_settings(input_path, output_path)
end
- def self.build_conversion_settings(input_path, output_path)
+ def self.build_conversion_settings(input_path, temp_output_filename)
+ # temp_output_filename is just the filename without extension (e.g., "new_sha1")
+ # MediaConvert will automatically add .mp4 extension based on container type
+ # We write it to the subdirectory as a temporary location
+ subdirectory = SiteSetting.mediaconvert_output_subdirectory
+ destination_path = File.join(subdirectory, temp_output_filename)
+
{
timecode_config: {
source: "ZEROBASED",
@@ -245,7 +361,7 @@ module VideoConversion
output_group_settings: {
type: "FILE_GROUP_SETTINGS",
file_group_settings: {
- destination: "s3://#{s3_upload_bucket}/#{output_path}",
+ destination: "s3://#{s3_upload_bucket}/#{destination_path}",
},
},
outputs: [
diff --git a/app/services/video_conversion/base_adapter.rb b/app/services/video_conversion/base_adapter.rb
index 5b9e14a7399..03759aee438 100644
--- a/app/services/video_conversion/base_adapter.rb
+++ b/app/services/video_conversion/base_adapter.rb
@@ -23,22 +23,27 @@ module VideoConversion
# Handles the completion of a successful conversion
# This is called by the job system when status is :complete
- def handle_completion(job_id, output_path, new_sha1)
+ def handle_completion(job_id, new_sha1)
raise NotImplementedError, "#{self.class} must implement #handle_completion"
end
protected
- def create_optimized_video_record(output_path, new_sha1, filesize, url)
- OptimizedVideo.create_for(
- @upload,
- @upload.original_filename.sub(/\.[^.]+$/, "_converted.mp4"),
- @upload.user_id,
+ def create_optimized_video_record(output_path, new_sha1, filesize, url, etag: nil)
+ options = {
filesize: filesize,
sha1: new_sha1,
url: url,
extension: "mp4",
adapter: adapter_name,
+ }
+ options[:etag] = etag if etag.present?
+
+ OptimizedVideo.create_for(
+ @upload,
+ @upload.original_filename.sub(/\.[^.]+$/, "_converted.mp4"),
+ @upload.user_id,
+ **options,
)
end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 1e4089751b6..97de5b8b683 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2142,6 +2142,7 @@ en:
video_conversion_enabled: "Enable video conversion for uploaded video files. This allows videos to be converted to web-compatible formats."
video_conversion_service: "The video conversion service to use for processing uploaded videos."
mediaconvert_role_arn: "AWS IAM role ARN for MediaConvert service. Required when using AWS MediaConvert for video conversion."
+ mediaconvert_output_subdirectory: "Subdirectory in S3 bucket where MediaConvert will temporarily write converted videos before they are moved to the final upload location."
min_ratio_to_crop: "Ratio used to crop tall images. Enter the result of width / height."
@@ -2848,6 +2849,7 @@ en:
content_localization_locale_limit: "The number of supported locales cannot exceed %{max}."
content_localization_language_switcher_requirements: "The language switcher requires the 'set locale from cookie' and 'allow user locale' site settings to be enabled, and 'content localization supported locales' to have at least one language."
mediaconvert_role_arn_required: "AWS IAM role ARN is required when using AWS MediaConvert for video conversion."
+ mediaconvert_output_subdirectory_required: "MediaConvert output subdirectory is required."
s3_credentials_required_for_video_conversion: "S3 credentials are required for video conversion. Please configure S3 access keys or enable IAM profile."
keywords:
diff --git a/config/site_settings.yml b/config/site_settings.yml
index f8c1b55fb8a..410cc683f77 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -2391,6 +2391,9 @@ files:
mediaconvert_endpoint:
default: ""
hidden: true
+ mediaconvert_output_subdirectory:
+ default: "transcoded"
+ validator: "MediaconvertOutputSubdirectoryValidator"
trust:
default_trust_level:
diff --git a/lib/validators/mediaconvert_output_subdirectory_validator.rb b/lib/validators/mediaconvert_output_subdirectory_validator.rb
new file mode 100644
index 00000000000..4b4858ba853
--- /dev/null
+++ b/lib/validators/mediaconvert_output_subdirectory_validator.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+class MediaconvertOutputSubdirectoryValidator
+ def initialize(opts = {})
+ @opts = opts
+ end
+
+ def valid_value?(val)
+ # Value must be present (not blank)
+ val.present?
+ end
+
+ def error_message
+ I18n.t("site_settings.errors.mediaconvert_output_subdirectory_required")
+ end
+end
diff --git a/spec/lib/validators/mediaconvert_output_subdirectory_validator_spec.rb b/spec/lib/validators/mediaconvert_output_subdirectory_validator_spec.rb
new file mode 100644
index 00000000000..3c0b8979a37
--- /dev/null
+++ b/spec/lib/validators/mediaconvert_output_subdirectory_validator_spec.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe MediaconvertOutputSubdirectoryValidator do
+ subject(:validator) { described_class.new }
+
+ describe "#valid_value?" do
+ it "returns true when value is present" do
+ expect(validator.valid_value?("transcoded")).to be true
+ expect(validator.valid_value?("converted-videos")).to be true
+ end
+
+ it "returns false when value is blank" do
+ expect(validator.valid_value?("")).to be false
+ expect(validator.valid_value?(nil)).to be false
+ end
+ end
+
+ describe "#error_message" do
+ it "returns the correct error message" do
+ expect(validator.error_message).to eq(
+ I18n.t("site_settings.errors.mediaconvert_output_subdirectory_required"),
+ )
+ end
+ end
+end
diff --git a/spec/services/video_conversion/aws_media_convert_adapter_spec.rb b/spec/services/video_conversion/aws_media_convert_adapter_spec.rb
index 6a8211e8d8f..355e4f6a15f 100644
--- a/spec/services/video_conversion/aws_media_convert_adapter_spec.rb
+++ b/spec/services/video_conversion/aws_media_convert_adapter_spec.rb
@@ -45,6 +45,7 @@ RSpec.describe VideoConversion::AwsMediaConvertAdapter do
allow(SiteSetting).to receive(:mediaconvert_endpoint).and_return(
"https://mediaconvert.endpoint",
)
+ allow(SiteSetting).to receive(:mediaconvert_output_subdirectory).and_return("transcoded")
allow(SiteSetting.Upload).to receive(:s3_upload_bucket).and_return(s3_bucket)
allow(SiteSetting).to receive(:s3_region).and_return(s3_region)
allow(SiteSetting).to receive(:s3_access_key_id).and_return("test-key")
@@ -98,7 +99,18 @@ RSpec.describe VideoConversion::AwsMediaConvertAdapter do
it "creates a MediaConvert job and enqueues status check" do
input_path = "s3://#{s3_bucket}/uploads/default/original/test.mp4"
- expected_settings = described_class.build_conversion_settings(input_path, output_path)
+ # MediaConvert automatically adds .mp4 extension, so we pass filename without extension
+ temp_output_filename = new_sha1
+ expected_settings =
+ described_class.build_conversion_settings(input_path, temp_output_filename)
+ # Verify the destination includes the subdirectory with simple filename (no .mp4)
+ # MediaConvert will add .mp4 automatically
+ destination =
+ expected_settings[:output_groups][0][:output_group_settings][:file_group_settings][
+ :destination
+ ]
+ expected_destination_path = File.join("transcoded", temp_output_filename)
+ expect(destination).to eq("s3://#{s3_bucket}/#{expected_destination_path}")
expected_job_params = {
role: SiteSetting.mediaconvert_role_arn,
@@ -107,7 +119,6 @@ RSpec.describe VideoConversion::AwsMediaConvertAdapter do
user_metadata: {
"upload_id" => upload.id.to_s,
"new_sha1" => new_sha1,
- "output_path" => output_path,
},
}
@@ -119,7 +130,6 @@ RSpec.describe VideoConversion::AwsMediaConvertAdapter do
adapter_type: "aws_mediaconvert",
job_id: job_id,
new_sha1: new_sha1,
- output_path: output_path,
original_filename: upload.original_filename,
upload_id: upload.id,
user_id: upload.user_id,
@@ -271,103 +281,269 @@ RSpec.describe VideoConversion::AwsMediaConvertAdapter do
describe "#handle_completion" do
let(:job_id) { "job-123" }
- let(:output_path) { "optimized/videos/test-sha1" }
- let(:expected_url) do
- "//#{s3_bucket}.s3.dualstack.#{s3_region}.amazonaws.com/#{output_path}.mp4"
+ let(:temp_path) { "transcoded/#{new_sha1}.mp4" }
+ let(:final_path) { "original/1X/#{new_sha1}.mp4" }
+ let(:s3_helper) { instance_double(S3Helper) }
+ let(:s3_client) { instance_double(Aws::S3::Client) }
+ let(:s3_resource) { instance_double(Aws::S3::Resource) }
+ let(:bucket_name) { s3_bucket }
+ let(:source_bucket) { instance_double(Aws::S3::Bucket) }
+ let(:source_s3_object) { instance_double(Aws::S3::Object) }
+ let(:destination_s3_object) { instance_double(Aws::S3::Object) }
+ let(:copy_response) { instance_double(Aws::S3::Types::CopyObjectOutput) }
+ let(:copy_result) { instance_double(Aws::S3::Types::CopyObjectResult, etag: '"etag123"') }
+
+ before do
+ allow(s3_store).to receive(:s3_helper).and_return(s3_helper)
+ allow(s3_store).to receive(:default_s3_options).and_return({})
+ allow(s3_store).to receive(:absolute_base_url).and_return(
+ "//#{s3_bucket}.s3.dualstack.#{s3_region}.amazonaws.com",
+ )
+
+ # Mock s3_helper.object for find_temp_file
+ allow(s3_helper).to receive(:object).with(temp_path).and_return(source_s3_object)
+ allow(source_s3_object).to receive(:exists?).and_return(true)
+ allow(source_s3_object).to receive(:size).and_return(1024)
+
+ # Mock s3_helper.copy - it returns [destination_path, etag]
+ # The destination path will have multisite prefix if in multisite mode
+ # Use a flexible matcher since the exact path format depends on multisite configuration
+ allow(s3_helper).to receive(:copy) do |source, dest, options: {}|
+ if source == temp_path && dest.include?(final_path)
+ # Return the destination path as-is (it may have multisite prefix)
+ [dest, "etag123"]
+ else
+ raise "Unexpected copy call: source=#{source}, dest=#{dest}"
+ end
+ end
+
+ # Mock s3_helper.object for destination verification (flexible path matching)
+ allow(s3_helper).to receive(:object) do |path|
+ if path == temp_path
+ source_s3_object
+ elsif path.include?(final_path)
+ destination_s3_object
+ else
+ source_s3_object
+ end
+ end
+ allow(destination_s3_object).to receive(:exists?).and_return(true)
+
+ # Mock s3_helper.remove for remove_temp_file
+ allow(s3_helper).to receive(:remove).with(temp_path, false)
end
- it "creates optimized video record and rebakes posts" do
+ # Helper to get S3 path with multisite prefix if in multisite mode (matching get_s3_path logic)
+ def get_s3_path_for_test(path)
+ if Rails.configuration.multisite
+ multisite_prefix = File.join("uploads", RailsMultisite::ConnectionManagement.current_db)
+ multisite_prefix = "#{multisite_prefix}/"
+ # Prevent double-prepending if path already includes the multisite prefix
+ return path if path.start_with?(multisite_prefix)
+ File.join(multisite_prefix, path)
+ else
+ path
+ end
+ end
+
+ it "copies file from subdirectory to final location, deletes temp file, and creates optimized video record" do
optimized_video_instance = instance_double(OptimizedVideo)
allow(OptimizedVideo).to receive(:create_for).and_return(optimized_video_instance)
allow(s3_store).to receive(:update_file_access_control)
+ allow(Discourse.store).to receive(:get_path_for_upload).and_return(final_path)
- result = adapter.handle_completion(job_id, output_path, new_sha1)
+ result = adapter.handle_completion(job_id, new_sha1)
expect(result).to be true
- expect(s3_object).to have_received(:exists?)
- expect(s3_object).to have_received(:size)
- expect(s3_store).to have_received(:update_file_access_control).with(
- "#{output_path}.mp4",
- upload.secure?,
- )
- expect(OptimizedVideo).to have_received(:create_for).with(
- upload,
- "video_converted.mp4",
- upload.user_id,
- {
- extension: "mp4",
- filesize: 1024,
- sha1: new_sha1,
- url: expected_url,
- adapter: "aws_mediaconvert",
- },
- )
+ # Verify s3_helper operations
+ expect(s3_helper).to have_received(:object).with(temp_path)
+ expect(source_s3_object).to have_received(:exists?)
+ expect(source_s3_object).to have_received(:size)
+ expect(s3_helper).to have_received(:copy) do |source, dest, options: {}|
+ expect(source).to eq(temp_path)
+ expect(dest).to include(final_path)
+ end
+ expect(s3_helper).to have_received(:object).at_least(:once)
+ expect(destination_s3_object).to have_received(:exists?)
+ expect(s3_helper).to have_received(:remove).with(temp_path, false)
+ expect(s3_store).to have_received(:update_file_access_control).at_least(:once)
+ # The hash passed to create_for uses symbol keys (from **options)
+ expect(OptimizedVideo).to have_received(
+ :create_for,
+ ) do |upload_arg, filename, user_id, options|
+ expect(upload_arg).to eq(upload)
+ expect(filename).to eq("video_converted.mp4")
+ expect(user_id).to eq(upload.user_id)
+ expect(options[:extension]).to eq("mp4")
+ expect(options[:filesize]).to eq(1024)
+ expect(options[:sha1]).to eq(new_sha1)
+ # URL will include the destination path (with or without multisite prefix)
+ expect(options[:url]).to match(
+ %r{//#{s3_bucket}\.s3\.dualstack\.#{s3_region}\.amazonaws\.com.*#{final_path}},
+ )
+ expect(options[:etag]).to eq("etag123")
+ expect(options[:adapter]).to eq("aws_mediaconvert")
+ end
expect(post).to have_received(:rebake!)
expect(Rails.logger).to have_received(:info).with(/Rebaking post #{post.id}/)
end
context "when S3 object doesn't exist" do
- before { allow(s3_object).to receive(:exists?).and_return(false) }
+ before do
+ allow(s3_helper).to receive(:object).with(temp_path).and_return(source_s3_object)
+ allow(source_s3_object).to receive(:exists?).and_return(false)
+ end
it "returns false" do
- expect(adapter.handle_completion(job_id, output_path, new_sha1)).to be false
+ expect(adapter.handle_completion(job_id, new_sha1)).to be false
end
end
- context "when optimized video creation fails" do
+ context "when source object doesn't exist for copy" do
before do
allow(s3_object).to receive(:exists?).and_return(true)
- allow(s3_object).to receive(:size).and_return(1024)
- allow(OptimizedVideo).to receive(:create_for).and_return(false)
- allow(s3_store).to receive(:update_file_access_control)
+ allow(source_s3_object).to receive(:exists?).and_return(false)
+ allow(Discourse.store).to receive(:get_path_for_upload).and_return(final_path)
end
it "returns false and logs error" do
- adapter.handle_completion(job_id, output_path, new_sha1)
- expect(Rails.logger).to have_received(:error).with(/Failed to create OptimizedVideo record/)
- expect(adapter.handle_completion(job_id, output_path, new_sha1)).to be false
+ result = adapter.handle_completion(job_id, new_sha1)
+ expect(Rails.logger).to have_received(:error).with(
+ /MediaConvert temp file not found at #{temp_path}/,
+ )
+ expect(result).to be false
end
end
- context "when an error occurs" do
- let(:error) { StandardError.new("Test error") }
+ context "when copy fails" do
+ let(:error) { StandardError.new("Copy failed") }
before do
- allow(s3_object).to receive(:exists?).and_return(true)
- allow(s3_object).to receive(:size).and_return(1024)
- allow(OptimizedVideo).to receive(:create_for).and_raise(error)
- allow(s3_store).to receive(:update_file_access_control)
+ allow(s3_helper).to receive(:object).with(temp_path).and_return(source_s3_object)
+ allow(source_s3_object).to receive(:exists?).and_return(true)
+ allow(source_s3_object).to receive(:size).and_return(1024)
+ allow(s3_helper).to receive(:copy) do |source, dest, options: {}|
+ raise error if source == temp_path && dest.include?(final_path)
+ end
+ allow(Discourse.store).to receive(:get_path_for_upload).and_return(final_path)
end
it "returns false and logs error" do
- adapter.handle_completion(job_id, output_path, new_sha1)
- expect(Discourse).to have_received(:warn_exception).with(
- error,
- message: "Error in video processing completion",
- env: {
- upload_id: upload.id,
- job_id: job_id,
- },
- )
- expect(adapter.handle_completion(job_id, output_path, new_sha1)).to be false
+ adapter.handle_completion(job_id, new_sha1)
+ expect(Discourse).to have_received(:warn_exception) do |exception, options|
+ expect(exception).to eq(error)
+ expect(options[:message]).to eq("Error in video processing completion")
+ expect(options[:env][:upload_id]).to eq(upload.id)
+ expect(options[:env][:job_id]).to eq(job_id)
+ expect(options[:env][:temp_path]).to eq(temp_path)
+ expect(options[:env][:error_class]).to eq("StandardError")
+ expect(options[:env][:error_message]).to eq("Copy failed")
+ end
+ expect(adapter.handle_completion(job_id, new_sha1)).to be false
end
end
- context "when ACL update is disabled" do
+ context "when delete fails" do
+ let(:delete_error) { StandardError.new("Delete failed") }
+
before do
- allow(SiteSetting).to receive(:s3_use_acls).and_return(false)
- allow(s3_object).to receive(:exists?).and_return(true)
+ allow(s3_helper).to receive(:object) do |path|
+ if path == temp_path
+ source_s3_object
+ elsif path.include?(final_path)
+ destination_s3_object
+ else
+ source_s3_object
+ end
+ end
+ allow(source_s3_object).to receive(:exists?).and_return(true)
+ allow(source_s3_object).to receive(:size).and_return(1024)
+ allow(s3_helper).to receive(:copy) do |source, dest, options: {}|
+ [dest, "etag123"] if source == temp_path && dest.include?(final_path)
+ end
+ allow(destination_s3_object).to receive(:exists?).and_return(true)
+ allow(s3_helper).to receive(:remove).with(temp_path, false).and_raise(delete_error)
allow(OptimizedVideo).to receive(:create_for).and_return(true)
allow(s3_store).to receive(:update_file_access_control)
+ allow(Discourse.store).to receive(:get_path_for_upload).and_return(final_path)
end
- it "still calls update_file_access_control but it handles the disabled ACL setting internally" do
- adapter.handle_completion(job_id, output_path, new_sha1)
- expect(s3_store).to have_received(:update_file_access_control).with(
- "#{output_path}.mp4",
- upload.secure?,
+ it "logs warning but continues" do
+ result = adapter.handle_completion(job_id, new_sha1)
+ expect(Rails.logger).to have_received(:warn).with(
+ /Failed to delete temporary MediaConvert file/,
)
- expect(adapter.handle_completion(job_id, output_path, new_sha1)).to be true
+ expect(result).to be true
+ end
+ end
+
+ context "when optimized video creation fails" do
+ before do
+ allow(s3_helper).to receive(:object) do |path|
+ if path == temp_path
+ source_s3_object
+ elsif path.include?(final_path)
+ destination_s3_object
+ else
+ source_s3_object
+ end
+ end
+ allow(source_s3_object).to receive(:exists?).and_return(true)
+ allow(source_s3_object).to receive(:size).and_return(1024)
+ allow(s3_helper).to receive(:copy) do |source, dest, options: {}|
+ [dest, "etag123"] if source == temp_path && dest.include?(final_path)
+ end
+ allow(destination_s3_object).to receive(:exists?).and_return(true)
+ allow(s3_helper).to receive(:remove).with(temp_path, false)
+ allow(OptimizedVideo).to receive(:create_for).and_return(false)
+ allow(s3_store).to receive(:update_file_access_control)
+ allow(Discourse.store).to receive(:get_path_for_upload).and_return(final_path)
+ end
+
+ it "returns false and logs error" do
+ adapter.handle_completion(job_id, new_sha1)
+ expect(Rails.logger).to have_received(:error).with(/Failed to create OptimizedVideo record/)
+ expect(adapter.handle_completion(job_id, new_sha1)).to be false
+ end
+ end
+
+ context "when an error occurs" do
+ let(:error) { StandardError.new("Test error") }
+
+ before do
+ allow(s3_helper).to receive(:object) do |path|
+ if path == temp_path
+ source_s3_object
+ elsif path.include?(final_path)
+ destination_s3_object
+ else
+ source_s3_object
+ end
+ end
+ allow(source_s3_object).to receive(:exists?).and_return(true)
+ allow(source_s3_object).to receive(:size).and_return(1024)
+ allow(s3_helper).to receive(:copy) do |source, dest, options: {}|
+ [dest, "etag123"] if source == temp_path && dest.include?(final_path)
+ end
+ allow(destination_s3_object).to receive(:exists?).and_return(true)
+ allow(s3_helper).to receive(:remove).with(temp_path, false)
+ allow(OptimizedVideo).to receive(:create_for).and_raise(error)
+ allow(s3_store).to receive(:update_file_access_control)
+ allow(Discourse.store).to receive(:get_path_for_upload).and_return(final_path)
+ end
+
+ it "returns false and logs error" do
+ adapter.handle_completion(job_id, new_sha1)
+ expect(Discourse).to have_received(:warn_exception) do |exception, options|
+ expect(exception).to eq(error)
+ expect(options[:message]).to eq("Error in video processing completion")
+ expect(options[:env][:upload_id]).to eq(upload.id)
+ expect(options[:env][:job_id]).to eq(job_id)
+ expect(options[:env][:temp_path]).to eq(temp_path)
+ expect(options[:env][:error_class]).to eq("StandardError")
+ expect(options[:env][:error_message]).to eq("Test error")
+ end
+ expect(adapter.handle_completion(job_id, new_sha1)).to be false
end
end
end
diff --git a/spec/services/video_conversion/base_adapter_spec.rb b/spec/services/video_conversion/base_adapter_spec.rb
index a9fb5905a36..9ab20b06f82 100644
--- a/spec/services/video_conversion/base_adapter_spec.rb
+++ b/spec/services/video_conversion/base_adapter_spec.rb
@@ -37,9 +37,10 @@ RSpec.describe VideoConversion::BaseAdapter do
describe "#handle_completion" do
it "raises NotImplementedError" do
- expect {
- adapter.handle_completion("job-123", "/path/to/output.mp4", "new-sha1")
- }.to raise_error(NotImplementedError, "#{described_class} must implement #handle_completion")
+ expect { adapter.handle_completion("job-123", "new-sha1") }.to raise_error(
+ NotImplementedError,
+ "#{described_class} must implement #handle_completion",
+ )
end
end