blob.cljs 747 B

1234567891011121314151617181920212223242526272829
  1. (ns frontend.blob)
  2. (defn- decode
  3. "Decodes the data portion of a data url from base64"
  4. [[media-type data]]
  5. [media-type (js/atob data)])
  6. (defn- uint8
  7. "Converts a base64 decoded data string to a Uint8Array"
  8. [[media-type data]]
  9. (->> (map #(.charCodeAt %1) data)
  10. js/Uint8Array.
  11. (vector media-type)))
  12. (defn- make-blob
  13. "Creates a JS Blob object from a media type and a Uint8Array"
  14. [[media-type uint8]]
  15. (js/Blob. (array uint8) (js-obj "type" media-type)))
  16. (defn blob
  17. "Converts a data-url into a JS Blob. This is useful for uploading
  18. image data from JavaScript."
  19. [data-url]
  20. {:pre [(string? data-url)]}
  21. (-> (re-find #"^data:([^;]+);base64,(.*)$" data-url)
  22. rest
  23. decode
  24. uint8
  25. make-blob))