Add tool to clean unused medias
This commit is contained in:
parent
626b59080e
commit
92146ba8e1
|
@ -151,4 +151,23 @@ namespace controllers\internals;
|
|||
|
||||
exit($success ? 0 : 1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete medias that are no longer usefull
|
||||
*/
|
||||
public function clean_unused_medias()
|
||||
{
|
||||
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8');
|
||||
$internal_media = new \controllers\internals\Media($bdd);
|
||||
|
||||
$medias = $internal_media->gets_unused();
|
||||
|
||||
foreach ($medias as $media)
|
||||
{
|
||||
$success = $internal_media->delete_for_user($media['id_user'], $media['id']);
|
||||
|
||||
echo ($success === false ? '[KO]' : '[OK]') . ' - ' . $media['path'] . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -174,7 +174,7 @@ namespace controllers\internals;
|
|||
* @param int $id_user : User id
|
||||
* @param int $id : Entry id
|
||||
*
|
||||
* @return int : Number of removed rows
|
||||
* @return mixed bool|int : False on error, else number of removed rows
|
||||
*/
|
||||
public function delete_for_user(int $id_user, int $id_media): bool
|
||||
{
|
||||
|
@ -184,7 +184,19 @@ namespace controllers\internals;
|
|||
return false;
|
||||
}
|
||||
|
||||
unlink($media['path']);
|
||||
//Delete file
|
||||
try
|
||||
{
|
||||
$filepath = PWD_DATA . '/' . $media['path'];
|
||||
if (file_exists($filepath))
|
||||
{
|
||||
unlink($filepath);
|
||||
}
|
||||
}
|
||||
catch (\Throwable $t)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->get_model()->delete_for_user($id_user, $id_media);
|
||||
}
|
||||
|
@ -225,6 +237,15 @@ namespace controllers\internals;
|
|||
return $this->get_model()->gets_for_received($id_received);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find medias that are not used
|
||||
* @return array
|
||||
*/
|
||||
public function gets_unused()
|
||||
{
|
||||
return $this->get_model()->gets_unused();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*/
|
||||
|
|
|
@ -245,6 +245,29 @@ namespace models;
|
|||
return $this->_delete('media_sended', $where);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all unused medias
|
||||
* @return array
|
||||
*/
|
||||
public function gets_unused ()
|
||||
{
|
||||
$query = '
|
||||
SELECT `media`.*
|
||||
FROM `media`
|
||||
LEFT JOIN `media_sended`
|
||||
ON `media`.id = `media_sended`.id_media
|
||||
LEFT JOIN `media_received`
|
||||
ON `media`.id = `media_received`.id_media
|
||||
LEFT JOIN `media_scheduled`
|
||||
ON `media`.id = `media_scheduled`.id_media
|
||||
WHERE `media_sended`.id IS NULL
|
||||
AND `media_received`.id IS NULL
|
||||
AND `media_scheduled`.id IS NULL
|
||||
';
|
||||
|
||||
return $this->_run_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue