Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion backend/lib/edgehog/devices/device/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ defmodule Edgehog.Devices.Device do
paginate_relationship_with application_deployments: :relay,
ota_operations: :relay,
tags: :relay,
file_download_requests: :relay
file_download_requests: :relay,
file_upload_requests: :relay

subscriptions do
pubsub EdgehogWeb.Endpoint
Expand Down Expand Up @@ -508,6 +509,12 @@ defmodule Edgehog.Devices.Device do
writable? false
end

has_many :file_upload_requests, Edgehog.Files.FileUploadRequest do
public? true
description "The existing file upload requests for this device"
writable? false
end

has_many :application_deployments, Deployment do
public? true
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# This file is part of Edgehog.
#
# Copyright 2026 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Files.FileUploadRequest.Calculations.GetPresignedUrl do
@moduledoc false
use Ash.Resource.Calculation

alias Ash.Resource.Calculation

@files_storage_module Application.compile_env(
:edgehog,
:files_storage_module,
Edgehog.Storage
)

@impl Calculation
def load(_query, _opts, _context) do
[:id]
end

@impl Calculation
def calculate(records, _opts, context) do
tenant_id = extract_tenant_id(context)

Enum.map(records, fn file_upload_request ->
file_path =
"uploads/tenants/#{tenant_id}/file_upload_requests/#{file_upload_request.id}"

case @files_storage_module.read_presigned_url(file_path) do
{:ok, %{get_url: get_url}} -> get_url
{:error, _reason} -> nil
end
end)
end

# Ash context can carry tenant as a struct in request flows or as a raw id in
# background executor flows, so we support both shapes to avoid crashes.
defp extract_tenant_id(%{tenant: %{tenant_id: tenant_id}}), do: tenant_id
defp extract_tenant_id(%{tenant: tenant_id}), do: tenant_id
defp extract_tenant_id(_), do: nil
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,42 @@ defmodule Edgehog.Files.FileUploadRequest do
@moduledoc false
use Edgehog.MultitenantResource,
domain: Edgehog.Files,
extensions: [AshGraphql.Resource]
extensions: [AshGraphql.Resource],
notifiers: [Ash.Notifier.PubSub]

alias Edgehog.Files.FileUploadRequest.Calculations
alias Edgehog.Files.FileUploadRequest.Changes
alias Edgehog.Files.FileUploadRequest.FileSource
alias Edgehog.Files.FileUploadRequest.ManualActions
alias Edgehog.Files.FileUploadRequest.Status

graphql do
type :file_upload_request

subscriptions do
pubsub EdgehogWeb.Endpoint

subscribe :file_upload_requests do
action_types [:create, :update]
end

subscribe :file_upload_requests_by_device do
action_types [:create, :update]
read_action :read_by_device
relay_id_translations device_id: :device
end
end
end

actions do
defaults [:read, :destroy]

read :read_by_device do
argument :device_id, :id, allow_nil?: false

get_by :device_id
end

create :send_request do
accept [:source, :source_type, :compression, :progress_tracked, :http_headers]

Expand Down Expand Up @@ -160,6 +182,16 @@ defmodule Edgehog.Files.FileUploadRequest do
end
end

calculations do
calculate :get_presigned_url, :string do
public? true

description "Get a presigned URL for downloading the uploaded file. The URL is valid for a limited time."

calculation Calculations.GetPresignedUrl
end
end

postgres do
table "file_upload_requests"
repo Edgehog.Repo
Expand Down
Loading
Loading