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

@@ -30,29 +30,27 @@ class StoreRemoteMediaPlugin 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
* @param File $file The abount-to-be-inserted File object.
*
* @return boolean success
*/
public function onStartFileSaveNew(array &$redir_data, $given_url)
public function onStartFileSaveNew(File &$file)
{
// save given URL as title if it's a media file this plugin understands
// which will make it shown in the AttachmentList widgets
if (isset($redir_data['title']) && strlen($redir_data['title']>0)) {
if (isset($file->title) && strlen($file->title)>0) {
// Title is already set
return true;
}
if (!isset($redir_data['type'])) {
if (!isset($file->mimetype)) {
// Unknown mimetype, it's not our job to figure out what it is.
return true;
}
switch (common_get_mime_media($redir_data['type'])) {
switch (common_get_mime_media($file->mimetype)) {
case 'image':
// Just to set something for now at least...
$redir_data['title'] = $given_url;
$file->title = $file->mimetype;
break;
}