Skip to content

Commit

Permalink
bug: move env qr code max upload length to runtime (#176)
Browse files Browse the repository at this point in the history
Co-authored-by: Jannik Streek <[email protected]>
  • Loading branch information
nwittstruck and JannikStreek authored Jan 22, 2024
1 parent f31637e commit 05746e9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
4 changes: 0 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ config :qrstorage, QrstorageWeb.Endpoint,
layout: false
]

# This is what will be used for the calculation on the client side. We add a buffer to account for deltas
# and overhead in the endpoint configuration, so the server actually allows for a bit more:
config :qrstorage, max_upload_length: System.get_env("QR_CODE_MAX_UPLOAD_LENGTH", "2666666")

# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
Expand Down
4 changes: 4 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ config :qrstorage, Qrstorage.Repo,
]
]

# This is what will be used for the calculation on the client side. We add a buffer to account for deltas
# and overhead in the endpoint configuration, so the server actually allows for a bit more:
config :qrstorage, max_upload_length: System.get_env("QR_CODE_MAX_UPLOAD_LENGTH", "2666666")

# Set possible translations
default_locale =
case config_env() do
Expand Down
12 changes: 6 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ services:
target: development
tty: true
environment:
DATABASE_USER: ${POSTGRES_USER:-postgres}
DATABASE_USER_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
DATABASE_NAME: ${POSTGRES_DB:-qrstorage_dev}
DATABASE_USER: ${POSTGRES_USER:-qrstorage-user}
DATABASE_USER_PASSWORD: ${POSTGRES_PASSWORD:-qrstorage-password}
DATABASE_NAME: ${POSTGRES_DB:-qrstorage-dev}
DATABASE_PORT: 5432
DATABASE_HOST: postgres
DATABASE_SSL: "false"
Expand Down Expand Up @@ -80,9 +80,9 @@ services:
postgres:
image: postgres:15
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
POSTGRES_DB: ${POSTGRES_DB:-qrstorage_dev}
POSTGRES_USER: ${POSTGRES_USER:-qrstorage-user}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-qrstorage-password}
POSTGRES_DB: ${POSTGRES_DB:-qrstorage-dev}
PGDATA: /var/lib/postgresql/data/pgdata
restart: always
# Exposing the port is not needed unless you want to access this database instance from the host.
Expand Down
11 changes: 6 additions & 5 deletions lib/qrstorage_web/endpoint.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule QrstorageWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :qrstorage
require Logger

# The session will be stored in the cookie and signed,
# this means its contents can be read but not tampered with.
Expand Down Expand Up @@ -36,16 +37,16 @@ defmodule QrstorageWeb.Endpoint do
plug Plug.RequestId
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]

# account for deltas and overhead for the upload length. deltas are roughly the same size as the actual input,
# so * 2 for deltas, plus 0.2 buffer for overhead and text characters:
upload_length_buffer = 2.2
max_upload_length = String.to_integer(Application.compile_env(:qrstorage, :max_upload_length))
max_upload_length = String.to_integer(System.get_env("QR_CODE_MAX_UPLOAD_LENGTH", "2666666"))

Logger.info("Reading QR_CODE_MAX_UPLOAD_LENGTH as: #{max_upload_length}")

plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library(),
length: ceil(max_upload_length * upload_length_buffer)
length: ceil(max_upload_length * upload_length_buffer),
json_decoder: Phoenix.json_library()

plug Plug.MethodOverride
plug Plug.Head
Expand Down

0 comments on commit 05746e9

Please sign in to comment.