/** * Save embedding information for a File, if applicable. * * Normally this event is called through File::saveNew() * * @param File $file The newly inserted File object. * @param array $redir_data lookup data eg from File_redirection::where() * @param string $given_url * * @return boolean success */ public function onEndFileSaveNew(File $file, array $redir_data, $given_url) { $fo = File_oembed::getKV('file_id', $file->id); if ($fo instanceof File_oembed) { common_log(LOG_WARNING, "Strangely, a File_oembed object exists for new file {$file_id}", __FILE__); return true; } if (isset($redir_data['oembed']['json']) && !empty($redir_data['oembed']['json'])) { File_oembed::saveNew($redir_data['oembed']['json'], $file->id); } elseif (isset($redir_data['type']) && ('text/html' === substr($redir_data['type'], 0, 9) || 'application/xhtml+xml' === substr($redir_data['type'], 0, 21))) { try { $oembed_data = File_oembed::_getOembed($given_url); if ($oembed_data === false) { throw new Exception('Did not get oEmbed data from URL'); } } catch (Exception $e) { return true; } File_oembed::saveNew($oembed_data, $file->id); } return true; }
/** * Save embedding information for this file, if applicable. * * Normally this won't need to be called manually, as File::saveNew() * takes care of it. * * @param array $redir_data lookup data eg from File_redirection::where() * @param string $given_url * @return boolean success */ public function saveOembed($redir_data, $given_url) { if (isset($redir_data['type']) && ('text/html' === substr($redir_data['type'], 0, 9) || 'application/xhtml+xml' === substr($redir_data['type'], 0, 21)) && ($oembed_data = File_oembed::_getOembed($given_url))) { $fo = File_oembed::staticGet('file_id', $this->id); if (empty($fo)) { File_oembed::saveNew($oembed_data, $this->id); return true; } else { common_log(LOG_WARNING, "Strangely, a File_oembed object exists for new file {$file_id}", __FILE__); } } return false; }
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::staticGet('url', $long_url); if (empty($file)) { // 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->id; if (!empty($redir_data['oembed']['json'])) { File_oembed::saveNew($redir_data['oembed']['json'], $file_id); } } else { if (is_string($redir_data)) { // The file is a known redirect target. $file = File::staticGet('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->id; } } } else { $file_id = $file->id; } $file_redir = File_redirection::staticGet('url', $short_url); if (empty($file_redir)) { $file_redir = new File_redirection(); $file_redir->url = $short_url; $file_redir->file_id = $file_id; $file_redir->insert(); } return $short_url; } return null; }
function _userMakeShort($long_url) { $short_url = common_shorten_url($long_url); if (!empty($short_url) && $short_url != $long_url) { $short_url = (string) $short_url; // store it $file = File::staticGet('url', $long_url); if (empty($file)) { // 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->id; if (!empty($redir_data['oembed']['json'])) { File_oembed::saveNew($redir_data['oembed']['json'], $file_id); } } else { if (is_string($redir_data)) { // The file is a known redirect target. $file = File::staticGet('url', $redir_data); $file_id = $file->id; } } } else { $file_id = $file->id; } $file_redir = File_redirection::staticGet('url', $short_url); if (empty($file_redir)) { $file_redir = new File_redirection(); $file_redir->url = $short_url; $file_redir->file_id = $file_id; $file_redir->insert(); } return $short_url; } return null; }
function saveNew($redir_data, $given_url) { $x = new File(); $x->url = $given_url; if (!empty($redir_data['protected'])) { $x->protected = $redir_data['protected']; } if (!empty($redir_data['title'])) { $x->title = $redir_data['title']; } if (!empty($redir_data['type'])) { $x->mimetype = $redir_data['type']; } if (!empty($redir_data['size'])) { $x->size = intval($redir_data['size']); } if (isset($redir_data['time']) && $redir_data['time'] > 0) { $x->date = intval($redir_data['time']); } $file_id = $x->insert(); if (isset($redir_data['type']) && 'text/html' === substr($redir_data['type'], 0, 9) && ($oembed_data = File_oembed::_getOembed($given_url)) && isset($oembed_data['json'])) { File_oembed::saveNew($oembed_data['json'], $file_id); } return $x; }
function _userMakeShort($long_url) { $short_url = common_shorten_url($long_url); if (!empty($short_url) && $short_url != $long_url) { $short_url = (string) $short_url; // store it $file = File::staticGet('url', $long_url); if (empty($file)) { $redir_data = File_redirection::where($long_url); $file = File::saveNew($redir_data, $long_url); $file_id = $file->id; if (!empty($redir_data['oembed']['json'])) { File_oembed::saveNew($redir_data['oembed']['json'], $file_id); } } else { $file_id = $file->id; } $file_redir = File_redirection::staticGet('url', $short_url); if (empty($file_redir)) { $file_redir = new File_redirection(); $file_redir->url = $short_url; $file_redir->file_id = $file_id; $file_redir->insert(); } return $short_url; } return null; }