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.
 
 
 
 
 

176 lines
4.4 KiB

<?php
class Download {
public $_id;
public $timestamp;
public $premURL;
public $premFilename;
public $url;
public $filename;
public $cstate;
public $worker;
static public $validStates = ['added', 'downloaded', 'downloading', '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);
$myDownload->updateState('downloading');
$myDownload->addEvent([
'state' => 'downloading'
]);
return $myDownload;
}
static function addDownload($url, $worker, $dtype)
{
$myDownload = new Download();
$db = DbMongo::get();
$coll = $db->download;
$values = [
'_id' => new MongoId(),
'timestamp' => time(),
'cstate' => 'added',
'url' => $url,
'dtype' => $dtype,
'from' => $_SERVER['REMOTE_ADDR'],
'worker' => $worker
];
// TODO : verifier le retour
$coll->insert($values);
$myDownload->db2Inst($values);
return $myDownload;
}
private function db2Inst($dbRet)
{
$this->_id = $dbRet['_id'];
$this->timestamp = $dbRet['timestamp'];
$this->url = $dbRet['url'];
$this->cstate = $dbRet['cstate'];
$this->dtype = $dbRet['dtype'];
$this->worker = $dbRet['worker'];
}
public function addEvent($event)
{
$db = DbMongo::get();
$coll = $db->download;
$dbRet = $coll->update(
['_id' => $this->_id],
['$push' => [
'event' => array_merge(
[ 'timestamp' => time() ],
$event
)
]]
);
}
/**
* Telecharge le fichier
*
* @return array Informations sur le transfert
*/
public function get() {
set_time_limit(0);
if ('premiumizeme' === $this->dtype) {
PremiumizeMe::get($this);
} elseif ('dlink' === $this->dtype) {
DLink::get($this);
}
}
// TODO: coder un changeState + option c'est utilisé à trop d'endroit mee sur premiumizeme
public function updateState($state, $extras = [])
{
$db = DbMongo::get();
$coll = $db->download;
$dbRet = $coll->update(
['_id' => $this->_id],
['$set' => [
'cstate' => $state,
'extras' => $extras
]]
);
$this->cstate = $state;
}
public function updateFilename($filename)
{
$db = DbMongo::get();
$coll = $db->download;
$dbRet = $coll->update(
['_id' => $this->_id],
['$set' => [
'filename' => $filename
]]
);
$this->filename = $filename;
}
/**
* Lance le telechargement du prochain fichier a dl
*/
static function launchNextDownload()
{
echo json_encode('Traitement');
if ('cgi-fcgi' === php_sapi_name()) {
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 (empty($element['filename'])) {
$element['href'] = false;
} elseif (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]);
}
}