예제 #1
0
파일: Storage.php 프로젝트: alex-dwt/file
 /**
  * Calculates and returns file mime type.
  * @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 mime type of formatted file.
  * @return string the MIME type. Null is returned if the MIME type cannot be determined.
  */
 public function getMimeType($data, $format = null)
 {
     $path = $this->getReadFilePath($data, $format);
     return FileSystemHelper::getMimeType($path, null, $this->tryDetectMimeTypeByExtension);
 }
예제 #2
0
파일: File.php 프로젝트: alex-dwt/file
 /**
  * Gets mime type of file.
  * @param string $format the name of formatted file version. Null (default) meaning source file.
  * @return string|null mime type of file. Null meaning file is empty.
  * @throws InvalidCallException if `$format` is not null but file has not been initialized yet.
  */
 public function getType($format = null)
 {
     if ($format === null && $this->_type !== null) {
         return $this->_type;
     }
     if ($this->status === self::STATUS_INITIALIZED_FILE) {
         $storage = $this->context->getStorage();
         if ($storage instanceof Storage) {
             $result = $storage->getMimeType($this->getData(), $format);
         } else {
             $tempName = $this->getTempName($format);
             $result = FileSystemHelper::getMimeType($tempName);
         }
         if ($format === null) {
             $this->_type = $result;
         }
         return $result;
     } elseif ($format !== null) {
         throw new InvalidCallException('You cannot call ' . __FUNCTION__ . ' for formatted version of file when the last has not been initialized yet.');
     }
     return null;
 }
예제 #3
0
 /**
  * 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;
 }