-
- // Double check that we don't already have the resolved URL
- $r = self::where($redir_info['url'], false);
- if (!empty($r->file_id)) {
- return $r;
- }
-
- $redir->httpcode = $redir_info['code'];
- $redir->redirections = intval($redir_info['redirects']);
- $redir->file = new File();
- $redir->file->url = $redir_info ? $redir_info['url'] : $in_url;
- $redir->file->mimetype = $redir_info['type'];
- $redir->file->size = $redir_info['size'];
- $redir->file->date = $redir_info['time'];
- if ($redir_info['protected']) {
- $redir->file->protected = true;
+
+ // the last url in the redirection chain can actually be a redirect!
+ // this is the case with local /attachment/{file_id} links
+ // in that case we have the file id already
+ try {
+ $r = File_redirection::getByUrl($redir_info['url']);
+
+ $f = File::getKV('id',$r->file_id);
+
+ if($f instanceof File) {
+ $redir->file = $f;
+ $redir->redir_url = $f->url;
+ } else {
+ // Invalid entry in File_redirection, delete and run again
+ common_log(LOG_ERR, "Could not find File with id=".$r->file_id." referenced in File_redirection, deleting File_redirection entry and trying again...");
+ $r->delete();
+ return self::where($in_url);
+ }
+ } catch (NoResultException $e) {
+ // save the file now when we know that we don't have it in File_redirection
+ try {
+ $redir->file = File::saveNew($redir_info,$redir_info['url']);
+ } catch (ServerException $e) {
+ common_log(LOG_ERR, $e);
+ }