class Files
{
- protected static $_mimeTypes = null;
-
- public static function rmdir($dir)
- {
- $files = self::getRecursiveDirectoryIterator($dir, \RecursiveIteratorIterator::CHILD_FIRST);
-
- foreach ($files as $file) {
- if ($file->isFile()) {
- unlink($file->__toString());
- } elseif ($file->isDir()) {
- rmdir($file->__toString());
- }
- }
- if (file_exists($dir)) {
- rmdir($dir);
- }
- }
-
- public static function humanReadableSize($size, $precision = 2)
- {
- $base = log($size) / log(1024);
- $suffixes = array('', 'k', 'M', 'G', 'T');
-
- return round(pow(1024, $base - floor($base)), $precision) . $suffixes[intval(floor($base))];
- }
-
- /**
- * @param $path
- * @param int $mode
- * @param array $exclude
- * @return array|\RecursiveIteratorIterator
- */
- public static function getRecursiveDirectoryIterator($path, $mode = \RecursiveIteratorIterator::SELF_FIRST, $exclude = array())
- {
- $path = realpath($path);
- if (!file_exists($path)) {
- return array();
- }
- $res = new \RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, $exclude), $mode);
- return $res;
- }
-
- public static function getDirectoryIterator($path, $recursive = false, $recursiveMode = \RecursiveIteratorIterator::SELF_FIRST)
- {
- if ($recursive) {
- return self::getRecursiveDirectoryIterator($path, $recursiveMode);
- }
-
- $path = realpath($path);
- if (!file_exists($path)) {
- return array();
- }
- return new \DirectoryIterator($path);
- }
-
- public static function getFilename($file)
- {
- $f = new \SplFileInfo($file);
- return $f->getFilename();
- }
-
- public static function getExtension($file)
- {
- $p = parse_url($file);
- if (isset($p['path']) && $p['path']) {
- $file = $p['path'];
- }
-
-
- $f = new \SplFileInfo($file);
- if (method_exists($f, 'getExtension')) {
- $res = $f->getExtension();
- } else {
- $e = explode('.', $file);
- $res = array_pop($e);
- }
- return mb_strtolower($res);
- }
-
- public static function getBasename($file, $suffix = null)
- {
-
- $f = new \SplFileInfo($file);
- if ($suffix == 'auto') {
- $suffix = '.' . self::getExtension($file);
- }
- return $f->getBasename($suffix);
- }
-
- public static function copy($from, $to, $context = null)
- {
-
- if (!file_exists($to)) {
- mkdir($to, 0777, true);
- }
-
- if (is_file($from)) {
- copy($from, $to);
- }
-
- if (!is_null($context)) {
- $dr = opendir($from, $context);
- } else {
- $dr = opendir($from);
- }
- if ($dr !== false) {
- while ($file = readdir($dr)) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- $f = $from . '/' . $file;
- if (is_dir($f)) {
- self::copy($f, $to . '/' . $file);
- } elseif (is_file($f)) {
- $dest = $to . '/' . $file;
- if (!file_exists($dest) ||
- filesize($f) != filesize($dest) ||
- filemtime($f) > filemtime($dest)
- ) {
- copy($f, $dest);
- }
- }
- }
- }
- }
-
- protected static function _getMimeTypes()
- {
- if (is_null(self::$_mimeTypes)) {
- $list = self::_getMimeTypesFromApacheFile();
- self::$_mimeTypes = $list;
- }
-
- return self::$_mimeTypes;
- }
-
- protected static function _getMimeTypesFromApacheFile()
- {
- $list = array();
- $fp = fopen(__DIR__ . '/mime.types', 'rb');
- while (($line = fgets($fp)) !== false) {
- $line = trim($line);
- if (substr($line, 0, 1) == '#') {
- continue;
- }
- $e = explode("\t", $line);
- $type = array_shift($e);
- $exts = array_pop($e);
- $exts = explode(' ', $exts);
- foreach ($exts as $ext) {
- $list[$ext] = $type;
- }
- }
- fclose($fp);
- $list['webvideo'] = 'video/webvideo';
- return $list;
- }
-
- public static function getMimeType($f)
- {
- return self::_getMimeType($f);
- }
-
- public static function _getMimeType($f)
- {
- $ext = mb_strtolower(self::getExtension($f));
- $types = self::_getMimeTypes();
-
- if (isset($types[$ext])) {
- return $types[$ext];
- } else {
- return 'application/octet-stream';
- }
- }
-
- public static function tmpdir($dir = null, $prefix = 'cubeit_', $create = true)
- {
- if (is_null($dir)) {
- $dir = sys_get_temp_dir();
- }
-
- do {
- $gen = rtrim($prefix, '_') . '_' . substr(md5(rand(1, 1000000000)), 0, 6);
- $res = $dir . '/' . $gen;
- } while (file_exists($res));
-
- if ($create) {
- mkdir($res, 0777, true);
- }
-
- return $res;
- }
-
- public static function tempnam($dir = null, $prefix = 'cubeit')
- {
- if (is_null($dir)) {
- $dir = sys_get_temp_dir();
- }
- return tempnam($dir, $prefix);
- }
-
- public static function dirmtime($dir, $recursive = false)
- {
- $time = filemtime($dir);
- $files = self::getDirectoryIterator($dir, $recursive);
- foreach ($files as $f) {
- $time = max($time, $f->getMTime());
- }
- return $time;
- }
+ protected static $_mimeTypes = null;
+
+ public static function rmdir($dir)
+ {
+ $files = self::getRecursiveDirectoryIterator($dir, \RecursiveIteratorIterator::CHILD_FIRST);
+
+ foreach ($files as $file) {
+ if ($file->isFile()) {
+ unlink($file->__toString());
+ } elseif ($file->isDir()) {
+ rmdir($file->__toString());
+ }
+ }
+ if (file_exists($dir)) {
+ rmdir($dir);
+ }
+ }
+
+ public static function humanReadableSize($size, $precision = 2)
+ {
+ $base = log($size) / log(1024);
+ $suffixes = array('', 'k', 'M', 'G', 'T');
+
+ return round(pow(1024, $base - floor($base)), $precision) . $suffixes[intval(floor($base))];
+ }
+
+ /**
+ * @param $path
+ * @param int $mode
+ * @param array $exclude
+ * @return array|\RecursiveIteratorIterator
+ */
+ public static function getRecursiveDirectoryIterator($path, $mode = \RecursiveIteratorIterator::SELF_FIRST, $exclude = array())
+ {
+ $path = realpath($path);
+ if (!file_exists($path)) {
+ return array();
+ }
+ $res = new \RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, $exclude), $mode);
+ return $res;
+ }
+
+ public static function getDirectoryIterator($path, $recursive = false, $recursiveMode = \RecursiveIteratorIterator::SELF_FIRST)
+ {
+ if ($recursive) {
+ return self::getRecursiveDirectoryIterator($path, $recursiveMode);
+ }
+
+ $path = realpath($path);
+ if (!file_exists($path)) {
+ return array();
+ }
+ return new \DirectoryIterator($path);
+ }
+
+ public static function getFilename($file)
+ {
+ $f = new \SplFileInfo($file);
+ return $f->getFilename();
+ }
+
+ public static function getExtension($file)
+ {
+ $p = parse_url($file);
+ if (isset($p['path']) && $p['path']) {
+ $file = $p['path'];
+ }
+
+
+ $f = new \SplFileInfo($file);
+ if (method_exists($f, 'getExtension')) {
+ $res = $f->getExtension();
+ } else {
+ $e = explode('.', $file);
+ $res = array_pop($e);
+ }
+ return mb_strtolower($res);
+ }
+
+ public static function getBasename($file, $suffix = null)
+ {
+
+ $f = new \SplFileInfo($file);
+ if ($suffix == 'auto') {
+ $suffix = '.' . self::getExtension($file);
+ }
+ return $f->getBasename($suffix);
+ }
+
+ public static function copy($from, $to, $context = null)
+ {
+
+ if (!file_exists($to)) {
+ mkdir($to, 0777, true);
+ }
+
+ if (is_file($from)) {
+ copy($from, $to);
+ }
+
+ if (!is_null($context)) {
+ $dr = opendir($from, $context);
+ } else {
+ $dr = opendir($from);
+ }
+ if ($dr !== false) {
+ while ($file = readdir($dr)) {
+ if ($file == '.' || $file == '..') {
+ continue;
+ }
+ $f = $from . '/' . $file;
+ if (is_dir($f)) {
+ self::copy($f, $to . '/' . $file);
+ } elseif (is_file($f)) {
+ $dest = $to . '/' . $file;
+ if (!file_exists($dest) ||
+ filesize($f) != filesize($dest) ||
+ filemtime($f) > filemtime($dest)
+ ) {
+ copy($f, $dest);
+ }
+ }
+ }
+ }
+ }
+
+ protected static function _getMimeTypes()
+ {
+ if (is_null(self::$_mimeTypes)) {
+ $list = self::_getMimeTypesFromApacheFile();
+ self::$_mimeTypes = $list;
+ }
+
+ return self::$_mimeTypes;
+ }
+
+ protected static function _getMimeTypesFromApacheFile()
+ {
+ $list = array();
+ $fp = fopen(__DIR__ . '/mime.types', 'rb');
+ while (($line = fgets($fp)) !== false) {
+ $line = trim($line);
+ if (substr($line, 0, 1) == '#') {
+ continue;
+ }
+ $e = explode("\t", $line);
+ $type = array_shift($e);
+ $exts = array_pop($e);
+ $exts = explode(' ', $exts);
+ foreach ($exts as $ext) {
+ $list[$ext] = $type;
+ }
+ }
+ fclose($fp);
+ $list['webvideo'] = 'video/webvideo';
+ return $list;
+ }
+
+ public static function getMimeType($f)
+ {
+ return self::_getMimeType($f);
+ }
+
+ public static function _getMimeType($f)
+ {
+ $ext = mb_strtolower(self::getExtension($f));
+ $types = self::_getMimeTypes();
+
+ if (isset($types[$ext])) {
+ return $types[$ext];
+ } else {
+ return 'application/octet-stream';
+ }
+ }
+
+ public static function tmpdir($dir = null, $prefix = 'cubeist_', $create = true)
+ {
+ if (is_null($dir)) {
+ $dir = sys_get_temp_dir();
+ }
+
+ do {
+ $gen = rtrim($prefix, '_') . '_' . substr(hash('sha256', uniqid($prefix, true)), 0, 10);
+ $res = $dir . '/' . $gen;
+ } while (file_exists($res));
+
+ if ($create) {
+ mkdir($res, 0777, true);
+ }
+
+ return $res;
+ }
+
+ public static function tempnam($dir = null, $prefix = 'cubeist')
+ {
+ if (is_null($dir)) {
+ $dir = sys_get_temp_dir();
+ }
+ return tempnam($dir, $prefix);
+ }
+
+ public static function dirmtime($dir, $recursive = false)
+ {
+ $time = filemtime($dir);
+ $files = self::getDirectoryIterator($dir, $recursive);
+ foreach ($files as $f) {
+ $time = max($time, $f->getMTime());
+ }
+ return $time;
+ }
}
\ No newline at end of file