feat: datasheet upload and auto-retrieve
- store datasheet PDFs on the server - download PDF automatically when given a link
This commit is contained in:
@@ -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.
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user