Skip to content

Commit 902c995

Browse files
authored
feat(upload): add support for announce_uploaded flag in upload tokens (#55)
1 parent 933ce7a commit 902c995

6 files changed

Lines changed: 40 additions & 10 deletions

File tree

src/uploads/_index.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ entries:
373373
imports:
374374
upload_repo: userspace.uploads:upload_repo
375375
upload_type: userspace.uploads:upload_type
376+
pipeline_lib: userspace.uploads:pipeline_lib
376377
uploads_resources: userspace.uploads:uploads_resources
377378
content_repo: userspace.uploads:content_repo
378379

src/uploads/pipeline_lib.lua

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ function pipeline_lib.merge_metadata(existing_metadata, new_metadata)
7878
return merged
7979
end
8080

81-
-- Invoke upload completion token callback (fire-and-forget)
82-
function pipeline_lib.invoke_upload_token(upload, success, error_msg)
81+
function pipeline_lib.invoke_upload_token(upload, status, error_msg)
8382
if not upload or not upload.metadata or not upload.metadata.__upload_token then
8483
return
8584
end
@@ -90,7 +89,11 @@ function pipeline_lib.invoke_upload_token(upload, success, error_msg)
9089
return
9190
end
9291

93-
local func_id = tostring(success and payload.function_id or payload.on_error_id)
92+
if status == STATUS.UPLOADED and not payload.announce_uploaded then
93+
return
94+
end
95+
96+
local func_id = tostring(status == STATUS.ERROR and payload.on_error_id or payload.function_id)
9497
if not func_id or func_id == "nil" then
9598
return
9699
end
@@ -118,7 +121,7 @@ function pipeline_lib.invoke_upload_token(upload, success, error_msg)
118121

119122
local _, call_err = executor:call(tostring(func_id), {
120123
upload_id = upload.uuid,
121-
status = success and "completed" or "error",
124+
status = status,
122125
error = error_msg,
123126
mime_type = upload.mime_type,
124127
size = upload.size,
@@ -165,7 +168,7 @@ function pipeline_lib.process_upload(upload)
165168
-- Notify about error status
166169
pipeline_lib.notify_status_change(upload, STATUS.ERROR, nil, error_msg)
167170

168-
pipeline_lib.invoke_upload_token(upload, false, error_msg)
171+
pipeline_lib.invoke_upload_token(upload, STATUS.ERROR, error_msg)
169172

170173
return false, error_msg
171174
end
@@ -190,7 +193,7 @@ function pipeline_lib.process_upload(upload)
190193
-- Notify about error status
191194
pipeline_lib.notify_status_change(upload, STATUS.ERROR, nil, error_msg)
192195

193-
pipeline_lib.invoke_upload_token(upload, false, error_msg)
196+
pipeline_lib.invoke_upload_token(upload, STATUS.ERROR, error_msg)
194197

195198
return false, error_msg
196199
end
@@ -245,7 +248,7 @@ function pipeline_lib.process_upload(upload)
245248
-- Notify about error status with stage info
246249
pipeline_lib.notify_status_change(upload, STATUS.ERROR, stage_title, error_msg)
247250

248-
pipeline_lib.invoke_upload_token(upload, false, error_msg)
251+
pipeline_lib.invoke_upload_token(upload, STATUS.ERROR, error_msg)
249252

250253
return false, error_msg
251254
end
@@ -279,12 +282,12 @@ function pipeline_lib.process_upload(upload)
279282
-- Notify about completion status
280283
pipeline_lib.notify_status_change(upload, STATUS.COMPLETED)
281284

282-
pipeline_lib.invoke_upload_token(upload, true, nil)
285+
pipeline_lib.invoke_upload_token(upload, STATUS.COMPLETED)
283286

284287
return true
285288
end
286289

287290
-- Export constants
288291
pipeline_lib.STATUS = STATUS
289292

290-
return pipeline_lib
293+
return pipeline_lib

src/uploads/test/upload_tokens_test.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ local function define_tests()
4848
test.eq(result.actor_scope, "default")
4949
test.is_nil(result.on_error_id)
5050
test.is_nil(result.params)
51+
-- Default contract: no early announce unless explicitly requested.
52+
test.is_nil(result.announce_uploaded)
53+
end)
54+
55+
it("should carry the opt-in announce_uploaded flag", function()
56+
local token, err = upload_tokens.pack({
57+
function_id = "my.module:handler",
58+
actor_id = "user-123",
59+
actor_scope = "default",
60+
announce_uploaded = true,
61+
})
62+
test.is_nil(err)
63+
64+
local result, uerr = upload_tokens.unpack(token :: string)
65+
test.is_nil(uerr)
66+
test.eq(result.announce_uploaded, true)
5167
end)
5268

5369
it("should error on missing function_id", function()

src/uploads/upload_lib.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local logger = require("logger")
88

99
local upload_repo = require("upload_repo")
1010
local upload_type = require("upload_type")
11+
local pipeline_lib = require("pipeline_lib")
1112
local resources = require("uploads_resources")
1213
local content_repo = require("content_repo")
1314

@@ -287,6 +288,8 @@ function upload_lib.upload_file(user_id: string, file_data: string | stream.Stre
287288

288289
publish_to_queue(upload_uuid)
289290

291+
pipeline_lib.invoke_upload_token(upload, pipeline_lib.STATUS.UPLOADED)
292+
290293
return upload
291294
end
292295

@@ -433,6 +436,8 @@ function upload_lib.complete_presigned_url(user_id, upload_id, etag, metadata_up
433436

434437
publish_to_queue(upload_id)
435438

439+
pipeline_lib.invoke_upload_token(upload, pipeline_lib.STATUS.UPLOADED)
440+
436441
return upload
437442
end
438443

@@ -668,6 +673,8 @@ function upload_lib.complete_multipart_upload(user_id, upload_id, parts: {{part_
668673
upload.updated_at = updated.updated_at
669674
upload.metadata = metadata
670675

676+
pipeline_lib.invoke_upload_token(upload, pipeline_lib.STATUS.UPLOADED)
677+
671678
return upload
672679
end
673680

src/uploads/upload_repo.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,9 @@ function upload_repo.complete_s3_upload(user_id, upload_id, etag, key, metadata_
739739
return nil, "Failed to update upload record: " .. tostring(err)
740740
end
741741

742-
return updated
742+
upload.status = updated.status
743+
upload.updated_at = updated.updated_at
744+
return upload
743745
end
744746

745747
function upload_repo.list_with_filters(options)

src/uploads/upload_tokens.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ local function pack(params)
5656
params = params.params,
5757
actor_id = params.actor_id,
5858
actor_scope = params.actor_scope,
59+
announce_uploaded = params.announce_uploaded and true or nil,
5960
issued_at = os.time(),
6061
}
6162

0 commit comments

Comments
 (0)