Esempio n. 1
0
 /**
  * Saves UploadedFile from fileData if exists and puts in it FileInfo
  * @return bool
  */
 public function save()
 {
     if (empty($this->uploadedFile)) {
         return $this->fileInfo ? true : false;
     }
     $file = $this->uploadedFile;
     $fileHash = md5_file($file->tempName);
     // just in case if file already exists
     $existingFileInfo = FileInfo::findOne(['hash' => $fileHash]);
     if ($existingFileInfo) {
         $this->fileInfo = $existingFileInfo;
         return true;
     }
     $filePath = Yii::getAlias('@files/' . $fileHash . '.' . $file->extension);
     if (!$file->saveAs($filePath)) {
         return false;
     }
     $mimeType = MimeType::findOne(['name' => FileHelper::getMimeType($filePath)]);
     $fileInfo = new FileInfo(['filePath' => $filePath, 'originalName' => $file->name, 'hash' => $fileHash, 'mimeTypeId' => $mimeType->id, 'size' => $file->size]);
     $result = $fileInfo->save();
     $this->fileInfo = $result ? $fileInfo : null;
     return $result;
 }
Esempio n. 2
0
 public function getMimeType()
 {
     return $this->hasOne(MimeType::className(), ['id' => 'mime_type_id']);
 }