Browse Source

dl separe du client, ajout de log, debut d'instance

master
Cabillot Julien 6 years ago
parent
commit
88b020bcc1
2 changed files with 48 additions and 38 deletions
  1. 81
      lib/Download.php
  2. 5
      root/index.php

81
lib/Download.php

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

5
root/index.php

@ -8,16 +8,17 @@ if (!isset($_GET['action'])) {
switch($_GET['action']) {
case 'status':
// http://mydl.cabillot.eu/
include dirname(__FILE__).'/../controller/status.php';
getStatus();
break;
case 'stream':
// http://mydl.cabillot.eu/stream/?myurl=https://1fichier.com/?yawxht1b3y
// http://mydl.cabillot.eu/stream/?myurl=https://1fichier.com/?fgrh5c7roq
include dirname(__FILE__).'/../controller/stream.php';
getStream();
break;
case 'download':
// http://mydl.cabillot.eu/download/?myurls[]=https://1fichier.com/?yawxht1b3y
// http://mydl.cabillot.eu/download/?myurls[]=https://1fichier.com/?fgrh5c7roq
include dirname(__FILE__).'/../controller/download.php';
addDownload();
break;

Loading…
Cancel
Save