From f02d32b718de732181d0f73beed2b7fdb77b1cbd Mon Sep 17 00:00:00 2001
From: Mikael Nordfeldth <mmn@hethane.se>
Date: Thu, 7 Jul 2016 00:44:50 +0200
Subject: [PATCH] Reworked File->getUrl to throw exception

In case you require a local URL and one can't be generated, throw
FileNotStoredLocallyException(File $file)
---
 classes/File.php                      | 18 ++++++++++++++----
 lib/filenotstoredlocallyexception.php | 15 +++++++++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)
 create mode 100644 lib/filenotstoredlocallyexception.php

diff --git a/classes/File.php b/classes/File.php
index 6197539d94..7bd7da27ba 100644
--- a/classes/File.php
+++ b/classes/File.php
@@ -529,13 +529,23 @@ class File extends Managed_DataObject
         return common_local_url('attachment', array('attachment'=>$this->getID()));
     }
 
-    public function getUrl($prefer_local=true)
+    /**
+     *  @param  mixed   $use_local  true means require local, null means prefer local, false means use whatever is stored
+     */
+    public function getUrl($use_local=null)
     {
-        if ($prefer_local && !empty($this->filename)) {
-            // A locally stored file, so let's generate a URL for our instance.
-            return self::url($this->getFilename());
+        if ($use_local !== false) {
+            if (is_string($this->filename) || !empty($this->filename)) {
+                // A locally stored file, so let's generate a URL for our instance.
+                return self::url($this->getFilename());
+            }
+            if ($use_local) {
+                // if the file wasn't stored locally (has filename) and we require a local URL
+                throw new FileNotStoredLocallyException($this);
+            }
         }
 
+
         // No local filename available, return the URL we have stored
         return $this->url;
     }
diff --git a/lib/filenotstoredlocallyexception.php b/lib/filenotstoredlocallyexception.php
new file mode 100644
index 0000000000..2d2ad8fc37
--- /dev/null
+++ b/lib/filenotstoredlocallyexception.php
@@ -0,0 +1,15 @@
+<?php
+
+if (!defined('GNUSOCIAL')) { exit(1); }
+
+class FileNotStoredLocallyException extends ServerException
+{
+    public $file = null;
+
+    public function __construct(File $file)
+    {
+        $this->file = $file;
+        common_debug('Requested local URL for a file that is not stored locally with id=='._ve($this->file->getID()));
+        parent::__construct(_('Requested local URL for a file that is not stored locally.'));
+    }
+}
-- 
2.39.5