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.

182 lines
4.6 KiB

6 years ago
1 year ago
1 year ago
1 year ago
6 years ago
6 years ago
6 years ago
1 year ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. class Download {
  3. public $_id;
  4. public $timestamp;
  5. public $premURL;
  6. public $premFilename;
  7. public $url;
  8. public $filename;
  9. public $cstate;
  10. public $worker;
  11. static public $validStates = ['added', 'downloaded', 'downloading', 'dupp'];
  12. /**
  13. * Retourne les infos sur le prochain fichier a telecharger
  14. *
  15. * @return Download Instance du fichier a dl
  16. */
  17. static function findNextJob()
  18. {
  19. $db = DbMongo::get();
  20. $coll = $db->download;
  21. $dbRet = $coll->findOne([
  22. 'cstate' => 'added',
  23. 'worker' => getenv('WORKER')
  24. ]);
  25. $myDownload = new Download();
  26. $myDownload->db2Inst($dbRet);
  27. // $myDownload->updateState('downloading');
  28. // $myDownload->addEvent([
  29. // 'state' => 'downloading'
  30. // ]);
  31. //
  32. if (!empty($myDownload->_id)) {
  33. echo 'New job: '.$myDownload->dtype.':'.$myDownload->url;
  34. } else {
  35. echo 'No job in the queue';
  36. }
  37. return $myDownload;
  38. }
  39. static function addDownload($url, $worker, $dtype)
  40. {
  41. $myDownload = new Download();
  42. $db = DbMongo::get();
  43. $coll = $db->download;
  44. $values = [
  45. '_id' => new MongoId(),
  46. 'timestamp' => time(),
  47. 'cstate' => 'added',
  48. 'url' => $url,
  49. 'dtype' => $dtype,
  50. 'from' => $_SERVER['REMOTE_ADDR'],
  51. 'worker' => $worker
  52. ];
  53. // TODO : verifier le retour
  54. $coll->insert($values);
  55. $myDownload->db2Inst($values);
  56. return $myDownload;
  57. }
  58. private function db2Inst($dbRet)
  59. {
  60. $this->_id = $dbRet['_id'];
  61. $this->timestamp = $dbRet['timestamp'];
  62. $this->url = $dbRet['url'];
  63. $this->cstate = $dbRet['cstate'];
  64. $this->dtype = $dbRet['dtype'];
  65. $this->worker = $dbRet['worker'];
  66. }
  67. public function addEvent($event)
  68. {
  69. $db = DbMongo::get();
  70. $coll = $db->download;
  71. $dbRet = $coll->update(
  72. ['_id' => $this->_id],
  73. ['$push' => [
  74. 'event' => array_merge(
  75. [ 'timestamp' => time() ],
  76. $event
  77. )
  78. ]]
  79. );
  80. }
  81. /**
  82. * Telecharge le fichier
  83. *
  84. * @return array Informations sur le transfert
  85. */
  86. public function get() {
  87. set_time_limit(0);
  88. if ('premiumizeme' === $this->dtype) {
  89. PremiumizeMe::get($this);
  90. } elseif ('dlink' === $this->dtype) {
  91. DLink::get($this);
  92. }
  93. }
  94. // TODO: coder un changeState + option c'est utilisé à trop d'endroit mee sur premiumizeme
  95. public function updateState($state, $extras = [])
  96. {
  97. $db = DbMongo::get();
  98. $coll = $db->download;
  99. $dbRet = $coll->update(
  100. ['_id' => $this->_id],
  101. ['$set' => [
  102. 'cstate' => $state,
  103. 'extras' => $extras
  104. ]]
  105. );
  106. $this->cstate = $state;
  107. }
  108. public function updateFilename($filename)
  109. {
  110. $db = DbMongo::get();
  111. $coll = $db->download;
  112. $dbRet = $coll->update(
  113. ['_id' => $this->_id],
  114. ['$set' => [
  115. 'filename' => $filename
  116. ]]
  117. );
  118. $this->filename = $filename;
  119. }
  120. /**
  121. * Lance le telechargement du prochain fichier a dl
  122. */
  123. static function launchNextDownload()
  124. {
  125. if ('cgi-fcgi' === php_sapi_name()) {
  126. //fastcgi_finish_request();
  127. }
  128. $toDownload = self::findNextJob();
  129. $toDownload->get();
  130. }
  131. static function listByState($state)
  132. {
  133. $db = DbMongo::get();
  134. $coll = $db->download;
  135. $retour = [];
  136. foreach ($coll->find(['cstate' => $state])->sort(['timestamp' => -1]) as $element) {
  137. $element['time'] = DateTime::createFromFormat('U', $element['timestamp']);
  138. if (empty($element['filename'])) {
  139. $element['href'] = false;
  140. } elseif (file_exists(DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$element['filename'])) {
  141. $element['href'] = true;
  142. } else {
  143. $element['href'] = false;
  144. }
  145. $retour[] = $element;
  146. }
  147. return $retour;
  148. }
  149. static function clear($state)
  150. {
  151. $db = DbMongo::get();
  152. $coll = $db->download;
  153. $coll->remove(['cstate' => $state]);
  154. }
  155. }