|
|
|
@ -1,57 +1,57 @@ |
|
|
|
<?php |
|
|
|
|
|
|
|
class Download { |
|
|
|
public $_id; |
|
|
|
public $timestamp; |
|
|
|
public $ip; |
|
|
|
public $url; |
|
|
|
public $premURL; |
|
|
|
public $filename; |
|
|
|
public $state; |
|
|
|
|
|
|
|
/** |
|
|
|
* Retourne les infos sur le prochain fichier a telecharger |
|
|
|
* |
|
|
|
* @return array |
|
|
|
* @return Download Instance du fichier a dl |
|
|
|
*/ |
|
|
|
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( |
|
|
|
$dbRet = $coll->findAndModify( |
|
|
|
['state' => 'added'], |
|
|
|
['$set' => ['state' => 'inprogress']], |
|
|
|
['$set' => ['state' => 'dlprogress']], |
|
|
|
null, |
|
|
|
['sort' => ["timestamp" => 1]] |
|
|
|
); |
|
|
|
return $retval; |
|
|
|
var_dump($dbRet); |
|
|
|
$myDownload = new Download(); |
|
|
|
$myDownload->db2Inst($dbRet); |
|
|
|
return $myDownload; |
|
|
|
} |
|
|
|
|
|
|
|
private function db2Inst($dbRet) |
|
|
|
{ |
|
|
|
$this->_id = $dbRet['_id']; |
|
|
|
$this->timestamp = $dbRet['timestamp']; |
|
|
|
$this->ip = $dbRet['ip']; |
|
|
|
$this->url = $dbRet['url']; |
|
|
|
$this->premURL = $dbRet['premURL']; |
|
|
|
$this->filename = $dbRet['filename']; |
|
|
|
$this->state = $dbRet['state']; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Telecharge depuis $url vers $out. |
|
|
|
* |
|
|
|
* @param string $url URL a telecharger |
|
|
|
* @param string $out Fichier de destination |
|
|
|
* Telecharge le fichier |
|
|
|
* |
|
|
|
* @return array Informations sur le transfert |
|
|
|
*/ |
|
|
|
static function get($url) { |
|
|
|
set_time_limit(0); |
|
|
|
|
|
|
|
$ch = curl_init($url); |
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); |
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true); |
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, true); |
|
|
|
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); |
|
|
|
$res = curl_exec($ch); |
|
|
|
curl_close($ch); |
|
|
|
|
|
|
|
preg_match('/^HTTP\/1\.[01] (.*) .*/', $res, $matches); |
|
|
|
$retCode = $matches[1]; |
|
|
|
preg_match('/Content-Length: (.*)/', $res, $matches); |
|
|
|
$retLenght = $matches[1]; |
|
|
|
preg_match("/Content-Disposition: attachment; filename=\"(.*)\"; filename\*=UTF-8\'\'(.*)/", $res, $matches); |
|
|
|
$retFilename = $matches[1]; |
|
|
|
unset($matches); |
|
|
|
|
|
|
|
public function get() { |
|
|
|
// TODO : utiliser un nom de fichier temporaire
|
|
|
|
set_time_limit(0); |
|
|
|
$fp = fopen(DOWNLOADDIR.DIRECTORY_SEPARATOR.$retFilename, 'w+'); |
|
|
|
$ch = curl_init($url); |
|
|
|
$fp = fopen(DOWNLOADDIR.DIRECTORY_SEPARATOR.$this->filename, 'w+'); |
|
|
|
$ch = curl_init($this->premURL); |
|
|
|
curl_setopt($ch, CURLOPT_FILE, $fp); |
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
|
|
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); |
|
|
|
@ -60,8 +60,14 @@ class Download { |
|
|
|
curl_close($ch); |
|
|
|
fclose($fp); |
|
|
|
|
|
|
|
// TODO : changer le statut du dl
|
|
|
|
// par exemple en passant par un param de la classe
|
|
|
|
// TODO : verifier le retour ?
|
|
|
|
// TODO : ajouter les infos sur le dl
|
|
|
|
$db = DbMongo::get(); |
|
|
|
$coll = $db->download; |
|
|
|
$coll->findAndModify( |
|
|
|
['_id' => $this->_id], |
|
|
|
['$set' => ['state' => 'downloaded']] |
|
|
|
); |
|
|
|
|
|
|
|
return $retour; |
|
|
|
} |
|
|
|
@ -115,18 +121,21 @@ class Download { |
|
|
|
static function launchNextDownload() |
|
|
|
{ |
|
|
|
if (!self::lockOk()) { |
|
|
|
echo 'deja lock'; |
|
|
|
echo json_encode('deja lock'); |
|
|
|
exit(); |
|
|
|
} |
|
|
|
|
|
|
|
if (!self::putLock()) { |
|
|
|
echo 'putlock err'; |
|
|
|
echo json_encode('putlock err'); |
|
|
|
exit(); |
|
|
|
} |
|
|
|
|
|
|
|
echo json_encode('Ok'); |
|
|
|
fastcgi_finish_request(); |
|
|
|
|
|
|
|
$toDownload = self::findNextJob(); |
|
|
|
$result = self::get($toDownload['premURL'], $toDownload['filename']); |
|
|
|
$toDownload->get(); |
|
|
|
|
|
|
|
self::removeLock(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |