/** * @todo its a copy from the old colde, refactor code * @param string $fileSource * @param string $fileName * @param int $folderId */ public function addFile($fileSource, $fileName, $folderId = 0, $isHidden = false) { if (empty($fileSource) || empty($fileName)) { throw new Exception("Unable to create file where file source and/or file name is empty."); } $fileInfo = FileHelper::getFileInfo($fileName); $baseName = Inflector::slug($fileInfo->name, '-'); $fileHashName = Storage::createFileHash($fileName); $fileHash = FileHelper::getFileHash($fileSource); $mimeType = FileHelper::getMimeType($fileSource); $newName = implode([$baseName . '_' . $fileHashName, $fileInfo->extension], '.'); $savePath = $this->serverPath . '/' . $newName; if (is_uploaded_file($fileSource)) { if (!@move_uploaded_file($fileSource, $savePath)) { throw new Exception("error while moving uploaded file from {$fileSource} to {$savePath}"); } } else { if (!@copy($fileSource, $savePath)) { throw new Exception("error while copy file from {$fileSource} to {$savePath}."); } } $model = new StorageFile(); $model->setAttributes(['name_original' => $fileName, 'name_new' => $baseName, 'name_new_compound' => $newName, 'mime_type' => $mimeType, 'extension' => strtolower($fileInfo->extension), 'folder_id' => (int) $folderId, 'hash_file' => $fileHash, 'hash_name' => $fileHashName, 'is_hidden' => $isHidden ? 1 : 0, 'file_size' => @filesize($savePath)]); if ($model->validate()) { if ($model->save()) { $this->deleteHasCache($this->fileCacheKey); $this->_filesArray[$model->id] = $model->toArray(); return $this->getFile($model->id); } } return false; }