Browse Source

function pour lancer le dl du prochain fichier

master
Cabillot Julien 6 years ago
parent
commit
700b628568
4 changed files with 108 additions and 10 deletions
  1. 19
      controller/download.php
  2. 6
      include/config.php
  3. 87
      lib/Download.php
  4. 6
      root/index.php

19
controller/download.php

@ -1,7 +1,9 @@
<?php
// TODO : ne doit pas lancer le dl directement mais simplement l'enregistrer en base
function getDownload()
/**
* Ajoute en base les infos sur le(s) fichier(s) a dl (ne telecharge pas directement le(s) fichier(s))
*/
function addDownload()
{
if (isset($_GET['myurls'])) {
$myurls = $_GET['myurls'];
@ -17,12 +19,13 @@ function getDownload()
$db = DbMongo::get();
$coll = $db->download;
$coll->insert([
'timestamp' => time(),
'ip' => $_SERVER['REMOTE_ADDR'],
'url' => $url,
'premURL' => $premInfos['location'],
'filename' => $premInfos['filename']
'timestamp' => time(),
'ip' => $_SERVER['REMOTE_ADDR'],
'url' => $url,
'premURL' => $premInfos['location'],
'filename' => $premInfos['filename'],
'state' => 'added'
]);
$result = Download::get($premInfos['location'], $premInfos['filename']);
}
}

6
include/config.php

@ -9,8 +9,12 @@ define('DB_COLL', 'mydl');
* Defini le repertoire temporaire ou seront stocke les downloads
*/
define('DOWNLOADDIR', dirname(__FILE__).'/../download');
/**
* Defini l'endroit ou se trouve le lock pour les download
*/
define('DOWNLOADLOCK', dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'download'.DIRECTORY_SEPARATOR.'lock');
include dirname(__FILE__).'/../lib/DbMongo.php';
include dirname(__FILE__).'/../lib/Download.php';
include dirname(__FILE__).'/../lib/PremiumizeMe.php';
include dirname(__FILE__).'/../lib/Stream.php';
include dirname(__FILE__).'/../lib/Stream.php';

87
lib/Download.php

@ -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();
}
}

6
root/index.php

@ -19,6 +19,10 @@ switch($_GET['action']) {
case 'download':
// http://mydl.cabillot.eu/download/?myurls[]=https://1fichier.com/?yawxht1b3y
include dirname(__FILE__).'/../controller/download.php';
getDownload();
addDownload();
break;
case 'launchdownload':
// http://mydl.cabillot.eu/launchdownload/
Download::launchNextDownload();
break;
}
Loading…
Cancel
Save