/** * @param array $data * @param bool $extract * @param bool $clean * @param bool $checkExist * * @return array */ protected function handleFolderContentFromData($data, $extract = false, $clean = false, $checkExist = false) { $out = []; if (!empty($data) && ArrayUtils::isArrayNumeric($data)) { foreach ($data as $key => $resource) { switch (ArrayUtils::get($resource, 'type')) { case 'folder': $name = ArrayUtils::get($resource, 'name', ''); $srcPath = ArrayUtils::get($resource, 'source_path'); if (!empty($srcPath)) { $srcContainer = ArrayUtils::get($resource, 'source_container', $this->container); // copy or move if (empty($name)) { $name = FileUtilities::getNameFromPath($srcPath); } $fullPathName = $this->folderPath . $name . '/'; $out[$key] = ['name' => $name, 'path' => $fullPathName, 'type' => 'folder']; try { $this->driver->copyFolder($this->container, $fullPathName, $srcContainer, $srcPath, true); $deleteSource = ArrayUtils::getBool($resource, 'delete_source'); if ($deleteSource) { $this->driver->deleteFolder($this->container, $srcPath, true); } } catch (\Exception $ex) { $out[$key]['error'] = ['message' => $ex->getMessage()]; } } else { $fullPathName = $this->folderPath . $name . '/'; $content = ArrayUtils::get($resource, 'content', ''); $isBase64 = ArrayUtils::getBool($resource, 'is_base64'); if ($isBase64) { $content = base64_decode($content); } $out[$key] = ['name' => $name, 'path' => $fullPathName, 'type' => 'folder']; try { $this->driver->createFolder($this->container, $fullPathName, $content); } catch (\Exception $ex) { $out[$key]['error'] = ['message' => $ex->getMessage()]; } } break; case 'file': $name = ArrayUtils::get($resource, 'name', ''); $srcPath = ArrayUtils::get($resource, 'source_path'); if (!empty($srcPath)) { // copy or move $srcContainer = ArrayUtils::get($resource, 'source_container', $this->container); if (empty($name)) { $name = FileUtilities::getNameFromPath($srcPath); } $fullPathName = $this->folderPath . $name; $out[$key] = ['name' => $name, 'path' => $fullPathName, 'type' => 'file']; try { $this->driver->copyFile($this->container, $fullPathName, $srcContainer, $srcPath, true); $deleteSource = ArrayUtils::getBool($resource, 'delete_source'); if ($deleteSource) { $this->driver->deleteFile($this->container, $srcPath); } } catch (\Exception $ex) { $out[$key]['error'] = ['message' => $ex->getMessage()]; } } elseif (isset($resource['content'])) { $fullPathName = $this->folderPath . $name; $out[$key] = ['name' => $name, 'path' => $fullPathName, 'type' => 'file']; $content = ArrayUtils::get($resource, 'content', ''); $isBase64 = ArrayUtils::getBool($resource, 'is_base64'); if ($isBase64) { $content = base64_decode($content); } try { $this->driver->writeFile($this->container, $fullPathName, $content); } catch (\Exception $ex) { $out[$key]['error'] = ['message' => $ex->getMessage()]; } } break; } } } return $out; }
/** * @param string $container * @param string $path * @param bool $include_content * @param bool $content_as_base * * @throws NotFoundException * @throws \Exception * @return array */ public function getFileProperties($container, $path, $include_content = false, $content_as_base = true) { if (!$this->fileExists($container, $path)) { throw new NotFoundException("File '{$path}' does not exist in storage."); } $file = static::addContainerToName($container, $path); $shortName = FileUtilities::getNameFromPath($path); $ext = FileUtilities::getFileExtension($file); $temp = stat($file); $data = ['path' => $path, 'name' => $shortName, 'content_type' => FileUtilities::determineContentType($ext, '', $file), 'last_modified' => gmdate('D, d M Y H:i:s \\G\\M\\T', ArrayUtils::get($temp, 'mtime', 0)), 'content_length' => ArrayUtils::get($temp, 'size', 0)]; if ($include_content) { $contents = file_get_contents($file); if (false === $contents) { throw new InternalServerErrorException('Failed to retrieve file properties.'); } if ($content_as_base) { $contents = base64_encode($contents); } $data['content'] = $contents; } return $data; }
/** * @param string $container * @param string $path * @param bool $include_content * @param bool $content_as_base * * @throws NotFoundException * @throws \Exception * @return array */ public function getFileProperties($container, $path, $include_content = false, $content_as_base = true) { if (!$this->containerExists($container) || !$this->blobExists($container, $path)) { throw new NotFoundException("File '{$path}' does not exist in storage."); } $blob = $this->getBlobProperties($container, $path); $shortName = FileUtilities::getNameFromPath($path); $blob['path'] = $path; $blob['name'] = $shortName; if ($include_content) { $data = $this->getBlobData($container, $path); if ($content_as_base) { $data = base64_encode($data); } $blob['content'] = $data; } return $blob; }