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

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