/** * Save embedding info for a new file. * * @param object $data Services_oEmbed_Object_* * @param int $file_id */ public static function saveNew($data, $file_id) { $file_oembed = new File_oembed(); $file_oembed->file_id = $file_id; if (!isset($data->version)) { common_debug('DEBUGGING oEmbed: data->version undefined in variable $data: ' . var_export($data, true)); } $file_oembed->version = $data->version; $file_oembed->type = $data->type; if (!empty($data->provider_name)) { $file_oembed->provider = $data->provider_name; } if (!empty($data->provider)) { $file_oembed->provider = $data->provider; } if (!empty($data->provider_url)) { $file_oembed->provider_url = $data->provider_url; } if (!empty($data->width)) { $file_oembed->width = intval($data->width); } if (!empty($data->height)) { $file_oembed->height = intval($data->height); } if (!empty($data->html)) { $file_oembed->html = $data->html; } if (!empty($data->title)) { $file_oembed->title = $data->title; } if (!empty($data->author_name)) { $file_oembed->author_name = $data->author_name; } if (!empty($data->author_url)) { $file_oembed->author_url = $data->author_url; } if (!empty($data->url)) { $file_oembed->url = $data->url; $given_url = File_redirection::_canonUrl($file_oembed->url); if (!empty($given_url)) { $file = File::getKV('url', $given_url); if ($file instanceof File) { $file_oembed->mimetype = $file->mimetype; } else { $file_redir = File_redirection::getKV('url', $given_url); if (empty($file_redir)) { $redir_data = File_redirection::where($given_url); $file_oembed->mimetype = $redir_data['type']; } else { $file_id = $file_redir->file_id; } } } } $file_oembed->insert(); if (!empty($data->thumbnail_url) || $data->type == 'photo') { $ft = File_thumbnail::getKV('file_id', $file_id); if ($ft instanceof File_thumbnail) { common_log(LOG_WARNING, "Strangely, a File_thumbnail object exists for new file {$file_id}", __FILE__); } else { File_thumbnail::saveNew($data, $file_id); } } }
static function _userMakeShort($long_url, User $user = null, $force = false) { $short_url = common_shorten_url($long_url, $user, $force); if (!empty($short_url) && $short_url != $long_url) { $short_url = (string) $short_url; // store it $file = File::getKV('url', $long_url); if ($file instanceof File) { $file_id = $file->getID(); } else { // Check if the target URL is itself a redirect... $redir_data = File_redirection::where($long_url); if (is_array($redir_data)) { // We haven't seen the target URL before. // Save file and embedding data about it! $file = File::saveNew($redir_data, $long_url); $file_id = $file->getID(); } else { if (is_string($redir_data)) { // The file is a known redirect target. $file = File::getKV('url', $redir_data); if (empty($file)) { // @fixme should we save a new one? // this case was triggering sometimes for redirects // with unresolvable targets; found while fixing // "can't linkify" bugs with shortened links to // SSL sites with cert issues. return null; } $file_id = $file->getID(); } } } $file_redir = File_redirection::getKV('url', $short_url); if (!$file_redir instanceof File_redirection) { $file_redir = new File_redirection(); $file_redir->urlhash = File::hashurl($short_url); $file_redir->url = $short_url; $file_redir->file_id = $file_id; $file_redir->insert(); } return $short_url; } return null; }