You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
205 lines
5.7 KiB
205 lines
5.7 KiB
<?php
|
|
|
|
class Download {
|
|
public $_id;
|
|
public $timestamp;
|
|
public $premURL;
|
|
public $url;
|
|
public $filename;
|
|
public $cstate;
|
|
static public $validStates = ['added', 'downloaded', 'dupp'];
|
|
|
|
/**
|
|
* Retourne les infos sur le prochain fichier a telecharger
|
|
*
|
|
* @return Download Instance du fichier a dl
|
|
*/
|
|
static function findNextJob()
|
|
{
|
|
$db = DbMongo::get();
|
|
$coll = $db->download;
|
|
$dbRet = $coll->findOne(
|
|
['cstate' => 'added']
|
|
);
|
|
$myDownload = new Download();
|
|
$myDownload->db2Inst($dbRet);
|
|
|
|
$dbRet = $coll->update(
|
|
['_id' => $myDownload->_id],
|
|
['$set' => ['cstate' => 'downloading']]
|
|
);
|
|
|
|
$myDownload->addEvent([
|
|
'timestamp' => time(),
|
|
'state' => 'downloading'
|
|
]);
|
|
|
|
return $myDownload;
|
|
}
|
|
|
|
static function addDownload($url)
|
|
{
|
|
$premInfos = PremiumizeMe::getLink($url);
|
|
$myTime = time();
|
|
$myDownload = new Download();
|
|
|
|
$db = DbMongo::get();
|
|
$coll = $db->download;
|
|
|
|
$values = [
|
|
'_id' => new MongoId(),
|
|
'timestamp' => $myTime,
|
|
'premURL' => $premInfos['location'],
|
|
'url' => $url,
|
|
'filename' => $premInfos['filename'],
|
|
'cstate' => 'added'
|
|
];
|
|
|
|
// TODO : verifier le retour
|
|
$coll->insert($values);
|
|
|
|
$myDownload->db2Inst($values);
|
|
|
|
$myDownload->addEvent([
|
|
'timestamp' => $myTime,
|
|
'ip' => $_SERVER['REMOTE_ADDR'],
|
|
'url' => $url,
|
|
'state' => 'added'
|
|
]);
|
|
|
|
return $myDownload;
|
|
}
|
|
|
|
private function db2Inst($dbRet)
|
|
{
|
|
$this->_id = $dbRet['_id'];
|
|
$this->timestamp = $dbRet['timestamp'];
|
|
$this->premURL = $dbRet['premURL'];
|
|
$this->url = $dbRet['url'];
|
|
$this->filename = $dbRet['filename'];
|
|
$this->cstate = $dbRet['cstate'];
|
|
}
|
|
|
|
private function addEvent($event)
|
|
{
|
|
$db = DbMongo::get();
|
|
$coll = $db->download;
|
|
$dbRet = $coll->update(
|
|
['_id' => $this->_id],
|
|
['$push' => ['event' => $event]]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Telecharge le fichier
|
|
*
|
|
* @return array Informations sur le transfert
|
|
*/
|
|
public function get() {
|
|
set_time_limit(0);
|
|
|
|
if (file_exists(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename) || file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$this->filename)) {
|
|
$this->addEvent([
|
|
'timestamp' => time(),
|
|
'state' => 'dupp'
|
|
]);
|
|
|
|
$db = DbMongo::get();
|
|
$coll = $db->download;
|
|
$dbRet = $coll->update(
|
|
['_id' => $this->_id],
|
|
['$set' => ['cstate' => 'dupp']]
|
|
);
|
|
return false;
|
|
}
|
|
// On le creer tout de suite pour eviter des conflits de process
|
|
touch(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename);
|
|
|
|
$fp = fopen(DOWNLOADINGDIR.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']);
|
|
curl_exec($ch);
|
|
$retour = curl_getinfo($ch);
|
|
curl_close($ch);
|
|
fclose($fp);
|
|
|
|
$this->addEvent([
|
|
'timestamp' => time(),
|
|
'state' => 'downloaded',
|
|
'curlinfo' => [
|
|
'http_code' => $retour['http_code'],
|
|
'total_time' => $retour['total_time'],
|
|
'namelookup_time' => $retour['namelookup_time'],
|
|
'connect_time' => $retour['connect_time'],
|
|
'pretransfer_time' => $retour['pretransfer_time'],
|
|
'size_download' => $retour['size_download'],
|
|
'speed_download' => $retour['speed_download'],
|
|
'starttransfer_time' => $retour['starttransfer_time']
|
|
]
|
|
]);
|
|
|
|
if (200 !== $retour['http_code']) {
|
|
$db = DbMongo::get();
|
|
$coll = $db->download;
|
|
$dbRet = $coll->update(
|
|
['_id' => $this->_id],
|
|
['$set' => ['cstate' => 'error']]
|
|
);
|
|
return false;
|
|
}
|
|
|
|
rename(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename, DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$this->filename);
|
|
|
|
$db = DbMongo::get();
|
|
$coll = $db->download;
|
|
$dbRet = $coll->update(
|
|
['_id' => $this->_id],
|
|
['$set' => ['cstate' => 'downloaded']]
|
|
);
|
|
|
|
return $retour;
|
|
}
|
|
|
|
/**
|
|
* Lance le telechargement du prochain fichier a dl
|
|
*/
|
|
static function launchNextDownload()
|
|
{
|
|
echo json_encode('Traitement');
|
|
fastcgi_finish_request();
|
|
|
|
$toDownload = self::findNextJob();
|
|
$toDownload->get();
|
|
}
|
|
|
|
static function listByState($state)
|
|
{
|
|
$db = DbMongo::get();
|
|
$coll = $db->download;
|
|
$retour = [];
|
|
|
|
foreach ($coll->find(['cstate' => $state])->sort(['timestamp' => -1]) as $element) {
|
|
$element['time'] = DateTime::createFromFormat('U', $element['timestamp']);
|
|
|
|
if (file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$element['filename'])) {
|
|
$element['href'] = true;
|
|
} else {
|
|
$element['href'] = false;
|
|
}
|
|
|
|
$retour[] = $element;
|
|
}
|
|
|
|
return $retour;
|
|
}
|
|
|
|
static function clear($state)
|
|
{
|
|
$db = DbMongo::get();
|
|
$coll = $db->download;
|
|
|
|
$coll->remove(['cstate' => $state]);
|
|
}
|
|
}
|