Skip to content

Commit

Permalink
Logo personnalisé d'un JDD : ajout timestamp dans nom de fichier (#4040)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti authored Jul 8, 2024
1 parent c23eaa5 commit dce2db1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
7 changes: 4 additions & 3 deletions apps/transport/lib/jobs/custom_logo_conversion_job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ defmodule Transport.Jobs.CustomLogoConversionJob do
use Oban.Worker, max_attempts: 3

@impl Oban.Worker
def perform(%Oban.Job{args: %{"datagouv_id" => datagouv_id, "path" => path}}) do
def perform(%Oban.Job{args: %{"datagouv_id" => datagouv_id, "path" => path}, inserted_at: %DateTime{} = inserted_at}) do
%DB.Dataset{datagouv_id: datagouv_id} = DB.Repo.get_by!(DB.Dataset, datagouv_id: datagouv_id)
local_path = Path.join(System.tmp_dir!(), path)
extension = local_path |> Path.extname() |> String.downcase()
timestamp = DateTime.to_unix(inserted_at)

logo_filename = "#{datagouv_id}#{extension}"
full_logo_filename = "#{datagouv_id}_full#{extension}"
logo_filename = "#{datagouv_id}.#{timestamp}#{extension}"
full_logo_filename = "#{datagouv_id}_full.#{timestamp}#{extension}"
logo_path = Path.join(System.tmp_dir!(), logo_filename)
full_logo_path = Path.join(System.tmp_dir!(), full_logo_filename)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ defmodule Transport.Test.Transport.Jobs.CustomLogoConversionJobTest do
remote_path = "#{Ecto.UUID.generate()}.png"
local_path = Path.join(System.tmp_dir!(), remote_path)

logo_filename = "#{datagouv_id}.png"
full_logo_filename = "#{datagouv_id}_full.png"
inserted_at = DateTime.utc_now()
inserted_at_unix = DateTime.to_unix(inserted_at)
logo_filename = "#{datagouv_id}.#{inserted_at_unix}.png"
full_logo_filename = "#{datagouv_id}_full.#{inserted_at_unix}.png"

Transport.ExAWS.Mock
|> expect(:request!, fn %ExAws.S3.Download{
Expand All @@ -44,7 +46,12 @@ defmodule Transport.Test.Transport.Jobs.CustomLogoConversionJobTest do

Transport.Test.S3TestUtils.s3_mocks_delete_object(Transport.S3.bucket_name(:logos), remote_path)

assert :ok == perform_job(CustomLogoConversionJob, %{"datagouv_id" => datagouv_id, "path" => remote_path})
assert :ok ==
perform_job(
CustomLogoConversionJob,
%{"datagouv_id" => datagouv_id, "path" => remote_path},
inserted_at: inserted_at
)

refute File.exists?(local_path)
expected_logo_url = Transport.S3.permanent_url(:logos, logo_filename)
Expand Down

0 comments on commit dce2db1

Please sign in to comment.