public static function createCopy($id, $model_name, $model_id) { // Get or create model name id $modelMN = new D3filesModelName(); $model_name_id = $modelMN->getByName($model_name, true); $model = D3filesModel::findOne($id); $newModel = new D3filesModel(); $newModel->d3files_id = $model->d3files_id; $newModel->is_file = 0; $newModel->model_name_id = $model_name_id; $newModel->model_id = $model_id; $newModel->save(); }
/** * @param string $name model name * @param bool $addIfNotSet add/don't add new, if name not exist * * @return integer model name id */ public function getByName($name, $addIfNotSet) { $model = D3filesModelName::find()->select('id')->where(['name' => $name])->one(); if ($model) { return $model->id; } if (!$addIfNotSet) { return null; } $newModel = new D3filesModelName(); $newModel->name = $name; $newModel->save(); return $newModel->id; }
public function run($id) { if (!($fileModel = D3filesModel::findOne(['id' => $id, 'deleted' => 0]))) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } if (!($file = D3files::findOne($fileModel->d3files_id))) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } if (!($fileModelName = D3filesModelName::findOne($fileModel->model_name_id))) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } /** * validate modelname */ if (Yii::$app->getModule('d3files')->disableController) { if ($fileModelName->name != $this->modelName) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } } // Check access rights to the record the file is attached to D3files::performReadValidation($fileModelName->name, $fileModel->model_id); $modelName = $fileModelName->name; if (!$fileModel->is_file) { if (!($realFileModel = D3filesModel::findOne(['d3files_id' => $fileModel->d3files_id, 'is_file' => 1]))) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } if (!($realfileModelName = D3filesModelName::findOne($realFileModel->model_name_id))) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } $modelName = $realfileModelName->name; //$modelName } $fileHandler = new FileHandler(['model_name' => $modelName, 'model_id' => $file->id, 'file_name' => $file->file_name]); $fileHandler->download(); }
public function run($id, $hash) { // Pause every request sleep(1); /** * Validate both parameters: * id - only digits > 0 * hash - only hex, exactly 32 chars long */ if (!preg_match('#^[1-9][0-9]*$#', $id)) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } $hash = strtoupper($hash); if (!preg_match('#^[0-9A-F]{32}$#', $hash)) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } if (!($fileModelShared = D3filesModelShared::find()->where(['and', "id={$id}", "hash='{$hash}'", "left_loadings>0", "expire_date>=CURDATE()"])->one())) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } if (!($fileModel = D3filesModel::findOne(['id' => $fileModelShared->d3files_model_id, 'deleted' => 0, 'is_file' => 1]))) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } if (!($file = D3files::findOne($fileModel->d3files_id))) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } if (!($fileModelName = D3filesModelName::findOne($fileModel->model_name_id))) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } $fileModelShared->left_loadings--; $fileModelShared->save(); $fileHandler = new FileHandler(['model_name' => $fileModelName->name, 'model_id' => $file->id, 'file_name' => $file->file_name]); $fileHandler->download(); }
public function run($id) { // $id here is id for model to which will be attached attachments Yii::$app->response->format = Response::FORMAT_JSON; if (!isset($_FILES['upload_file'])) { throw new NotFoundHttpException(Yii::t('d3files', 'File not uploaded.')); } // If controller actions are not disabled, use $_POST['model_name'] if (!Yii::$app->getModule('d3files')->disableController) { $this->modelName = Yii::$app->request->post('model_name'); } if (empty($this->modelName)) { throw new HttpException(422, Yii::t('d3files', 'mandatory POST parameter modelName is not set')); } // Check access rights to the record the file is attached to D3files::performReadValidation($this->modelName, $id); $tmp_id = uniqid(); $fileHandler = new FileHandler(['model_name' => $this->modelName, 'model_id' => $tmp_id, 'file_name' => $_FILES['upload_file']['name']]); $fileHandler->upload(); $model = new D3files(); $model->file_name = $_FILES['upload_file']['name']; $model->add_datetime = new \yii\db\Expression('NOW()'); $model->user_id = Yii::$app->user->getId(); if ($model->save()) { // Get or create model name id $modelMN = new D3filesModelName(); $model_name_id = $modelMN->getByName($this->modelName, true); $modelM = new D3filesModel(); $modelM->d3files_id = $model->id; $modelM->is_file = 1; $modelM->model_name_id = $model_name_id; $modelM->model_id = $id; $modelM->save(); $fileHandler->rename($model->id); } else { $fileHandler->remove(); throw new HttpException(500, Yii::t('d3files', 'Insert DB record failed')); } $renderParam = ['id' => $model->id, 'file_name' => $model->file_name, 'file_model_id' => $modelM->id]; return $this->controller->renderFile(Yii::$app->getModule('d3files')->getView('d3files/upload'), $renderParam); }
public function run($id) { Yii::$app->response->format = Response::FORMAT_JSON; if (!($fileModel = D3filesModel::findOne(['id' => $id, 'deleted' => 0]))) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } if (!($fileModelName = D3filesModelName::findOne($fileModel->model_name_id))) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } /** * validate modelname */ if (Yii::$app->getModule('d3files')->disableController) { if ($fileModelName->name != $this->modelName) { throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.')); } } // Check access rights to the record the file is attached to D3files::performReadValidation($fileModelName->name, $fileModel->model_id); $fileModel->deleted = 1; $fileModel->save(); return $this->controller->renderFile(Yii::$app->getModule('d3files')->getView('d3files/delete')); }
/** * Upload yii\web\UploadedFile * @param UploadedFile $uploadFile * @param string $modelName model name with name space * @param int $modelId * @throws Exception */ public static function saveYii2UploadFile(UploadedFile $uploadFile, $modelName, $modelId) { $fileHandler = new FileHandler(['model_name' => $modelName, 'model_id' => uniqid(), 'file_name' => $uploadFile->name, 'file_types' => '*']); $fileHandler->uploadYii2UloadFile($uploadFile); $model = new D3files(); $model->file_name = $uploadFile->name; $model->add_datetime = new \yii\db\Expression('NOW()'); $model->user_id = \Yii::$app->person->user_id; if ($model->save()) { // Get or create model name id $modelMN = new D3filesModelName(); $model_name_id = $modelMN->getByName($modelName, true); $modelM = new D3filesModel(); $modelM->d3files_id = $model->id; $modelM->is_file = 1; $modelM->model_name_id = $model_name_id; $modelM->model_id = $modelId; $modelM->save(); $fileHandler->rename($model->id); } else { $fileHandler->remove(); throw new Exception(500, Yii::t('d3files', 'Insert DB record failed')); } }