diff --git a/modules/cloudinary_stream_wrapper/src/StreamWrapper/CloudinaryStreamWrapper.php b/modules/cloudinary_stream_wrapper/src/StreamWrapper/CloudinaryStreamWrapper.php
index 4d70392..8cafeac 100644
--- a/modules/cloudinary_stream_wrapper/src/StreamWrapper/CloudinaryStreamWrapper.php
+++ b/modules/cloudinary_stream_wrapper/src/StreamWrapper/CloudinaryStreamWrapper.php
@@ -738,20 +738,28 @@ class CloudinaryStreamWrapper implements StreamWrapperInterface {
    * @see http://php.net/manual/streamwrapper.stream-flush.php
    */
   public function stream_flush() {
+    $uri = $this->uri;
     if ($this->streamWrite) {
-      $public_id = $this->getPublicId($this->uri);
-      $base64_data = 'data:' . $this->getMimeType($this->uri) . ';base64,' . base64_encode($this->streamData);
+      $public_id = $this->getPublicId($uri);
+      $base64_data = 'data:' . $this->getMimeType($uri) . ';base64,' . base64_encode($this->streamData);
       $dirname = dirname($public_id);
       if ($dirname == '.') {
         $dirname = '';
       }
 
+      // Check for the resource type.
+      $this->resourceType = cloudinary_stream_wrapper_is_image($uri) ? AssetType::IMAGE : AssetType::RAW;
+
       $options = array(
         'public_id' => $public_id,
         AssetType::KEY => $this->resourceType,
         'tags' => CLOUDINARY_STREAM_WRAPPER_FOLDER_TAG_PREFIX . $dirname,
       );
 
+      if (isset(pathinfo($uri)['extension'])) {
+        $options['format'] = pathinfo($uri)['extension'];
+      }
+
       if (cloudinary_stream_wrapper_create_file($base64_data, $options)) {
         return TRUE;
       }
