feat: datasheet upload and auto-retrieve

- store datasheet PDFs on the server
- download PDF automatically when given a link
This commit is contained in:
Schuwi
2025-09-19 23:09:29 +02:00
parent 086bc65ac1
commit 5d2e3f7768
7 changed files with 517 additions and 47 deletions

View File

@@ -320,6 +320,30 @@ defmodule ComponentsElixir.Inventory do
|> Repo.insert()
end
@doc """
Creates a component and downloads datasheet from URL if provided.
"""
def create_component_with_datasheet(attrs \\ %{}) do
# If a datasheet_url is provided, download it
updated_attrs =
case Map.get(attrs, "datasheet_url") do
url when is_binary(url) and url != "" ->
case ComponentsElixir.DatasheetDownloader.download_pdf_from_url(url) do
{:ok, filename} ->
Map.put(attrs, "datasheet_filename", filename)
{:error, _reason} ->
# Continue without datasheet file if download fails
attrs
end
_ ->
attrs
end
%Component{}
|> Component.changeset(updated_attrs)
|> Repo.insert()
end
@doc """
Updates a component.
"""
@@ -329,6 +353,34 @@ defmodule ComponentsElixir.Inventory do
|> Repo.update()
end
@doc """
Updates a component and downloads datasheet from URL if provided.
"""
def update_component_with_datasheet(%Component{} = component, attrs) do
# If a datasheet_url is provided and changed, download it
updated_attrs =
case Map.get(attrs, "datasheet_url") do
url when is_binary(url) and url != "" and url != component.datasheet_url ->
case ComponentsElixir.DatasheetDownloader.download_pdf_from_url(url) do
{:ok, filename} ->
# Delete old datasheet file if it exists
if component.datasheet_filename do
ComponentsElixir.DatasheetDownloader.delete_datasheet_file(component.datasheet_filename)
end
Map.put(attrs, "datasheet_filename", filename)
{:error, _reason} ->
# Keep existing filename if download fails
attrs
end
_ ->
attrs
end
component
|> Component.changeset(updated_attrs)
|> Repo.update()
end
@doc """
Deletes a component.
"""