Sha256: a6ec419c9469e0c5e998ef76b395dfa83ded7868c9f3195f4c088cc9b5f2953e

Contents?: true

Size: 1.17 KB

Versions: 139

Compression:

Stored size: 1.17 KB

Contents

import { DirectUpload } from "@rails/activestorage"

export class AttachmentUpload {
  constructor(attachment, element) {
    this.attachment = attachment
    this.element = element
    this.directUpload = new DirectUpload(attachment.file, this.directUploadUrl, this)
  }

  start() {
    this.directUpload.create(this.directUploadDidComplete.bind(this))
  }

  directUploadWillStoreFileWithXHR(xhr) {
    xhr.upload.addEventListener("progress", event => {
      const progress = event.loaded / event.total * 100
      this.attachment.setUploadProgress(progress)
    })
  }

  directUploadDidComplete(error, attributes) {
    if (error) {
      throw new Error(`Direct upload failed: ${error}`)
    }

    this.attachment.setAttributes({
      sgid: attributes.attachable_sgid,
      url: this.createBlobUrl(attributes.signed_id, attributes.filename)
    })
  }

  createBlobUrl(signedId, filename) {
    return this.blobUrlTemplate
      .replace(":signed_id", signedId)
      .replace(":filename", encodeURIComponent(filename))
  }

  get directUploadUrl() {
    return this.element.dataset.directUploadUrl
  }

  get blobUrlTemplate() {
    return this.element.dataset.blobUrlTemplate
  }
}

Version data entries

139 entries across 134 versions & 11 rubygems

Version Path
actiontext-7.2.2 app/javascript/actiontext/attachment_upload.js
actiontext-7.1.5 app/javascript/actiontext/attachment_upload.js
actiontext-7.2.1.2 app/javascript/actiontext/attachment_upload.js
actiontext-7.1.4.2 app/javascript/actiontext/attachment_upload.js
actiontext-7.0.8.6 app/javascript/actiontext/attachment_upload.js
actiontext-6.1.7.10 app/javascript/actiontext/attachment_upload.js
actiontext-6.1.7.9 app/javascript/actiontext/attachment_upload.js
actiontext-7.2.1.1 app/javascript/actiontext/attachment_upload.js
actiontext-7.1.4.1 app/javascript/actiontext/attachment_upload.js
actiontext-7.0.8.5 app/javascript/actiontext/attachment_upload.js
actiontext-7.1.4 app/javascript/actiontext/attachment_upload.js
actiontext-7.2.1 app/javascript/actiontext/attachment_upload.js
actiontext-7.2.0 app/javascript/actiontext/attachment_upload.js
actiontext-7.2.0.rc1 app/javascript/actiontext/attachment_upload.js
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actiontext-7.0.8.4/app/javascript/actiontext/attachment_upload.js
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actiontext-7.1.3.4/app/javascript/actiontext/attachment_upload.js
actiontext-7.2.0.beta3 app/javascript/actiontext/attachment_upload.js
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/app/javascript/actiontext/attachment_upload.js
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/app/javascript/actiontext/attachment_upload.js
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actiontext-7.0.5.1/app/javascript/actiontext/attachment_upload.js