]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Proxy.php
Merge pull request #12895 from HankG/mastodon-status-edit-fix-deleting-all-images
[friendica.git] / src / Module / Proxy.php
index 81d142a08a5523a9b906fca2db8ee3d72038e252..4d1e1c304b082e912ef54abe6d70756e5b96ceb6 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -23,7 +23,6 @@ namespace Friendica\Module;
 
 use Friendica\BaseModule;
 use Friendica\Core\Logger;
-use Friendica\Core\Session;
 use Friendica\Core\System;
 use Friendica\DI;
 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
@@ -75,7 +74,7 @@ class Proxy extends BaseModule
                        throw new \Friendica\Network\HTTPException\BadRequestException();
                }
 
-               if (!Session::getLocalUser()) {
+               if (!DI::userSession()->getLocalUserId()) {
                        Logger::debug('Redirecting not logged in user to original address', ['url' => $request['url']]);
                        System::externalRedirect($request['url']);
                }
@@ -84,16 +83,21 @@ class Proxy extends BaseModule
                $request['url'] = str_replace(' ', '+', $request['url']);
 
                // Fetch the content with the local user
-               $fetchResult = HTTPSignature::fetchRaw($request['url'], Session::getLocalUser(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]);
-               $img_str = $fetchResult->getBody();
-
-               if (!$fetchResult->isSuccess() || empty($img_str)) {
-                       Logger::notice('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]);
+               try {
+                       $fetchResult = HTTPSignature::fetchRaw($request['url'], DI::userSession()->getLocalUserId(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]);
+                       $img_str = $fetchResult->getBody();
+
+                       if (!$fetchResult->isSuccess() || empty($img_str)) {
+                               Logger::notice('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]);
+                               self::responseError();
+                               // stop.
+                       }
+               } catch (\Exception $exception) {
+                       Logger::notice('Error fetching image', ['image' => $request['url'], 'exception' => $exception]);
                        self::responseError();
-                       // stop.
                }
 
-               Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => Session::getLocalUser(), 'image' => $request['url']]);
+               Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => DI::userSession()->getLocalUserId(), 'image' => $request['url']]);
 
                $mime = Images::getMimeTypeByData($img_str);