Refactor on File::processNew

The code was so involved there was even a comment asking for a refactor.

Now, File_redirection::where always returns a nice File_redirection
object instead of an array or string or nothing.  The object is
either one which already existed or else a new, unsaved object.

Instead of duplicating "does it exist" checks everywhere, do it in
File_redirection::where.  You either get what exists or something to save.

An unsaved File_redirection may be paired with an unsaved File.
You will want to save the File first (using ->saveFile()) and put the
id in File_redirection#file_id before saving.
This commit is contained in:
Stephen Paul Weber
2015-11-02 05:15:08 +00:00
parent 2c8536dbf0
commit a9b1b60a97
6 changed files with 96 additions and 143 deletions

View File

@@ -90,12 +90,10 @@ class OembedPlugin extends Plugin
* 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)
public function onEndFileSaveNew(File $file)
{
$fo = File_oembed::getKV('file_id', $file->id);
if ($fo instanceof File_oembed) {
@@ -103,15 +101,12 @@ class OembedPlugin extends Plugin
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)))) {
if (isset($file->mimetype)
&& (('text/html' === substr($file->mimetype, 0, 9)
|| 'application/xhtml+xml' === substr($file->mimetype, 0, 21)))) {
try {
$oembed_data = File_oembed::_getOembed($given_url);
$oembed_data = File_oembed::_getOembed($file->url);
if ($oembed_data === false) {
throw new Exception('Did not get oEmbed data from URL');
}