Gestion download au travers de premiumizeme
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.
 
 
 
 
 

200 lines
5.1 KiB

<?php
class Download {
public $_id;
public $timestamp;
public $premURL;
public $filename;
public $cstate;
/**
* 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);
$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'],
'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->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() {
// TODO : tester si fichier existant (temp et definitif)
set_time_limit(0);
if (file_exists(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename) || file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$this->filename)) {
echo 'le fichier existe deja, exit';
exit();
}
$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);
rename(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename, DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$this->filename);
// TODO : verifier le retour ?
$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']
]
]);
$db = DbMongo::get();
$coll = $db->download;
$dbRet = $coll->update(
['_id' => $this->_id],
['$set' => ['cstate' => 'downloaded']]
);
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 json_encode('deja lock');
exit();
}
if (!self::putLock()) {
echo json_encode('putlock err');
exit();
}
echo json_encode('Ok');
fastcgi_finish_request();
$toDownload = self::findNextJob();
$toDownload->get();
self::removeLock();
}
}