Sha256: 4e5be7b446aa69816562393be9579728a44f7ed3f43ba5b06764aa39c30e445d

Contents?: true

Size: 1.81 KB

Versions: 30

Compression:

Stored size: 1.81 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,
      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")
    this.xhr.setRequestHeader("X-CSRF-Token", getMetaValue("csrf-token"))
    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

30 entries across 30 versions & 2 rubygems

Version Path
activestorage-5.2.8.1 app/javascript/activestorage/blob_record.js
activestorage-5.2.8 app/javascript/activestorage/blob_record.js
activestorage-5.2.7.1 app/javascript/activestorage/blob_record.js
activestorage-5.2.7 app/javascript/activestorage/blob_record.js
activestorage-5.2.6.3 app/javascript/activestorage/blob_record.js
activestorage-5.2.6.2 app/javascript/activestorage/blob_record.js
activestorage-5.2.6.1 app/javascript/activestorage/blob_record.js
activestorage-5.2.6 app/javascript/activestorage/blob_record.js
activestorage-5.2.4.6 app/javascript/activestorage/blob_record.js
activestorage-5.2.5 app/javascript/activestorage/blob_record.js
activestorage-5.2.4.5 app/javascript/activestorage/blob_record.js
activestorage-5.2.4.4 app/javascript/activestorage/blob_record.js
activestorage-5.2.4.3 app/javascript/activestorage/blob_record.js
activestorage-5.2.4.2 app/javascript/activestorage/blob_record.js
activestorage-5.2.4.1 app/javascript/activestorage/blob_record.js
activestorage-5.2.4 app/javascript/activestorage/blob_record.js
activestorage-5.2.4.rc1 app/javascript/activestorage/blob_record.js
spiral_form-0.1.1 vendor/bundle/gems/activestorage-5.2.3/app/javascript/activestorage/blob_record.js
spiral_form-0.1.0 vendor/bundle/gems/activestorage-5.2.3/app/javascript/activestorage/blob_record.js
activestorage-5.2.3 app/javascript/activestorage/blob_record.js