File.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. public function getContent()
  36. {
  37. return $this->content;
  38. }
  39. public function getSize()
  40. {
  41. return $this->size;
  42. }
  43. public function getFormat()
  44. {
  45. return $this->format;
  46. }
  47. public function getVolid()
  48. {
  49. return $this->volid;
  50. }
  51. public function setContent($content)
  52. {
  53. $this->content = $content;
  54. return $this;
  55. }
  56. public function setSize($size)
  57. {
  58. $this->size = $size;
  59. return $this;
  60. }
  61. public function setFormat($format)
  62. {
  63. $this->format = $format;
  64. return $this;
  65. }
  66. public function setVolid($volid)
  67. {
  68. $this->volid = $volid;
  69. return $this;
  70. }
  71. public function getFriendlyName()
  72. {
  73. $ex = explode("/", $this->getVolid());
  74. $friendlyName = end($ex);
  75. $s = array("-", "_", ".tar.gz", ".iso", '.tar.xz');
  76. $r = array(" ", " ", "", "", "");
  77. $friendlyName = str_replace($s, $r, $friendlyName);
  78. $friendlyName = ucwords($friendlyName);
  79. return $friendlyName;
  80. }
  81. /**
  82. *
  83. * @param string $path i.e. /nodes/proxmox/storage/local/content/local:backup/vzdump-lxc-100-2017_01_18-10_33_18.tar.lzo
  84. * @throws proxmox\ProxmoxApiException
  85. */
  86. public function setPath($path)
  87. {
  88. if (!preg_match('/\/content\//', $path))
  89. {
  90. throw new proxmox\ProxmoxApiException(sprintf("File Path ('%s') is not valid", $path));
  91. }
  92. $this->path = $path;
  93. }
  94. public function getDate()
  95. {
  96. if($this->ctime){
  97. return date("Y-m-d", $this->ctime);
  98. }
  99. preg_match('/[0-9]{4}_[0-9]{2}_[0-9]{2}/', $this->volid, $matches);
  100. return str_replace("_", "-", $matches[0]);
  101. }
  102. public function getHour()
  103. {
  104. if($this->ctime){
  105. return date("H:i:s", $this->ctime);
  106. }
  107. preg_match('/-[0-9]{2}_[0-9]{2}_[0-9]{2}/', $this->volid, $matches);
  108. return str_replace(array("-", "_"), array("", ":"), $matches[0]);
  109. }
  110. public function getTimestamp()
  111. {
  112. return strtotime($this->getDate() . " " . $this->getHour());
  113. }
  114. public function getAttributes()
  115. {
  116. return array(
  117. "content" => $this->getContent(),
  118. "size" => $this->getSize(),
  119. "format" => $this->getFormat(),
  120. "volid" => $this->getVolid(),
  121. "date" => $this->getDate(),
  122. "hour" => $this->getHour()
  123. );
  124. }
  125. public function delete()
  126. {
  127. if (empty($this->path))
  128. throw new proxmox\ProxmoxApiException('File [path] - property is missing and it is not optional');
  129. return $this->api()->delete($this->path);
  130. }
  131. /**
  132. * @return mixed
  133. */
  134. public function getCtime()
  135. {
  136. return $this->ctime;
  137. }
  138. /**
  139. * @param mixed $ctime
  140. * @return File
  141. */
  142. public function setCtime($ctime)
  143. {
  144. $this->ctime = $ctime;
  145. return $this;
  146. }
  147. }