From 5585f89e9a7607cf0d92c70337c8286e16fc0523 Mon Sep 17 00:00:00 2001
From: Don Richards <2738244+DonRichards@users.noreply.github.com>
Date: Thu, 16 Feb 2023 09:56:33 -0500
Subject: [PATCH 1/5] Update MediaSourceService.php

---
 src/MediaSource/MediaSourceService.php | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/MediaSource/MediaSourceService.php b/src/MediaSource/MediaSourceService.php
index 53378985..5e90e140 100644
--- a/src/MediaSource/MediaSourceService.php
+++ b/src/MediaSource/MediaSourceService.php
@@ -289,8 +289,18 @@ class MediaSourceService {
       }
 
       $directory = $this->fileSystem->dirname($content_location);
+      
+      // Check if the directory is writable.
+      if (!is_writable(dirname($directory))) {
+        $parent_directory = dirname($directory);
+        $error_current_user = shell_exec('whoami');
+        $sanitized_current_user = str_replace(array("\n", "\r"), '', $error_current_user);
+        throw new HttpException(500, "The user '$sanitized_current_user' does not have permissions to write to: '$parent_directory' . Error");
+      }
+
+
       if (!$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
-        throw new HttpException(500, "The destination directory does not exist, could not be created, or is not writable");
+        throw new HttpException(500, "The destination directory does not exist, could not be created, or is not writable: $directory");
       }
 
       // Copy over the file content.

From 80391bc3724d54b13a9cb167cd5b3d14fc671015 Mon Sep 17 00:00:00 2001
From: Don Richards <2738244+DonRichards@users.noreply.github.com>
Date: Mon, 20 Feb 2023 13:34:07 -0500
Subject: [PATCH 2/5] Moved into existing check

---
 src/MediaSource/MediaSourceService.php | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/src/MediaSource/MediaSourceService.php b/src/MediaSource/MediaSourceService.php
index 5e90e140..b554cf68 100644
--- a/src/MediaSource/MediaSourceService.php
+++ b/src/MediaSource/MediaSourceService.php
@@ -290,17 +290,11 @@ class MediaSourceService {
 
       $directory = $this->fileSystem->dirname($content_location);
       
-      // Check if the directory is writable.
-      if (!is_writable(dirname($directory))) {
+      if (!$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
         $parent_directory = dirname($directory);
         $error_current_user = shell_exec('whoami');
         $sanitized_current_user = str_replace(array("\n", "\r"), '', $error_current_user);
-        throw new HttpException(500, "The user '$sanitized_current_user' does not have permissions to write to: '$parent_directory' . Error");
-      }
-
-
-      if (!$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
-        throw new HttpException(500, "The destination directory does not exist, could not be created, or is not writable: $directory");
+        throw new HttpException(500, "The destination directory does not exist, could not be created, or is not writable by $sanitized_current_user: $directory");
       }
 
       // Copy over the file content.

From 9bd4e8c5c0fb7167874cb2078d55430db53fe00c Mon Sep 17 00:00:00 2001
From: Don Richards <2738244+DonRichards@users.noreply.github.com>
Date: Mon, 20 Feb 2023 14:24:40 -0500
Subject: [PATCH 3/5] Update MediaSourceService.php

---
 src/MediaSource/MediaSourceService.php | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/MediaSource/MediaSourceService.php b/src/MediaSource/MediaSourceService.php
index b554cf68..befab65a 100644
--- a/src/MediaSource/MediaSourceService.php
+++ b/src/MediaSource/MediaSourceService.php
@@ -294,7 +294,9 @@ class MediaSourceService {
         $parent_directory = dirname($directory);
         $error_current_user = shell_exec('whoami');
         $sanitized_current_user = str_replace(array("\n", "\r"), '', $error_current_user);
-        throw new HttpException(500, "The destination directory does not exist, could not be created, or is not writable by $sanitized_current_user: $directory");
+        $error_message = "The destination directory does not exist, could not be created, or is not writable by $sanitized_current_user: $directory";
+        watchdog('media_source_service', $error_message, [], WATCHDOG_ERROR);
+        throw new HttpException(500, "An error occurred while processing your request. Please contact the website administrator.");
       }
 
       // Copy over the file content.

From c1ace5a2da58592b91dce7c83b97e1f4a693b5d7 Mon Sep 17 00:00:00 2001
From: Don Richards <2738244+DonRichards@users.noreply.github.com>
Date: Thu, 23 Feb 2023 16:13:56 -0500
Subject: [PATCH 4/5] Replace the watchdog output

---
 src/MediaSource/MediaSourceService.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/MediaSource/MediaSourceService.php b/src/MediaSource/MediaSourceService.php
index befab65a..4df7f848 100644
--- a/src/MediaSource/MediaSourceService.php
+++ b/src/MediaSource/MediaSourceService.php
@@ -295,7 +295,7 @@ class MediaSourceService {
         $error_current_user = shell_exec('whoami');
         $sanitized_current_user = str_replace(array("\n", "\r"), '', $error_current_user);
         $error_message = "The destination directory does not exist, could not be created, or is not writable by $sanitized_current_user: $directory";
-        watchdog('media_source_service', $error_message, [], WATCHDOG_ERROR);
+        $this->logger->error($error_message);
         throw new HttpException(500, "An error occurred while processing your request. Please contact the website administrator.");
       }
 

From e4639be4098cce12b0e2a2a1a2d40596b010fde7 Mon Sep 17 00:00:00 2001
From: Don Richards <2738244+DonRichards@users.noreply.github.com>
Date: Thu, 23 Feb 2023 16:23:41 -0500
Subject: [PATCH 5/5] Fix phpcs errors

---
 src/MediaSource/MediaSourceService.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/MediaSource/MediaSourceService.php b/src/MediaSource/MediaSourceService.php
index 4df7f848..fb189794 100644
--- a/src/MediaSource/MediaSourceService.php
+++ b/src/MediaSource/MediaSourceService.php
@@ -289,11 +289,11 @@ class MediaSourceService {
       }
 
       $directory = $this->fileSystem->dirname($content_location);
-      
+
       if (!$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
         $parent_directory = dirname($directory);
         $error_current_user = shell_exec('whoami');
-        $sanitized_current_user = str_replace(array("\n", "\r"), '', $error_current_user);
+        $sanitized_current_user = str_replace(["\n", "\r"], '', $error_current_user);
         $error_message = "The destination directory does not exist, could not be created, or is not writable by $sanitized_current_user: $directory";
         $this->logger->error($error_message);
         throw new HttpException(500, "An error occurred while processing your request. Please contact the website administrator.");