}
Event::handle('EndFileSaveNew', array($file, $redir_data, $given_url));
+ assert ($file instanceof File);
return $file;
}
*
* @return mixed File on success, -1 on some errors
*
- * @throws ServerException on some errors
+ * @throws ServerException on failure
*/
- public function processNew($given_url, $notice_id=null, $followRedirects=true) {
- if (empty($given_url)) return -1; // error, no url to process
+ public static function processNew($given_url, $notice_id=null, $followRedirects=true) {
+ if (empty($given_url)) {
+ throw new ServerException('No given URL to process');
+ }
+
$given_url = File_redirection::_canonUrl($given_url);
- if (empty($given_url)) return -1; // error, no url to process
+ if (empty($given_url)) {
+ throw new ServerException('No canonical URL from given URL to process');
+ }
+
$file = File::getKV('url', $given_url);
- if (empty($file)) {
+ if (!$file instanceof File) {
+ // First check if we have a lookup trace for this URL already
$file_redir = File_redirection::getKV('url', $given_url);
- if (empty($file_redir)) {
+ if ($file_redir instanceof File_redirection) {
+ $file = File::getKV('id', $file_redir->file_id);
+ if (!$file instanceof File) {
+ // File did not exist, let's clean up the File_redirection entry
+ $file_redir->delete();
+ }
+ }
+
+ // If we still don't have a File object, let's create one now!
+ if (!$file instanceof File) {
// @fixme for new URLs this also looks up non-redirect data
// such as target content type, size, etc, which we need
// for File::saveNew(); so we call it even if not following
// TRANS: Server exception thrown when a URL cannot be processed.
throw new ServerException(sprintf(_("Cannot process URL '%s'"), $given_url));
}
+
// TODO: max field length
if ($redir_url === $given_url || strlen($redir_url) > 255 || !$followRedirects) {
- $x = File::saveNew($redir_data, $given_url);
- $file_id = $x->id;
+ // Save the File object based on our lookup trace
+ $file = File::saveNew($redir_data, $given_url);
} else {
// This seems kind of messed up... for now skipping this part
// if we're already under a redirect, so we don't go into
//
// Seen in the wild with clojure.org, which redirects through
// wikispaces for auth and appends session data in the URL params.
- $x = File::processNew($redir_url, $notice_id, /*followRedirects*/false);
- $file_id = $x->id;
- File_redirection::saveNew($redir_data, $file_id, $given_url);
+ $file = self::processNew($redir_url, $notice_id, /*followRedirects*/false);
+ File_redirection::saveNew($redir_data, $file->id, $given_url);
}
- } else {
- $file_id = $file_redir->file_id;
}
- } else {
- $file_id = $file->id;
- $x = $file;
- }
- if (empty($x)) {
- $x = File::getKV('id', $file_id);
- if (empty($x)) {
- // @todo FIXME: This could possibly be a clearer message :)
- // TRANS: Server exception thrown when... Robin thinks something is impossible!
- throw new ServerException(_('Robin thinks something is impossible.'));
+ if (!$file instanceof File) {
+ // This should only happen if File::saveNew somehow did not return a File object,
+ // though we have an assert for that in case the event there might've gone wrong.
+ // If anything else goes wrong, there should've been an exception thrown.
+ throw new ServerException('URL processing failed without new File object');
}
}
if (!empty($notice_id)) {
- File_to_post::processNew($file_id, $notice_id);
+ File_to_post::processNew($file->id, $notice_id);
}
- return $x;
+ return $file;
}
public static function respectsQuota(Profile $scoped, $fileSize) {
if (common_config('attachments', 'process_links')) {
// @fixme validation?
foreach (array_unique($urls) as $url) {
- File::processNew($url, $this->id);
+ try {
+ File::processNew($url, $this->id);
+ } catch (ServerException $e) {
+ // Could not save URL. Log it?
+ }
}
}
}
* @private callback
*/
function saveUrl($url, $notice_id) {
- File::processNew($url, $notice_id);
+ try {
+ File::processNew($url, $notice_id);
+ } catch (ServerException $e) {
+ // Could not save URL. Log it?
+ }
}
static function checkDupes($profile_id, $content) {
if (!$f instanceof File) {
if (common_config('attachments', 'process_links')) {
// XXX: this writes to the database. :<
- $f = File::processNew($longurl);
+ try {
+ $f = File::processNew($longurl);
+ } catch (ServerException $e) {
+ $f = null;
+ }
}
}
if (Event::handle('StartShortenUrl',
array($long_url, $shortenerName, &$shortenedUrl))) {
if ($shortenerName == 'internal') {
- $f = File::processNew($long_url);
- if (empty($f)) {
- return $long_url;
- } else {
- $shortenedUrl = common_local_url('redirecturl',
- array('id' => $f->id));
+ try {
+ $f = File::processNew($long_url);
+ $shortenedUrl = common_local_url('redirecturl', array('id' => $f->id));
if ((mb_strlen($shortenedUrl) < mb_strlen($long_url)) || $force) {
return $shortenedUrl;
} else {
return $long_url;
}
+ } catch (ServerException $e) {
+ return $long_url;
}
} else {
return $long_url;