public function createOne() { //get saved data $existFilesData = Session::get('files'); $files = unserialize($existFilesData); //get paths to upload directory $path = Config::getModuleData('menu', 'full_path'); $relativePath = Config::getModuleData('menu', 'relative_path'); //get new file,that saved in /tmp directory $newFileData = Request::getFileUpload()->getFile('files'); $editor = new Manager($newFileData, $path); //validate file name $editor->renameFileName(); //merge new and exist files data if ($existFilesData) { $fileObjects = $files; $fileObjects[uniqid()] = $editor->getFile(); } else { $fileObjects = [uniqid() => $editor->getFile()]; } Session::set('files', serialize($fileObjects)); $file = $editor->getFile(); return array('id' => array_search($editor->getFile(), $fileObjects), 'path' => $relativePath . $file->getName() . '.' . $file->getExtension()); }
* required = true, * type="File" * ), * @SWG\ResponseMessage(code=400, message="Invalid file type"), * @SWG\ResponseMessage(code=200, message="File was uploaded") * ) * ) */ return function () { /** * @var Bootstrap $this */ /** @var \Bluz\Http\File $file */ //if (Request::getFileUpload()->getFile('files')->getType() == 'image') { if (Request::getInstance()->isPost()) { $upload = new Musician\Upload(); $path = Config::getModuleData('musician', 'upload_path'); if (empty($path)) { throw new Exception('Upload_path is not configured'); } $upload->setUploadDir($path . "/musician"); $file = $upload->upload(); $url = "/uploads/musician/" . $file->getFullName(); return ['fullName' => $file->getFullName(), 'url' => $url]; } else { return []; } // } else { // throw new Exception('Invalid file type'); // } };
/** * Drop photo after the test */ public static function tearDownAfterClass() { Db::delete('media')->where('userId', [1])->execute(); $path = Config::getModuleData('media', 'upload_path') . '/1'; Tools\Cleaner::delete($path); }
/** * Generates cookie for authentication * * @throws \Bluz\Db\Exception\DbException */ public function generateCookie() { $hash = hash('md5', microtime(true)); $ttl = Config::getModuleData('users', 'rememberMe'); $this->delete(['userId' => Auth::getIdentity()->id, 'foreignKey' => Auth::getIdentity()->login, 'provider' => self::PROVIDER_COOKIE, 'tokenType' => self::TYPE_ACCESS]); $row = new Row(); $row->userId = Auth::getIdentity()->id; $row->foreignKey = Auth::getIdentity()->login; $row->provider = self::PROVIDER_COOKIE; $row->tokenType = self::TYPE_ACCESS; $row->expired = gmdate('Y-m-d H:i:s', time() + $ttl); $row->tokenSecret = $this->generateSecret(Auth::getIdentity()->id); $row->token = hash('md5', $row->tokenSecret . $hash); $row->save(); Response::setCookie('rToken', $hash, time() + $ttl, '/'); Response::setCookie('rId', Auth::getIdentity()->id, time() + $ttl, '/'); }
public function readOne($primary) { $filesArray = unserialize(Session::get('files')); $path = Config::getModuleData('menu', 'full_path'); if ($filesArray) { foreach ($filesArray as $file) { $filename = $path . $file->getFullName(); if (is_file($filename)) { unlink($filename); } } } Session::delete('files'); return parent::readOne($primary); }
// save original name $original = $file->getName(); // rename file to date/time stamp $file->setName(date('Ymd_Hi')); // switch to JSON response $this->useJson(); if (!$this->user()) { throw new Exception('User not found'); } $userId = $this->user()->id; // directory structure: // uploads/ // %userId%/ // %module%/ // filename.ext $path = Config::getModuleData('media', 'upload_path'); if (empty($path)) { throw new ConfigException('Upload path is not configured'); } $file->moveTo($path . '/' . $userId . '/media'); // save media data $media = new Media\Row(); $media->userId = $userId; $media->module = 'media'; $media->type = $file->getMimeType(); $media->title = $original; $media->file = 'uploads/' . $userId . '/media/' . $file->getFullName(); $media->preview = $media->file; $media->save(); // displaying file return array('filelink' => $media->file);