Skip to content

Commit db61e6e

Browse files
committed
chore: add path_on_device to file download request
Add trigger to track the path on device for file download requests. Signed-off-by: Omar <omar.brbutovic@secomind.com>
1 parent c4878fc commit db61e6e

7 files changed

Lines changed: 483 additions & 2 deletions

File tree

REUSE.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ SPDX-License-Identifier = "Apache-2.0"
6060
[[annotations]]
6161
path = "backend/priv/astarte_resources/trigger_templates/**.json.eex"
6262
precedence = "aggregate"
63-
SPDX-FileCopyrightText = "2023-2025 Seco Mind Srl"
63+
SPDX-FileCopyrightText = "2023-2026 Seco Mind Srl"
6464
SPDX-License-Identifier = "Apache-2.0"
6565

6666
[[annotations]]

backend/lib/edgehog/files/file_download_request/file_download_request.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ defmodule Edgehog.Files.FileDownloadRequest do
144144

145145
run ManualActions.SendFileDownloadRequest
146146
end
147+
148+
update :set_path_on_device do
149+
argument :path_on_device, :string, allow_nil?: false
150+
151+
change set_attribute(:path_on_device, arg(:path_on_device))
152+
end
153+
154+
update :set_size_bytes do
155+
argument :decompressed_file_size_bytes, :integer, allow_nil?: false
156+
157+
change set_attribute(:uncompressed_file_size_bytes, arg(:decompressed_file_size_bytes))
158+
end
147159
end
148160

149161
attributes do
@@ -217,6 +229,11 @@ defmodule Edgehog.Files.FileDownloadRequest do
217229
public? true
218230
end
219231

232+
attribute :path_on_device, :string do
233+
description "Path on the device for the transferred file"
234+
public? true
235+
end
236+
220237
attribute :progress, :boolean do
221238
description "Flag to enable the progress reporting of the download."
222239
public? true

backend/lib/edgehog/files/files.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ defmodule Edgehog.Files do
8484
resource Repository
8585

8686
resource FileDownloadRequest do
87+
define :fetch_file_download_request, action: :read, get_by: [:id]
8788
define :send_file_download_request, args: [:file_download_request]
89+
define :set_path_on_device, args: [:path_on_device]
90+
define :set_size_bytes, args: [:decompressed_file_size_bytes]
8891
end
8992
end
9093
end

backend/lib/edgehog/triggers/handler/manual_actions/handle_trigger.ex

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# This file is part of Edgehog.
33
#
4-
# Copyright 2024 - 2025 SECO Mind Srl
4+
# Copyright 2024-2026 SECO Mind Srl
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ defmodule Edgehog.Triggers.Handler.ManualActions.HandleTrigger do
2828
alias Edgehog.Containers.Deployment
2929
alias Edgehog.Devices
3030
alias Edgehog.Devices.Device
31+
alias Edgehog.Files
3132
alias Edgehog.OSManagement
3233
alias Edgehog.Triggers.DeviceConnected
3334
alias Edgehog.Triggers.DeviceDeletionFinished
@@ -49,6 +50,7 @@ defmodule Edgehog.Triggers.Handler.ManualActions.HandleTrigger do
4950
@ota_event "io.edgehog.devicemanager.OTAEvent"
5051
@ota_response "io.edgehog.devicemanager.OTAResponse"
5152
@system_info "io.edgehog.devicemanager.SystemInfo"
53+
@file_storage "io.edgehog.devicemanager.storage.File"
5254

5355
@impl Ash.Resource.Actions.Implementation
5456
def run(input, _opts, _context) do
@@ -363,6 +365,23 @@ defmodule Edgehog.Triggers.Handler.ManualActions.HandleTrigger do
363365
end
364366
end
365367

368+
defp handle_event(%IncomingData{interface: @file_storage} = event, tenant, _realm_id, _device_id, _timestamp) do
369+
case String.split(event.path, "/") do
370+
["", request_id, "pathOnDevice"] ->
371+
file_download_request = Files.fetch_file_download_request!(request_id, tenant: tenant)
372+
373+
Files.set_path_on_device(file_download_request, event.value, tenant: tenant)
374+
375+
["", request_id, "sizeBytes"] ->
376+
file_download_request = Files.fetch_file_download_request!(request_id, tenant: tenant)
377+
378+
Files.set_size_bytes(file_download_request, event.value, tenant: tenant)
379+
380+
_ ->
381+
{:error, :invalid_event_path}
382+
end
383+
end
384+
366385
defp handle_event(_unhandled_event, tenant, realm_id, device_id, _timestamp) do
367386
Device
368387
|> Ash.Changeset.for_create(:from_unhandled_event, %{realm_id: realm_id, device_id: device_id})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "edgehog-file-transfer-completion",
3+
"action": {
4+
"http_url": "<%= @trigger_url %>",
5+
"http_method": "post",
6+
"http_static_headers": {},
7+
"ignore_ssl_errors": false
8+
},
9+
"simple_triggers": [
10+
{
11+
"type": "data_trigger",
12+
"interface_name": "io.edgehog.devicemanager.storage.File",
13+
"interface_major": 0,
14+
"on": "incoming_data",
15+
"match_path": "/*",
16+
"value_match_operator": "*"
17+
}
18+
]
19+
<%= if @can_use_trigger_delivery_policy do %>,
20+
"policy": "edgehog-retry-on-server-error"
21+
<% end %>
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# This file is part of Edgehog.
3+
#
4+
# Copyright 2026 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
20+
21+
defmodule Edgehog.Repo.Migrations.AddPathOnDevice do
22+
@moduledoc """
23+
Updates resources based on their most recent snapshots.
24+
25+
This file was autogenerated with `mix ash_postgres.generate_migrations`
26+
"""
27+
28+
use Ecto.Migration
29+
30+
def up do
31+
alter table(:file_download_requests) do
32+
add :path_on_device, :text
33+
end
34+
end
35+
36+
def down do
37+
alter table(:file_download_requests) do
38+
remove :path_on_device
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)