Class: CardDB::Resources::Files
- Defined in:
- lib/carddb/resources/files.rb
Overview
File upload helper resource.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#confirm_upload(id:) ⇒ Object
Confirm that a presigned upload completed.
-
#delete(id:) ⇒ Object
Delete a file.
-
#get(id, cache: nil) ⇒ Object
Get one file by ID.
-
#request_upload(input:) ⇒ Object
Request a presigned URL for direct upload.
-
#upload(filename:, content_type:, body:, size: nil, purpose: nil, is_public: false, entity_type: nil, entity_id: nil, publisher_id: nil, dataset_id: nil) ⇒ Object
Request a presigned URL, upload bytes directly to storage, and confirm the file.
Methods inherited from Base
Constructor Details
This class inherits a constructor from CardDB::Resources::Base
Instance Method Details
#confirm_upload(id:) ⇒ Object
Confirm that a presigned upload completed. Requires a server-side secret credential.
25 26 27 28 29 30 |
# File 'lib/carddb/resources/files.rb', line 25 def confirm_upload(id:) config.require_secret_credential!('files.confirm_upload') data = connection.execute(QueryBuilder.file_upload_confirm, { id: id }) File.new(data['fileUploadConfirm'], client: client) end |
#delete(id:) ⇒ Object
Delete a file. Requires a server-side secret credential. rubocop:disable Naming/PredicateMethod
67 68 69 70 71 72 |
# File 'lib/carddb/resources/files.rb', line 67 def delete(id:) config.require_secret_credential!('files.delete') data = connection.execute(QueryBuilder.file_delete, { id: id }) !!data['fileDelete'] end |
#get(id, cache: nil) ⇒ Object
Get one file by ID.
8 9 10 11 12 13 14 |
# File 'lib/carddb/resources/files.rb', line 8 def get(id, cache: nil) key = cache_key('files', 'get', id: id) with_cache(key, resource: :files, cache: cache) do data = connection.execute(QueryBuilder.file, { id: id }) data['file'] ? File.new(data['file'], client: client) : nil end end |
#request_upload(input:) ⇒ Object
Request a presigned URL for direct upload. Requires a server-side secret credential.
17 18 19 20 21 22 |
# File 'lib/carddb/resources/files.rb', line 17 def request_upload(input:) config.require_secret_credential!('files.request_upload') data = connection.execute(QueryBuilder.file_upload_request, { input: input }) PresignedUpload.new(data['fileUploadRequest'], client: client) end |
#upload(filename:, content_type:, body:, size: nil, purpose: nil, is_public: false, entity_type: nil, entity_id: nil, publisher_id: nil, dataset_id: nil) ⇒ Object
Request a presigned URL, upload bytes directly to storage, and confirm the file.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/carddb/resources/files.rb', line 33 def upload( filename:, content_type:, body:, size: nil, purpose: nil, is_public: false, entity_type: nil, entity_id: nil, publisher_id: nil, dataset_id: nil ) upload_size = size || infer_upload_size(body) raise ArgumentError, 'files.upload requires a positive size or an inferable body size' unless upload_size&.positive? upload = request_upload( input: { filename: filename, contentType: content_type, size: upload_size, isPublic: is_public, entityType: entity_type || entity_type_for_purpose(purpose), entityId: entity_id, publisherId: publisher_id, datasetId: dataset_id }.compact ) put_upload(upload.upload_url, content_type, body) confirm_upload(id: upload.file.id) end |