File.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-10-11)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace MGProvision\Proxmox\v2\models;
  20. use MGProvision\Proxmox\v2 as proxmox;
  21. /**
  22. * Description of File
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgarden.com>
  25. * @version 1.0.0
  26. */
  27. class File extends AbstractObject
  28. {
  29. protected $content;
  30. protected $size;
  31. protected $format;
  32. protected $volid;
  33. protected $ctime;
  34. private $path;
  35. protected $node;
  36. protected $vmid;
  37. public function getContent()
  38. {
  39. return $this->content;
  40. }
  41. public function getSize()
  42. {
  43. return $this->size;
  44. }
  45. public function getFormat()
  46. {
  47. return $this->format;
  48. }
  49. public function getVolid()
  50. {
  51. return $this->volid;
  52. }
  53. public function setContent($content)
  54. {
  55. $this->content = $content;
  56. return $this;
  57. }
  58. public function setSize($size)
  59. {
  60. $this->size = $size;
  61. return $this;
  62. }
  63. public function setFormat($format)
  64. {
  65. $this->format = $format;
  66. return $this;
  67. }
  68. public function setVolid($volid)
  69. {
  70. $this->volid = $volid;
  71. return $this;
  72. }
  73. public function getFriendlyName()
  74. {
  75. $ex = explode("/", $this->getVolid());
  76. $friendlyName = end($ex);
  77. $s = array("-", "_", ".tar.gz", ".iso", '.tar.xz');
  78. $r = array(" ", " ", "", "", "");
  79. $friendlyName = str_replace($s, $r, $friendlyName);
  80. $friendlyName = ucwords($friendlyName);
  81. return $friendlyName;
  82. }
  83. /**
  84. *
  85. * @param string $path i.e. /nodes/proxmox/storage/local/content/local:backup/vzdump-lxc-100-2017_01_18-10_33_18.tar.lzo
  86. * @throws proxmox\ProxmoxApiException
  87. */
  88. public function setPath($path)
  89. {
  90. if (!preg_match('/\/content\//', $path))
  91. {
  92. throw new proxmox\ProxmoxApiException(sprintf("File Path ('%s') is not valid", $path));
  93. }
  94. $this->path = $path;
  95. }
  96. public function getDate()
  97. {
  98. if($this->ctime){
  99. return date("Y-m-d", $this->ctime);
  100. }
  101. preg_match('/[0-9]{4}_[0-9]{2}_[0-9]{2}/', $this->volid, $matches);
  102. return str_replace("_", "-", $matches[0]);
  103. }
  104. public function getHour()
  105. {
  106. if($this->ctime){
  107. return date("H:i:s", $this->ctime);
  108. }
  109. preg_match('/-[0-9]{2}_[0-9]{2}_[0-9]{2}/', $this->volid, $matches);
  110. return str_replace(array("-", "_"), array("", ":"), $matches[0]);
  111. }
  112. public function getTimestamp()
  113. {
  114. return strtotime($this->getDate() . " " . $this->getHour());
  115. }
  116. public function getAttributes()
  117. {
  118. return array(
  119. "content" => $this->getContent(),
  120. "size" => $this->getSize(),
  121. "format" => $this->getFormat(),
  122. "volid" => $this->getVolid(),
  123. "date" => $this->getDate(),
  124. "hour" => $this->getHour()
  125. );
  126. }
  127. public function delete()
  128. {
  129. if (empty($this->path))
  130. throw new proxmox\ProxmoxApiException('File [path] - property is missing and it is not optional');
  131. return $this->api()->delete($this->path);
  132. }
  133. /**
  134. * @return mixed
  135. */
  136. public function getCtime()
  137. {
  138. return $this->ctime;
  139. }
  140. /**
  141. * @param mixed $ctime
  142. * @return File
  143. */
  144. public function setCtime($ctime)
  145. {
  146. $this->ctime = $ctime;
  147. return $this;
  148. }
  149. /**
  150. * @return mixed
  151. */
  152. public function getNode()
  153. {
  154. return $this->node;
  155. }
  156. /**
  157. * @param mixed $node
  158. * @return File
  159. */
  160. public function setNode($node)
  161. {
  162. $this->node = $node;
  163. return $this;
  164. }
  165. public function formatVolid()
  166. {
  167. $ex = explode("/", $this->getVolid());
  168. return end($ex);
  169. }
  170. public function getVmid()
  171. {
  172. return $this->vmid;
  173. }
  174. /**
  175. * @param mixed $vmid
  176. * @return File
  177. */
  178. public function setVmid($vmid)
  179. {
  180. $this->vmid = $vmid;
  181. return $this;
  182. }
  183. }