예제 #1
0
 /**
  * Generates filename for new file according to storage's configuration.
  * @param string|null $originFilename name of origin file.
  * If `saveOriginNames` is true the storage will analyse and use this property for new filename.
  * Otherwise the storage will analyse and use only extension from this property.
  * @return string generated new filename.
  */
 public function createNewFilename($originFilename = null)
 {
     if ($originFilename !== null) {
         $extension = FileSystemHelper::extension($originFilename);
         $extension = FileSystemHelper::normalizeFilename($extension);
         if (!$this->isValidExtension($extension)) {
             $extension = null;
         }
         if ($this->saveOriginNames) {
             $filename = FileSystemHelper::filename($originFilename);
             $filename = FileSystemHelper::normalizeFilename($filename);
             if (!$this->isValidFileName($filename)) {
                 $filename = null;
             }
         } else {
             $filename = null;
         }
         $charset = Yii::$app->charset;
         if ($filename !== null) {
             if (mb_strlen($extension === null ? $filename : "{$filename}.{$extension}", $charset) > $this->maxLength) {
                 $filename = null;
             }
         }
         if ($filename === null && $extension !== null) {
             if ($this->fileNamesLength + 1 + mb_strlen($extension, $charset) > $this->maxLength) {
                 $extension = null;
             }
         }
     }
     if (!isset($filename)) {
         $filename = $this->generateRandomString($this->fileNamesLength);
     }
     return isset($extension) ? "{$filename}.{$extension}" : $filename;
 }
예제 #2
0
파일: File.php 프로젝트: alex-dwt/file
 /**
  * Note! This method returns filename (like in `pathinfo(..., PATHINFO_FILENAME)), NOT basename (like `basename()`).
  * It is used because the same logic was in [[\yii\web\UploadedFile]] (for back capability).
  * 
  * @param string $format the name of format.
  * @inheritdoc
  */
 public function getBaseName($format = null)
 {
     if ($this->status === self::STATUS_INITIALIZED_FILE) {
         $storage = $this->context->getStorage();
         if ($storage instanceof Storage) {
             return $storage->getFileName($this->getData(), $format);
         }
     }
     return FileSystemHelper::filename($this->getName($format));
 }
예제 #3
0
파일: Storage.php 프로젝트: alex-dwt/file
 /**
  * Calculates and returns file name.
  * @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 name of formatted file.
  * @return string the MIME type. Null is returned if the MIME type cannot be determined.
  */
 public function getFileName($data, $format = null)
 {
     $url = $this->getUrl($data, $format);
     return urldecode(FileSystemHelper::filename($url));
 }