/** * @param string $format the name of format. * @inheritdoc */ public function getExtension($format = null) { if ($this->status === self::STATUS_INITIALIZED_FILE) { $storage = $this->context->getStorage(); if ($storage instanceof Storage) { return $storage->getExtension($this->getData(), $format); } } return FileSystemHelper::extension($this->getName($format)); }
/** * Validates file extension. * @param string $extension the extension that must be validated. * @param boolean $validateEncoded whether method must validate encoded in local file system charset extension. * This param internally is used by this method. * @return boolean whether this extension can be used for saving new file. */ protected function isValidExtension($extension, $validateEncoded = true) { $charset = Yii::$app->charset; if (!is_string($extension) || $extension === '' || mb_strpos($extension, '.', 0, $charset) !== false) { return false; } if (!preg_match($this->extensionRegular, $extension)) { return false; } if ($this->containsSpecialChar($extension)) { return false; } $extensionLoweredCase = mb_strtolower($extension, $charset); foreach ($this->disallowedExtensions as $disallowedExt) { if (mb_strtolower($disallowedExt, $charset) === $extensionLoweredCase) { return false; } } if ($validateEncoded) { $encodedExtension = FileSystemHelper::encodeFilename($extension); return $this->isValidExtension($encodedExtension, false); } return true; }
/** * Calculates and returns file extension. * @param string $data data that should be used for loading source file. * @param string|null $format if this param is passed the method will return extension of formatted file. * @return string the MIME type. Null is returned if the MIME type cannot be determined. */ public function getExtension($data, $format = null) { $url = $this->getUrl($data, $format); return urldecode(FileSystemHelper::extension($url)); }
/** * @inheritdoc */ public function format($readFilePath) { $tmpFilePath = FileSystemHelper::getNewTempFilename($this->type); PdfHelper::savePreview($readFilePath, $tmpFilePath); return $tmpFilePath; }
/** * @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; }