/** * Saves formatted version of file (e.g. any resized image for source image) by copying from another file with needed content. * The method returns new data that can be used for loading source file. * * Note: override this method in subclass if your storage allows copy method (more quickly). * * @param string $data data that can be used for loading source file. * @param string $sourceFilePath path to file with content of the formatted file. * @param string $formatName name of the format. * @return string|boolean string data that can be used for loading source file int he future. * False meaning formatted file was not save. */ public function saveFormattedFileByCopying($data, $sourceFilePath, $formatName) { if (false === ($content = @file_get_contents($sourceFilePath))) { Yii::warning("Cannot read content of the file '{$sourceFilePath}'.", __METHOD__); return false; } $decodedFilePath = FileSystemHelper::decodeFilename($sourceFilePath); $extension = $decodedFilePath === false ? false : FileSystemHelper::extension($decodedFilePath); return $this->saveFormattedFile($data, $content, $formatName, $extension); }
/** * @inheritdoc */ public function getAllFiles() { $rootDirectory = $this->getRootDirectory(); $fileDirs = FileSystemHelper::findDirectories($rootDirectory, ['recursive' => false]); $result = []; foreach ($fileDirs as $fileDir) { $files = FileSystemHelper::findFiles($fileDir, ['recursive' => false]); $folder = FileSystemHelper::basename($fileDir); foreach ($files as $fsFile) { $file = FileSystemHelper::decodeFilename($fsFile); if ($file !== false) { $result[] = "{$folder}/" . FileSystemHelper::basename($file); } } } return $result; }
/** * @internal internally used in [[self::generateFormat()]] & [[self::generateFormats()]]. * Generates formatted version of file. * * @param string $data string data that can be used for manipulating with file. * @param string $format the name of format. * @param boolean $regenerate whether formatted file must be generated even if it exists. * @param string|null $tmpNameDependentFile path to tmp filename of dependent formatted version. * It's used only for [[FromFormatter]]. 'Dependent' means the temp filename of [[FromFormatter::$from]] file. * * @return array array that contains 2 elements: * * - string (index 0), new data that can be used for manipulating with file. * - string (index 1), temp file path to the new formatted version of file. * - boolean (index 2), whether temp file can be unlinked. * * @throws InvalidConfigException if cannot save file. */ protected function generateFormatInternal($data, $format, $regenerate = false, $tmpNameDependentFile = null) { $storage = $this->getStorage(); if (!$regenerate && $storage->fileExists($data, $format)) { return [$data, $storage->getReadFilePath($data, $format), false]; } $formatter = $this->getFormatter($format); if ($formatter instanceof FromFormatter && ($tmpNameDependentFile !== null || $storage->fileExists($data, $formatter->from))) { if ($tmpNameDependentFile === null) { $tmpNameDependentFile = $storage->getReadFilePath($data, $formatter->from); } $formattedTmpFile = $formatter->format($tmpNameDependentFile, true); } else { $readFilePath = $storage->getReadFilePath($data); $formattedTmpFile = $formatter->format($readFilePath); } if ($storage instanceof Storage) { $resultData = $storage->saveFormattedFileByCopying($data, $formattedTmpFile, $format); } else { if (false === ($content = @file_get_contents($formattedTmpFile))) { Yii::warning("Cannot read content of the file '{$formattedTmpFile}'.", __METHOD__); $resultData = false; } else { $decodedFromattedTmpFile = FileSystemHelper::decodeFilename($formattedTmpFile); $formattedTmpFileExt = $decodedFromattedTmpFile === false ? false : FileSystemHelper::extension($decodedFromattedTmpFile); $resultData = $storage->saveFormattedFile($data, $content, $format, $formattedTmpFileExt); } } if ($resultData === false) { throw new InvalidConfigException("Cannot save formatted as '{$format}' file for '{$data}' in the '{$this->name}' context."); } return [$resultData, $formattedTmpFile, true]; }
/** * Parses extension from file path. * It returns [[self::$extension]] is the last is set. * Otherwise the method tries to detect file mime type. * If mime type was not detected the method tries to parse file extension. * @param string $readFilePath path to file which extension must be parsed. * @return string|null parsed extension in local file system charset or null if extension is unknown. */ protected function parseFileExtension($readFilePath) { if ($this->extension !== null) { return $this->extension; } // try to determine mime type $mimeType = FileSystemHelper::getMimeType($readFilePath, null, true); if ($mimeType !== null) { $extensions = FileSystemHelper::getExtensionsByMimeType($mimeType, $this->mimeMagicFile); if (is_array($extensions) && count($extensions)) { return reset($extensions); } } // try to parse extension $decodedReadFilePath = FileSystemHelper::decodeFilename($readFilePath); if ($decodedReadFilePath === false) { return null; } $extension = FileSystemHelper::extension($decodedReadFilePath); if ($extension === null || $extension === '') { return $extension; } $fsExtension = FileSystemHelper::encodeFilename($extension); return $fsExtension === false ? null : $fsExtension; }