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-7.0.8.6 app/javascript/activestorage/direct_upload.js
activestorage-6.1.7.10 app/javascript/activestorage/direct_upload.js
activestorage-6.1.7.9 app/javascript/activestorage/direct_upload.js
activestorage-7.0.8.5 app/javascript/activestorage/direct_upload.js
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activestorage-7.0.8.4/app/javascript/activestorage/direct_upload.js
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activestorage-7.0.5.1/app/javascript/activestorage/direct_upload.js
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activestorage-7.0.5.1/app/javascript/activestorage/direct_upload.js
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activestorage-7.0.5.1/app/javascript/activestorage/direct_upload.js
activestorage-7.0.8.4 app/javascript/activestorage/direct_upload.js
activestorage-6.1.7.8 app/javascript/activestorage/direct_upload.js
optimacms-0.1.61 spec/dummy/node_modules/@rails/activestorage/src/direct_upload.js
activestorage-7.0.8.1 app/javascript/activestorage/direct_upload.js
activestorage-6.1.7.7 app/javascript/activestorage/direct_upload.js
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-7.0.3.1/app/javascript/activestorage/direct_upload.js
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-7.0.2.3/app/javascript/activestorage/direct_upload.js
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activestorage-6.1.6.1/app/javascript/activestorage/direct_upload.js
activestorage-7.0.8 app/javascript/activestorage/direct_upload.js
activestorage-7.0.7.2 app/javascript/activestorage/direct_upload.js
activestorage-6.1.7.6 app/javascript/activestorage/direct_upload.js
activestorage-7.0.7.1 app/javascript/activestorage/direct_upload.js