-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for the firmware file size limit to be configurable (#1746)
This allows for the firmware file size upload limit to be adjusted via runtime env var. This also restrictions large file uploads to only the firmwares api route. I'd also like this to be configurable per org, such that free users can only upload smaller firmware sizes, but thats for a different discussion.
- Loading branch information
Showing
6 changed files
with
49 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule NervesHubWeb.DymanicConfigMultipart do | ||
@moduledoc """ | ||
A wrapper around `Plug.Parsers.MULTIPART` which allows for the `:length` opt (max file size) | ||
to be set during runtime. | ||
This also restricts large file uploads to the firmware upload api route. | ||
This can later be expanded to allow for different file size limits based on the organization. | ||
Thank you to https://hexdocs.pm/plug/Plug.Parsers.MULTIPART.html#module-dynamic-configuration | ||
for the inspiration. | ||
""" | ||
|
||
@multipart Plug.Parsers.MULTIPART | ||
|
||
def init(opts) do | ||
opts | ||
end | ||
|
||
def parse(conn, "multipart", subtype, headers, opts) do | ||
opts = @multipart.init([length: max_file_size(conn)] ++ opts) | ||
@multipart.parse(conn, "multipart", subtype, headers, opts) | ||
end | ||
|
||
def parse(conn, _type, _subtype, _headers, _opts) do | ||
{:next, conn} | ||
end | ||
|
||
defp max_file_size(conn) do | ||
if String.match?(conn.request_path, ~r/^\/api\/orgs\/\w+\/products\/\w+\/firmwares$/) do | ||
Application.get_env(:nerves_hub, NervesHub.Firmwares.Upload, [])[:max_size] | ||
else | ||
1_000_000 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters