PageRenderTime 29ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/Field/src/Utility/FileToolbox.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 177 lines | 96 code | 18 blank | 63 comment | 17 complexity | 98644ce58ed8041af6a6e0a30006dbcf MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. /**
  3. * Licensed under The GPL-3.0 License
  4. * For full copyright and license information, please see the LICENSE.txt
  5. * Redistributions of files must retain the above copyright notice.
  6. *
  7. * @since 2.0.0
  8. * @author Christopher Castro <chris@quickapps.es>
  9. * @link http://www.quickappscms.org
  10. * @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
  11. */
  12. namespace Field\Utility;
  13. use Cake\Core\Plugin;
  14. use Field\Utility\FileIconMap;
  15. /**
  16. * FileToolbox class for handling files related tasks.
  17. *
  18. * A set of utility methods related to files, such as `bytesToSize()`,
  19. * `fileIcon()`, etc.
  20. */
  21. class FileToolbox
  22. {
  23. /**
  24. * Renders the given custom field.
  25. *
  26. * @param \Cake\View\View $view Instance of view class
  27. * @param \Field\Model\Entity\Field $field The field to be rendered
  28. * @return string HTML code
  29. */
  30. public static function formatter($view, $field)
  31. {
  32. switch ($field->viewModeSettings['formatter']) {
  33. case 'link':
  34. $out = $view->element('Field.FileField/display_link', compact('field'));
  35. break;
  36. case 'table':
  37. $out = $view->element('Field.FileField/display_table', compact('field'));
  38. break;
  39. case 'url':
  40. default:
  41. $out = $view->element('Field.FileField/display_url', compact('field'));
  42. break;
  43. }
  44. return $out;
  45. }
  46. /**
  47. * Gets a translated string representation of the size.
  48. *
  49. * @param int $bytes Size to convert given in bytes units
  50. * @param int $precision Decimal precision
  51. * @return string Human-readable size, e.g. `1 KB`, `36.8 MB`, etc
  52. */
  53. public static function bytesToSize($bytes, $precision = 2)
  54. {
  55. $kilobyte = 1024;
  56. $megabyte = $kilobyte * 1024;
  57. $gigabyte = $megabyte * 1024;
  58. $terabyte = $gigabyte * 1024;
  59. if (($bytes >= 0) && ($bytes < $kilobyte)) {
  60. return $bytes . ' B';
  61. } elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
  62. return round($bytes / $kilobyte, $precision) . ' KB';
  63. } elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
  64. return round($bytes / $megabyte, $precision) . ' MB';
  65. } elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
  66. return round($bytes / $gigabyte, $precision) . ' GB';
  67. } elseif ($bytes >= $terabyte) {
  68. return round($bytes / $terabyte, $precision) . ' TB';
  69. } else {
  70. return $bytes . ' B';
  71. }
  72. }
  73. /**
  74. * Creates a path to the icon for a file mime.
  75. *
  76. * @param string $mime File mime type
  77. * @param mixed $iconsDirectory A path to a directory of icons to be used for
  78. * files. Defaults to built-in icons directory (Field/webroot/img/file-icons/)
  79. * @return mixed A string for the icon file name, or false if an appropriate
  80. * icon could not be found
  81. */
  82. public static function fileIcon($mime, $iconsDirectory = false)
  83. {
  84. if (!$iconsDirectory) {
  85. $iconsDirectory = Plugin::path('Field') . 'webroot/img/file-icons/';
  86. }
  87. // If there's an icon matching the exact mimetype, go for it.
  88. $dashedMime = strtr($mime, ['/' => '-']);
  89. if (is_readable("{$iconsDirectory}{$dashedMime}.png")) {
  90. return "{$dashedMime}.png";
  91. }
  92. // For a few mimetypes, we can "manually" map to a generic icon.
  93. $genericMime = (string)static::fileIconMap($mime);
  94. if ($genericMime && is_readable("{$iconsDirectory}{$genericMime}.png")) {
  95. return "{$genericMime}.png";
  96. }
  97. // Use generic icons for each category that provides such icons.
  98. if (preg_match('/^(audio|image|text|video)\//', $mime, $matches)) {
  99. if (is_readable("{$iconsDirectory}{$matches[1]}-x-generic.png")) {
  100. return "{$matches[1]}-x-generic.png";
  101. }
  102. }
  103. if (is_readable("{$iconsDirectory}/application-octet-stream.png")) {
  104. return 'application-octet-stream.png';
  105. }
  106. return false;
  107. }
  108. /**
  109. * Determine the generic icon MIME package based on a file's MIME type.
  110. *
  111. * @param string $mime File mime type
  112. * @return string The generic icon MIME package expected for this file
  113. */
  114. public static function fileIconMap($mime)
  115. {
  116. foreach (FileIconMap::$map as $icon => $mimeList) {
  117. if (in_array($mime, $mimeList)) {
  118. return $icon;
  119. }
  120. }
  121. return false;
  122. }
  123. /**
  124. * Get file extension.
  125. *
  126. * @param string $fileName File name, including its extension. e.g.: `my-file.docx`
  127. * @return string File extension without the ending DOT and lowercased,
  128. * e.g. `pdf`, `jpg`, `png`, etc. If no extension is found an empty string will
  129. * be returned
  130. */
  131. public static function ext($fileName)
  132. {
  133. if (strpos($fileName, '.') === false) {
  134. return '';
  135. }
  136. return strtolower(
  137. substr(
  138. $fileName,
  139. strrpos($fileName, '.') + 1,
  140. strlen($fileName)
  141. )
  142. );
  143. }
  144. /**
  145. * Remove file extension.
  146. *
  147. * @param string $fileName File name, including its extension.
  148. * e.g. `my-file.docx`, `myFile.DoCX`, etc
  149. * @return string File name without extension, e.g. `my-file`, `myFile`, etc
  150. */
  151. public static function removeExt($fileName)
  152. {
  153. $ext = static::ext($fileName);
  154. if ($ext) {
  155. return preg_replace("/\.{$ext}$/i", '', $fileName);
  156. }
  157. return $fileName;
  158. }
  159. }