|
|
|
@ -1,6 +1,25 @@ |
|
|
|
<?php |
|
|
|
|
|
|
|
class Download { |
|
|
|
/** |
|
|
|
* Retourne les infos sur le prochain fichier a telecharger |
|
|
|
* |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
static function findNextJob() |
|
|
|
{ |
|
|
|
$db = DbMongo::get(); |
|
|
|
$coll = $db->download; |
|
|
|
// TODO : en plus faudrait rajouter une colonne histo pour avoir l'histo de ce fichier
|
|
|
|
$retval = $coll->findAndModify( |
|
|
|
['state' => 'added'], |
|
|
|
['$set' => ['state' => 'inprogress']], |
|
|
|
null, |
|
|
|
['sort' => ["timestamp" => 1]] |
|
|
|
); |
|
|
|
return $retval; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Telecharge depuis $url vers $out. |
|
|
|
* |
|
|
|
@ -29,6 +48,7 @@ class Download { |
|
|
|
$retFilename = $matches[1]; |
|
|
|
unset($matches); |
|
|
|
|
|
|
|
// TODO : utiliser un nom de fichier temporaire
|
|
|
|
set_time_limit(0); |
|
|
|
$fp = fopen(DOWNLOADDIR.DIRECTORY_SEPARATOR.$retFilename, 'w+'); |
|
|
|
$ch = curl_init($url); |
|
|
|
@ -40,6 +60,73 @@ class Download { |
|
|
|
curl_close($ch); |
|
|
|
fclose($fp); |
|
|
|
|
|
|
|
// TODO : changer le statut du dl
|
|
|
|
// par exemple en passant par un param de la classe
|
|
|
|
|
|
|
|
return $retour; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Retourne si l'on peut lancer un nouveau dl ou si un lock est deja en place |
|
|
|
* |
|
|
|
* @return bool |
|
|
|
*/ |
|
|
|
static function lockOk() |
|
|
|
{ |
|
|
|
if (file_exists(DOWNLOADLOCK)) { |
|
|
|
return false; |
|
|
|
} else { |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Mets en place un lock, si err -> false |
|
|
|
* |
|
|
|
* @return bool |
|
|
|
*/ |
|
|
|
static function putLock() |
|
|
|
{ |
|
|
|
if (!touch(DOWNLOADLOCK)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Supprime le lock |
|
|
|
*/ |
|
|
|
static function removeLock() |
|
|
|
{ |
|
|
|
if (!file_exists(DOWNLOADLOCK)) { |
|
|
|
echo 'le fichier de lock n\'existe plus !!! exit'; |
|
|
|
exit(); |
|
|
|
} |
|
|
|
if (!unlink(DOWNLOADLOCK)) { |
|
|
|
echo 'impossible de supprimer le fichier de lock !!! exit'; |
|
|
|
exit(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Lance le telechargement du prochain fichier a dl |
|
|
|
*/ |
|
|
|
static function launchNextDownload() |
|
|
|
{ |
|
|
|
if (!self::lockOk()) { |
|
|
|
echo 'deja lock'; |
|
|
|
exit(); |
|
|
|
} |
|
|
|
|
|
|
|
if (!self::putLock()) { |
|
|
|
echo 'putlock err'; |
|
|
|
exit(); |
|
|
|
} |
|
|
|
|
|
|
|
$toDownload = self::findNextJob(); |
|
|
|
$result = self::get($toDownload['premURL'], $toDownload['filename']); |
|
|
|
|
|
|
|
self::removeLock(); |
|
|
|
} |
|
|
|
} |