Skip to content

Commit

Permalink
add FilesController::ajax_file_upload() api fc2blog#243
Browse files Browse the repository at this point in the history
  • Loading branch information
uzulla committed May 18, 2021
1 parent 514ce33 commit 6e377f6
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions app/src/Web/Controller/Admin/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,49 @@ public function ajax_delete(Request $request): string
return "admin/common/json.twig";
}

/**
* ajaxによるファイルアップロード受付
* @param Request $request
* @return string
*/
public function ajax_file_upload(Request $request): string
{
if ($this->isInvalidAjaxRequest($request) || $request->method !== 'POST' || !$request->isValidSig()) {
return $this->error403();
}

$files_model = new FilesModel();
$blog_id = $this->getBlogId($request);

// アップロード時処理
if ($request->file('file')) {
// 新規登録処理
$errors = [];
$errors['file'] = $files_model->insertValidate($request->file('file'), $request->get('file'), $data_file);
if (empty($errors['file'])) {
$data_file['blog_id'] = $blog_id;
$tmp_name = $data_file['tmp_name'];
unset($data_file['tmp_name']);
if ($id = $files_model->insert($data_file)) {
// ファイルの移動
$data_file['id'] = $id;
$move_file_path = App::getUserFilePath($data_file, true);
App::mkdir($move_file_path);
if (defined("THIS_IS_TEST")) {
rename($tmp_name, $move_file_path);
} else {
move_uploaded_file($tmp_name, $move_file_path);
}
$this->setContentType("application/json; charset=utf-8");
$this->set('json', ['status' => 'ok']);
return "admin/common/json.twig";
}
}
}

$this->setContentType("application/json; charset=utf-8");
$this->set('json', ['status' => 'ng']);
return "admin/common/json.twig";
}
}

0 comments on commit 6e377f6

Please sign in to comment.