Sha256: 9b355778008459153c142c04c39f1b82345996f7b3cbd3b725ecb2398284857e

Contents?: true

Size: 1.16 KB

Versions: 158

Compression:

Stored size: 1.16 KB

Contents

import { FileChecksum } from "./file_checksum"
import { BlobRecord } from "./blob_record"
import { BlobUpload } from "./blob_upload"

let id = 0

export class DirectUpload {
  constructor(file, url, delegate) {
    this.id = ++id
    this.file = file
    this.url = url
    this.delegate = delegate
  }

  create(callback) {
    FileChecksum.create(this.file, (error, checksum) => {
      if (error) {
        callback(error)
        return
      }

      const blob = new BlobRecord(this.file, checksum, this.url)
      notify(this.delegate, "directUploadWillCreateBlobWithXHR", blob.xhr)

      blob.create(error => {
        if (error) {
          callback(error)
        } else {
          const upload = new BlobUpload(blob)
          notify(this.delegate, "directUploadWillStoreFileWithXHR", upload.xhr)
          upload.create(error => {
            if (error) {
              callback(error)
            } else {
              callback(null, blob.toJSON())
            }
          })
        }
      })
    })
  }
}

function notify(object, methodName, ...messages) {
  if (object && typeof object[methodName] == "function") {
    return object[methodName](...messages)
  }
}

Version data entries

158 entries across 154 versions & 19 rubygems

Version Path
activestorage-6.0.0 app/javascript/activestorage/direct_upload.js
spiral_form-0.1.1 vendor/bundle/gems/activestorage-5.2.3/app/javascript/activestorage/direct_upload.js
spiral_form-0.1.0 vendor/bundle/gems/activestorage-5.2.3/app/javascript/activestorage/direct_upload.js
activestorage-6.0.0.rc2 app/javascript/activestorage/direct_upload.js
activestorage-6.0.0.rc1 app/javascript/activestorage/direct_upload.js
activestorage-5.2.3 app/javascript/activestorage/direct_upload.js
activestorage-5.2.3.rc1 app/javascript/activestorage/direct_upload.js
activestorage-6.0.0.beta3 app/javascript/activestorage/direct_upload.js
activestorage-5.2.2.1 app/javascript/activestorage/direct_upload.js
activestorage-6.0.0.beta2 app/javascript/activestorage/direct_upload.js
activestorage-6.0.0.beta1 app/javascript/activestorage/direct_upload.js
activestorage-5.2.2 app/javascript/activestorage/direct_upload.js
activestorage-5.2.2.rc1 app/javascript/activestorage/direct_upload.js
activestorage-5.2.1.1 app/javascript/activestorage/direct_upload.js
activestorage-5.2.1 app/javascript/activestorage/direct_upload.js
activestorage-5.2.1.rc1 app/javascript/activestorage/direct_upload.js
activestorage-5.2.0 app/javascript/activestorage/direct_upload.js
activestorage-5.2.0.rc2 app/javascript/activestorage/direct_upload.js