Sha256: 6dbfc67e2b575fb0da6e0e6d7a2b10ecc4a27f3bf28ea84d38a4e8beb07015b6

Contents?: true

Size: 1.92 KB

Versions: 102

Compression:

Stored size: 1.92 KB

Contents

import { getMetaValue } from "./helpers"

export class BlobRecord {
  constructor(file, checksum, url) {
    this.file = file

    this.attributes = {
      filename: file.name,
      content_type: file.type || "application/octet-stream",
      byte_size: file.size,
      checksum: checksum
    }

    this.xhr = new XMLHttpRequest
    this.xhr.open("POST", url, true)
    this.xhr.responseType = "json"
    this.xhr.setRequestHeader("Content-Type", "application/json")
    this.xhr.setRequestHeader("Accept", "application/json")
    this.xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest")

    const csrfToken = getMetaValue("csrf-token")
    if (csrfToken != undefined) {
      this.xhr.setRequestHeader("X-CSRF-Token", csrfToken)
    }

    this.xhr.addEventListener("load", event => this.requestDidLoad(event))
    this.xhr.addEventListener("error", event => this.requestDidError(event))
  }

  get status() {
    return this.xhr.status
  }

  get response() {
    const { responseType, response } = this.xhr
    if (responseType == "json") {
      return response
    } else {
      // Shim for IE 11: https://connect.microsoft.com/IE/feedback/details/794808
      return JSON.parse(response)
    }
  }

  create(callback) {
    this.callback = callback
    this.xhr.send(JSON.stringify({ blob: this.attributes }))
  }

  requestDidLoad(event) {
    if (this.status >= 200 && this.status < 300) {
      const { response } = this
      const { direct_upload } = response
      delete response.direct_upload
      this.attributes = response
      this.directUploadData = direct_upload
      this.callback(null, this.toJSON())
    } else {
      this.requestDidError(event)
    }
  }

  requestDidError(event) {
    this.callback(`Error creating Blob for "${this.file.name}". Status: ${this.status}`)
  }

  toJSON() {
    const result = {}
    for (const key in this.attributes) {
      result[key] = this.attributes[key]
    }
    return result
  }
}

Version data entries

102 entries across 98 versions & 10 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/activestorage-7.0.8.7/app/javascript/activestorage/blob_record.js
activestorage-7.0.8.7 app/javascript/activestorage/blob_record.js
activestorage-7.0.8.6 app/javascript/activestorage/blob_record.js
activestorage-6.1.7.10 app/javascript/activestorage/blob_record.js
activestorage-6.1.7.9 app/javascript/activestorage/blob_record.js
activestorage-7.0.8.5 app/javascript/activestorage/blob_record.js
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activestorage-7.0.8.4/app/javascript/activestorage/blob_record.js
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activestorage-7.0.5.1/app/javascript/activestorage/blob_record.js
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activestorage-7.0.5.1/app/javascript/activestorage/blob_record.js
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activestorage-7.0.5.1/app/javascript/activestorage/blob_record.js
activestorage-7.0.8.4 app/javascript/activestorage/blob_record.js
activestorage-6.1.7.8 app/javascript/activestorage/blob_record.js
optimacms-0.1.61 spec/dummy/node_modules/@rails/activestorage/src/blob_record.js
activestorage-7.0.8.1 app/javascript/activestorage/blob_record.js
activestorage-6.1.7.7 app/javascript/activestorage/blob_record.js
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-7.0.3.1/app/javascript/activestorage/blob_record.js
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-7.0.2.3/app/javascript/activestorage/blob_record.js
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-6.1.6.1/app/javascript/activestorage/blob_record.js
activestorage-7.0.8 app/javascript/activestorage/blob_record.js
activestorage-7.0.7.2 app/javascript/activestorage/blob_record.js