Skip to content

Commit 94ea6f4

Browse files
committed
feat(uploads): add simple download route
for devices that need it
1 parent e2e46c1 commit 94ea6f4

File tree

2 files changed

+102
-38
lines changed

2 files changed

+102
-38
lines changed

OPENAPI_DOC.yml

Lines changed: 77 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18318,6 +18318,83 @@ paths:
1831818318
application/json:
1831918319
schema:
1832018320
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
18321+
/api/engine/v2/uploads/{id}/download/{api_key}/{file_name}:
18322+
get:
18323+
summary: a traditional looking route for downloading files
18324+
tags:
18325+
- Uploads
18326+
operationId: PlaceOS::Api::Uploads_download_simple_route
18327+
parameters:
18328+
- name: id
18329+
in: path
18330+
description: upload id of the upload
18331+
example: uploads-XXX
18332+
required: true
18333+
schema:
18334+
type: string
18335+
- name: api_key
18336+
in: path
18337+
required: true
18338+
schema:
18339+
type: string
18340+
- name: file_name
18341+
in: query
18342+
required: true
18343+
schema:
18344+
type: string
18345+
responses:
18346+
200:
18347+
description: OK
18348+
409:
18349+
description: Conflict
18350+
content:
18351+
application/json:
18352+
schema:
18353+
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
18354+
401:
18355+
description: Unauthorized
18356+
content:
18357+
application/json:
18358+
schema:
18359+
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
18360+
403:
18361+
description: Forbidden
18362+
404:
18363+
description: Not Found
18364+
content:
18365+
application/json:
18366+
schema:
18367+
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
18368+
408:
18369+
description: Request Timeout
18370+
content:
18371+
application/json:
18372+
schema:
18373+
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
18374+
400:
18375+
description: Bad Request
18376+
content:
18377+
application/json:
18378+
schema:
18379+
$ref: '#/components/schemas/PlaceOS__Api__Application__ParameterError'
18380+
422:
18381+
description: Unprocessable Entity
18382+
content:
18383+
application/json:
18384+
schema:
18385+
$ref: '#/components/schemas/PlaceOS__Api__Application__ParameterError'
18386+
406:
18387+
description: Not Acceptable
18388+
content:
18389+
application/json:
18390+
schema:
18391+
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
18392+
415:
18393+
description: Unsupported Media Type
18394+
content:
18395+
application/json:
18396+
schema:
18397+
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
1832118398
/api/engine/v2/uploads/{id}/edit:
1832218399
get:
1832318400
summary: obtain a signed request for each chunk of the file being uploaded.
@@ -22557,13 +22634,6 @@ components:
2255722634
makebreak:
2255822635
type: boolean
2255922636
nullable: true
22560-
analytics_enabled:
22561-
type: boolean
22562-
nullable: true
22563-
analytics_interval_minutes:
22564-
type: integer
22565-
format: Int32
22566-
nullable: true
2256722637
uri:
2256822638
type: string
2256922639
nullable: true
@@ -23442,13 +23512,6 @@ components:
2344223512
makebreak:
2344323513
type: boolean
2344423514
nullable: true
23445-
analytics_enabled:
23446-
type: boolean
23447-
nullable: true
23448-
analytics_interval_minutes:
23449-
type: integer
23450-
format: Int32
23451-
nullable: true
2345223515
uri:
2345323516
type: string
2345423517
nullable: true
@@ -25893,13 +25956,6 @@ components:
2589325956
makebreak:
2589425957
type: boolean
2589525958
nullable: true
25896-
analytics_enabled:
25897-
type: boolean
25898-
nullable: true
25899-
analytics_interval_minutes:
25900-
type: integer
25901-
format: Int32
25902-
nullable: true
2590325959
uri:
2590425960
type: string
2590525961
nullable: true
@@ -28622,13 +28678,6 @@ components:
2862228678
makebreak:
2862328679
type: boolean
2862428680
nullable: true
28625-
analytics_enabled:
28626-
type: boolean
28627-
nullable: true
28628-
analytics_interval_minutes:
28629-
type: integer
28630-
format: Int32
28631-
nullable: true
2863228681
uri:
2863328682
type: string
2863428683
nullable: true

src/placeos-rest-api/controllers/uploads.cr

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module PlaceOS::Api
1717
end
1818
end
1919

20-
@[AC::Route::Filter(:before_action, only: [:get_link, :download_proxy_file_contents, :edit, :update, :finished, :destroy])]
20+
@[AC::Route::Filter(:before_action, only: [:get_link, :download_proxy_file_contents, :download_simple_route, :edit, :update, :finished, :destroy])]
2121
def get_upload(
2222
@[AC::Param::Info(description: "upload id of the upload", example: "uploads-XXX")]
2323
id : String,
@@ -104,15 +104,15 @@ module PlaceOS::Api
104104

105105
# 4. Assemble full SQL (with CTE for total_count)
106106
sql = <<-SQL
107-
WITH total AS (
108-
SELECT COUNT(u.*) AS total_count
109-
FROM "#{table_name}" u
110-
#{where_clause}
111-
)
112-
SELECT u.*, t.total_count FROM "#{table_name}" u
113-
CROSS JOIN total t
114-
#{where_clause}
115-
LIMIT $#{limit_idx} OFFSET $#{offset_idx};
107+
WITH total AS (
108+
SELECT COUNT(u.*) AS total_count
109+
FROM "#{table_name}" u
110+
#{where_clause}
111+
)
112+
SELECT u.*, t.total_count FROM "#{table_name}" u
113+
CROSS JOIN total t
114+
#{where_clause}
115+
LIMIT $#{limit_idx} OFFSET $#{offset_idx};
116116
SQL
117117

118118
# 5. Append limit and offset params
@@ -276,6 +276,21 @@ module PlaceOS::Api
276276
end
277277
end
278278

279+
# skip authentication for the lookup
280+
skip_action :authorize!, only: :download_simple_route
281+
skip_action :set_user_id, only: :download_simple_route
282+
283+
# a traditional looking route for downloading files
284+
@[AC::Route::GET("/:id/download/:api_key/*:file_name")]
285+
def download_simple_route(api_key : String, file_name : String)
286+
# check api key
287+
request.headers["X-API-Key"] = api_key
288+
authorize!
289+
290+
response.headers["Content-Disposition"] = %(attachment; filename="#{file_name}")
291+
download_proxy_file_contents
292+
end
293+
279294
# obtain a signed request for each chunk of the file being uploaded.
280295
#
281296
# once all parts are uploaded, you can obtain the final commit request by passing `?part=finish`

0 commit comments

Comments
 (0)