File_redirection should get URLs properly

This commit is contained in:
Mikael Nordfeldth 2015-12-27 21:36:23 +01:00
parent bd00ef839d
commit 4bc0b374bc
1 changed files with 9 additions and 6 deletions

View File

@ -262,8 +262,9 @@ class File_redirection extends Managed_DataObject
if (!empty($short_url) && $short_url != $long_url) { if (!empty($short_url) && $short_url != $long_url) {
$short_url = (string)$short_url; $short_url = (string)$short_url;
// store it // store it
$file = File::getKV('url', $long_url); try {
if (!$file instanceof File) { $file = File::getByUrl($long_url);
} catch (NoResultException $e) {
// Check if the target URL is itself a redirect... // Check if the target URL is itself a redirect...
$redir = File_redirection::where($long_url); $redir = File_redirection::where($long_url);
$file = $redir->getFile(); $file = $redir->getFile();
@ -271,12 +272,14 @@ class File_redirection extends Managed_DataObject
$file->saveFile(); $file->saveFile();
} }
} }
$file_redir = File_redirection::getKV('url', $short_url); // Now we definitely have a File object in $file
if (!$file_redir instanceof File_redirection) { try {
$file_redir = new File_redirection; $file_redir = File_redirection::getByUrl($short_url);
} catch (NoResultException $e) {
$file_redir = new File_redirection();
$file_redir->urlhash = File::hashurl($short_url); $file_redir->urlhash = File::hashurl($short_url);
$file_redir->url = $short_url; $file_redir->url = $short_url;
$file_redir->file_id = $file->id; $file_redir->file_id = $file->getID();
$file_redir->insert(); $file_redir->insert();
} }
return $short_url; return $short_url;