Пример #1
0
 /**
  * List folders and files
  *
  * @param  string $root      root path name
  * @param  string $prefix    Optional. search only for folders and files by specified prefix.
  * @param  string $delimiter Optional. Delimiter, i.e. '/', for specifying folder hierarchy
  *
  * @return array
  * @throws \Exception
  */
 public static function listTree($root, $prefix = '', $delimiter = '')
 {
     $dir = $root . (!empty($prefix) ? $prefix : '');
     $out = [];
     if (is_dir($dir)) {
         $files = array_diff(scandir($dir), ['.', '..']);
         foreach ($files as $file) {
             $key = $dir . $file;
             $local = (!empty($prefix) ? $prefix : '') . $file;
             // get file meta
             if (is_dir($key)) {
                 $stat = stat($key);
                 $out[] = ['path' => str_replace(DIRECTORY_SEPARATOR, '/', $local) . '/', 'last_modified' => gmdate('D, d M Y H:i:s \\G\\M\\T', ArrayUtils::get($stat, 'mtime', 0))];
                 if (empty($delimiter)) {
                     $out = array_merge($out, static::listTree($root, $local . DIRECTORY_SEPARATOR));
                 }
             } elseif (is_file($key)) {
                 $stat = stat($key);
                 $ext = FileUtilities::getFileExtension($key);
                 $out[] = ['path' => str_replace(DIRECTORY_SEPARATOR, '/', $local), 'content_type' => FileUtilities::determineContentType($ext, '', $key), 'last_modified' => gmdate('D, d M Y H:i:s \\G\\M\\T', ArrayUtils::get($stat, 'mtime', 0)), 'content_length' => ArrayUtils::get($stat, 'size', 0)];
             } else {
                 error_log($key);
             }
         }
     } else {
         throw new NotFoundException("Folder '{$prefix}' does not exist in storage.");
     }
     return $out;
 }
Пример #2
0
 public static function sendFile($file, $download = false)
 {
     if (is_file($file)) {
         $ext = FileUtilities::getFileExtension($file);
         $disposition = $download ? 'attachment' : 'inline';
         header('Last-Modified: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', filemtime($file)));
         header('Content-Type: ' . FileUtilities::determineContentType($ext, '', $file));
         header('Content-Length:' . filesize($file));
         header('Content-Disposition: ' . $disposition . '; filename="' . basename($file) . '";');
         header('Cache-Control: private');
         // use this to open files directly
         header('Expires: 0');
         header('Pragma: public');
         readfile($file);
     } else {
         Log::debug('FileUtilities::downloadFile is_file call fail: ' . $file);
         $statusHeader = 'HTTP/1.1 404 The specified file ' . $file . ' does not exist.';
         header($statusHeader);
         header('Content-Type: text/html');
     }
 }
Пример #3
0
 /**
  * @param        $dest_path
  * @param        $dest_name
  * @param        $source_file
  * @param string $contentType
  * @param bool   $extract
  * @param bool   $clean
  * @param bool   $check_exist
  *
  * @throws \Exception
  * @return array
  */
 protected function handleFile($dest_path, $dest_name, $source_file, $contentType = '', $extract = false, $clean = false, $check_exist = false)
 {
     $ext = FileUtilities::getFileExtension($source_file);
     if (empty($contentType)) {
         $contentType = FileUtilities::determineContentType($ext, '', $source_file);
     }
     if ((FileUtilities::isZipContent($contentType) || 'zip' === $ext) && $extract) {
         // need to extract zip file and move contents to storage
         $zip = new \ZipArchive();
         if (true === $zip->open($source_file)) {
             return $this->extractZipFile($this->container, $dest_path, $zip, $clean);
         } else {
             throw new InternalServerErrorException('Error opening temporary zip file.');
         }
     } else {
         $name = empty($dest_name) ? basename($source_file) : $dest_name;
         $fullPathName = FileUtilities::fixFolderPath($dest_path) . $name;
         $this->driver->moveFile($this->container, $fullPathName, $source_file, $check_exist);
         return ['name' => $name, 'path' => $fullPathName, 'type' => 'file'];
     }
 }
Пример #4
0
 /**
  * @param string $container
  * @param string $path
  * @param string $local_path
  * @param bool   $check_exist
  *
  * @throws \Exception
  * @throws NotFoundException
  * @throws BadRequestException
  * @return void
  */
 public function moveFile($container, $path, $local_path, $check_exist = true)
 {
     // does local file exist?
     if (!file_exists($local_path)) {
         throw new NotFoundException("File '{$local_path}' does not exist.");
     }
     // does this file already exist?
     if ($this->fileExists($container, $path)) {
         if ($check_exist) {
             throw new BadRequestException("File '{$path}' already exists.");
         }
     }
     // does this file's parent folder exist?
     $parent = FileUtilities::getParentFolder($path);
     if (!empty($parent) && !$this->folderExists($container, $parent)) {
         throw new NotFoundException("Folder '{$parent}' does not exist.");
     }
     // create the file
     $this->checkContainerForWrite($container);
     // need to be able to write to storage
     $ext = FileUtilities::getFileExtension($path);
     $mime = FileUtilities::determineContentType($ext, '', $local_path);
     $this->putBlobFromFile($container, $path, $local_path, $mime);
 }