/**
* create function
* @method : POST
*/
public function create()
{
$chatRoomMediaModel = new ChatRoomMediaModel();
$customConfig = new ModuleCommonConfig();
$imageService = \CodeIgniter\Config\Services::image();
$chatRoomMediaEntity = new ChatRoomMediaEntity();
if ($this->request->getPost()) {
$rules = [
'image' => 'uploaded[image]|max_size[image,4096]|ext_in[image,png,webp,jpeg,jpg,gif]',
'chatRoomId' => 'required'
];
if (!$this->validate($rules)) {
return $this->respond([
'error' => $this->validator->getErrors(),
'success' => false
], ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Shared.api.validation'));
}
$chatRoomMediaEntity->chatRoomId = $this->request->getPost('chatRoomId');
if (isset($_FILES['image'])) {
foreach ($this->request->getFileMultiple('image') as $avatar) {
$avatar->move($customConfig->uploadDirectory . '/chat_room', time() . '.' . $avatar->getClientExtension());
$chatRoomMediaEntity->path = $avatar->getName();
$chatRoomMediaEntity->editPath();
if ($avatar->getClientExtension() != 'gif') {
$imageService->withFile(ROOTPATH . $chatRoomMediaEntity->path)
->withResource()
->save(ROOTPATH . $chatRoomMediaEntity->path, 90);
}
if (!$chatRoomMediaModel->save($chatRoomMediaEntity)) {
return $this->respond([
'error' => $chatRoomMediaModel->errors(),
'success' => false,
], ResponseInterface::HTTP_BAD_REQUEST, lang('Shared.api.reject'));
}
}
}
return $this->respond([
'data' => ''
], ResponseInterface::HTTP_CREATED, lang('Shared.api.save'));
}
}