Class: Crm::Core::AttachmentStore
- Inherits:
-
Object
- Object
- Crm::Core::AttachmentStore
- Defined in:
- lib/crm/core/attachment_store.rb
Overview
AttachmentStore
represents an attachment of an activity comment.
To upload a file as an attachment, add it to comment_attachments
. The SDK will automatically upload the content of the file.
Note that this method of uploading an attachment using a browser will upload the file twice. It is first uploaded to the ruby application (e.g. Rails) which then uploads it to AWS S3.
To upload the attachment directly to AWS S3 (i.e. bypassing the ruby application), please proceed as follows:
-
Request an upload permission (AttachmentStore.generate_upload_permission). The response grants the client permission to upload a file to a given key on AWS S3. This permission is valid for one hour.
-
Upload the file to the URL (
permission.url
orpermission.uri
), together with the fields (permission.fields
) as parameters. AWS S3 itself then verifies the signature of these parameters prior to accepting the upload. -
Attach the upload to a new activity comment by setting its
comment_attachments
attribute to an array of upload IDs. The client may append filenames to the upload IDs for producing download URLs with proper filenames later on.The format of
comment_attachments
is["upload_id/filename.ext", ...]
, e.g.["e13f0d960feeb2b2903bd/screenshot.jpg"]
. JustRelate WebCRM in turn translates these upload IDs to attachment IDs. Syntactically they look the same. Upload IDs, however, are only temporary, whereas attachment IDs are permanent. If the client appended a filename to the upload ID, the attachment ID will contain this filename, too. Otherwise, the attachment ID ends with"/file"
. Please note that JustRelate WebCRM replaces filename characters other thana-zA-Z0-9.+-
with a dash. Multiple dashes will be joined into a single dash. -
Later, when downloading the attachment, pass the attachment ID to AttachmentStore.generate_download_url. JustRelate WebCRM returns a signed AWS S3 URL that remains valid for 5 minutes.
Defined Under Namespace
Classes: Permission
Class Method Summary collapse
-
.generate_download_url(attachment_id) ⇒ String
Generates a download URL for the given attachment.
-
.generate_upload_permission ⇒ Permission
Obtains the permission to upload a file manually.
-
.upload(file) ⇒ String
Uploads a file to S3.
Class Method Details
.generate_download_url(attachment_id) ⇒ String
Generates a download URL for the given attachment. The URL is valid for a couple of minutes. Hence, it is recommended to have such URLs generated on demand.
88 89 90 91 92 |
# File 'lib/crm/core/attachment_store.rb', line 88 def generate_download_url() response = Core::RestApi.instance.post("attachment_store/generate_download_url", {'attachment_id' => }) resolve_uri(response["url"]).to_s end |
.generate_upload_permission ⇒ Permission
Obtains the permission to upload a file manually. The permission is valid for a couple of minutes. Hence, it is recommended to have such permissions generated on demand.
76 77 78 79 80 |
# File 'lib/crm/core/attachment_store.rb', line 76 def perm = Core::RestApi.instance.post("attachment_store/generate_upload_permission", {}) uri = resolve_uri(perm["url"]) Permission.new(uri, uri.to_s, perm["fields"], perm["upload_id"]) end |
.upload(file) ⇒ String
Uploads a file to S3.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/crm/core/attachment_store.rb', line 98 def upload(file) = file_name = File.basename(file.path) upload_io = UploadIO.new(file, 'application/octet-stream', file_name) params = .fields.merge(file: upload_io) request = Net::HTTP::Post::Multipart.new(.uri, params) response = Core::ConnectionManager.new(.uri).request(request) if response.code.starts_with?('2') [.upload_id, file_name].compact.join('/') else raise Errors::ServerError, "File upload failed with code #{response.code}" end end |