Sha256: 9b355778008459153c142c04c39f1b82345996f7b3cbd3b725ecb2398284857e

Contents?: true

Size: 1.16 KB

Versions: 156

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

156 entries across 152 versions & 18 rubygems

Version Path
activestorage-6.1.5 app/javascript/activestorage/direct_upload.js
activestorage-7.0.2.3 app/javascript/activestorage/direct_upload.js
activestorage-6.1.4.7 app/javascript/activestorage/direct_upload.js
activestorage-6.0.4.7 app/javascript/activestorage/direct_upload.js
activestorage-5.2.6.3 app/javascript/activestorage/direct_upload.js
activestorage-7.0.2.2 app/javascript/activestorage/direct_upload.js
activestorage-6.1.4.6 app/javascript/activestorage/direct_upload.js
activestorage-6.0.4.6 app/javascript/activestorage/direct_upload.js
activestorage-5.2.6.2 app/javascript/activestorage/direct_upload.js
activestorage-5.2.6.1 app/javascript/activestorage/direct_upload.js
activestorage-6.0.4.5 app/javascript/activestorage/direct_upload.js
activestorage-6.1.4.5 app/javascript/activestorage/direct_upload.js
activestorage-7.0.2.1 app/javascript/activestorage/direct_upload.js
activestorage-7.0.2 app/javascript/activestorage/direct_upload.js
activestorage-6.1.4.4 app/javascript/activestorage/direct_upload.js
activestorage-6.0.4.4 app/javascript/activestorage/direct_upload.js
activestorage-6.1.4.3 app/javascript/activestorage/direct_upload.js
activestorage-6.0.4.3 app/javascript/activestorage/direct_upload.js
activestorage-6.0.4.2 app/javascript/activestorage/direct_upload.js
activestorage-6.1.4.2 app/javascript/activestorage/direct_upload.js