Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(platform/image-processor): allow http downloads on image processor #188

Merged
merged 4 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion common/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ where
set_abort(abort);

// The reason this is allowed to be asserted is because we're catching the panic
// and returning None instead of propagating it, if the panic was caused by a task abort.
// and returning None instead of propagating it, if the panic was caused by a
// task abort.
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
Ok(r) => Some(r),
Err(err) => {
Expand Down
1 change: 1 addition & 0 deletions platform/image_processor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ gifski = "1.13"
png = "0.17"
num_cpus = "1.16"
bytes = "1.0"
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"] }

common = { workspace = true, features = ["default"] }
config = { workspace = true }
Expand Down
4 changes: 4 additions & 0 deletions platform/image_processor/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct ImageProcessorConfig {

/// Instance ID (defaults to a random ULID)
pub instance_id: Ulid,

/// Allow http downloads
pub allow_http: bool,
}

impl Default for ImageProcessorConfig {
Expand All @@ -24,6 +27,7 @@ impl Default for ImageProcessorConfig {
target_bucket: S3BucketConfig::default(),
concurrency: num_cpus::get(),
instance_id: Ulid::new(),
allow_http: true,
}
}
}
6 changes: 6 additions & 0 deletions platform/image_processor/src/processor/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ pub enum ProcessorError {

#[error("gifski encode: {0}")]
GifskiEncode(anyhow::Error),

#[error("http download disabled")]
HttpDownloadDisabled,

#[error("http download: {0}")]
HttpDownload(#[from] reqwest::Error),
}

pub type Result<T, E = ProcessorError> = std::result::Result<T, E>;
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ impl<'data> FfmpegDecoder<'data> {
let input_stream_duration = input_stream.duration().unwrap_or(0);
let input_stream_frames = input_stream
.nb_frames()
.ok_or_else(|| ProcessorError::FfmpegDecode(anyhow!("no frame count")))?.max(1);
.ok_or_else(|| ProcessorError::FfmpegDecode(anyhow!("no frame count")))?
.max(1);

if input_stream_time_base.den == 0 || input_stream_time_base.num == 0 {
return Err(ProcessorError::FfmpegDecode(anyhow!("stream time base is 0")));
Expand Down
46 changes: 31 additions & 15 deletions platform/image_processor/src/processor/job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,39 @@ pub async fn handle_job(

impl<'a, G: ImageProcessorGlobal> Job<'a, G> {
async fn download_source(&self) -> Result<Bytes> {
tracing::info!(
"downloading {}/{}",
self.global.config().source_bucket.name,
self.job.id.to_string(),
);

let response = self
.global
.s3_source_bucket()
.get_object(&self.job.task.input_path)
.await
.map_err(ProcessorError::S3Download)?;
if self.job.task.input_path.starts_with("http://") || self.job.task.input_path.starts_with("https://") {
if !self.global.config().allow_http {
return Err(ProcessorError::HttpDownloadDisabled);
}

tracing::info!("downloading {}", self.job.task.input_path);

let response = reqwest::get(&self.job.task.input_path)
.await
.map_err(ProcessorError::HttpDownload)?;

response.error_for_status_ref().map_err(ProcessorError::HttpDownload)?;

if (200..299).contains(&response.status_code()) {
Ok(response.bytes().clone())
Ok(response.bytes().await.map_err(ProcessorError::HttpDownload)?)
} else {
Err(ProcessorError::S3Download(s3::error::S3Error::HttpFail))
tracing::info!(
"downloading {}/{}",
self.global.config().source_bucket.name,
self.job.task.input_path
);

let response = self
.global
.s3_source_bucket()
.get_object(&self.job.task.input_path)
.await
.map_err(ProcessorError::S3Download)?;

if (200..299).contains(&response.status_code()) {
Ok(response.bytes().clone())
} else {
Err(ProcessorError::S3Download(s3::error::S3Error::HttpFail))
}
}
}

Expand Down
Loading