From 3d807e4ea938b49d6d2938ae55ddc94a810850ab Mon Sep 17 00:00:00 2001
From: Joris Neuteboom <joris.neuteboom@iodigital.com>
Date: Tue, 18 Apr 2023 13:18:26 +0200
Subject: [PATCH] Check if the stream wrapper already has the file preloaded

---
 .../cloudinary_stream_wrapper.module                     | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/modules/cloudinary_stream_wrapper/cloudinary_stream_wrapper.module b/modules/cloudinary_stream_wrapper/cloudinary_stream_wrapper.module
index b74359b..b48c884 100644
--- a/modules/cloudinary_stream_wrapper/cloudinary_stream_wrapper.module
+++ b/modules/cloudinary_stream_wrapper/cloudinary_stream_wrapper.module
@@ -309,9 +309,14 @@ function cloudinary_stream_wrapper_delete_file($resource) {
  * Load file on Cloudinary.
  */
 function cloudinary_stream_wrapper_load_file($public_id, $options = []) {
-  $resource = [];
+  // First check if the file is already loaded.
+  $resource = cloudinary_stream_wrapper_resource_prepare($public_id);
 
-  // Try to load file resource on Cloudinary.
+  if ($resource) {
+    return $resource;
+  }
+
+  // If not, load it from Cloudinary API.
   try {
     $api = new AdminApi();
     $data = (array) $api->asset($public_id, $options);
-- 
2.37.5

