From 6a4470912f534992e05412cb53adbeeac3978356 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Fri, 26 Feb 2016 14:10:32 +0100 Subject: [PATCH] Fiddling with merge request #98 to use internal routing functions --- classes/File.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/classes/File.php b/classes/File.php index 4e51be4938..8d9ecf5195 100644 --- a/classes/File.php +++ b/classes/File.php @@ -108,13 +108,20 @@ class File extends Managed_DataObject // if the given url is an local attachment url and the id already exists, don't // save a new file record. This should never happen, but let's make it foolproof - $attachment_path = common_path('attachment/'); - if (mb_strpos($given_url,$attachment_path) === 0) { - $possible_file_id = mb_substr($given_url, mb_strlen($attachment_path)); - if(is_numeric($possible_file_id)) { - $file = File::getKV('id',$possible_file_id); - if($file instanceof File) { + // FIXME: how about attachments servers? + $u = parse_url($given_url); + if (isset($u['host']) && $u['host'] === common_config('site', 'server')) { + $r = Router::get(); + $args = $r->map(mb_substr($u['path'])); + if ($args['action'] === 'attachment') { + try { + // $args['attachment'] should always be set if action===attachment, given our routing rules + $file = File::getByID($args['attachment']); return $file; + } catch (EmptyIdException $e) { + // ...but $args['attachment'] can also be 0... + } catch (NoResultException $e) { + // apparently this link goes to us, but is _not_ an existing attachment (File) ID? } } }