refactor all media create to centralise copy of medias and save, etc.

This commit is contained in:
osaajani 2021-03-26 23:32:29 +01:00
parent 92146ba8e1
commit 04a40049ce
18 changed files with 400 additions and 367 deletions

View file

@ -362,12 +362,15 @@ namespace controllers\publics;
{
foreach ($files_arrays as $file)
{
$new_media_id = $this->internal_media->upload_and_create_for_user($this->user['id'], $file);
if (!$new_media_id)
try
{
$new_media_id = $this->internal_media->upload_and_create_for_user($this->user['id'], $file);
}
catch (\Exception $e)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['CANNOT_CREATE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_CREATE'] . ' : Cannot upload and create media file ' . $file['name'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_CREATE'] . ' : Cannot upload and create media file ' . $file['name'] . ' : ' . $e->getMessage();
$this->auto_http_code(false);
return $this->json($return);

View file

@ -183,58 +183,10 @@ use Monolog\Logger;
}
$sms = $response['sms'];
$mms = !empty($sms['mms']);
$mms = (bool) $sms['mms'] ?? false;
$medias = empty($sms['medias']) ? [] : $sms['medias'];
$media_ids = [];
//We create medias to link to the sms
if ($mms)
{
foreach ($medias as $media)
{
try
{
$media['mimetype'] = empty($media['mimetype']) ? mime_content_type($media['filepath']) : $media['mimetype'];
$mimey = new \Mimey\MimeTypes;
$extension = empty($media['extension']) ? $mimey->getExtension($media['mimetype']) : $media['extension'];
$new_filename = \controllers\internals\Tool::random_uuid() . '.' . $extension;
$new_filedir = PWD_DATA . '/medias/' . $this->user['id'];
$new_filerelpath = 'medias/' . $this->user['id'] . '/' . $new_filename;
$new_filepath = $new_filedir . '/' . $new_filename;
//Create user dir if not exists
if (!file_exists($new_filedir))
{
if (!mkdir($new_filedir, fileperms(PWD_DATA), true))
{
throw new \Exception('Cannot create dir ' . $new_filedir . ' to copy media : ' . json_encode($media));
}
}
if (!rename($media['filepath'], $new_filepath))
{
throw new \Exception('Cannot copy media : ' . json_encode($media) . ' to ' . $new_filepath);
}
$new_media_id = $this->internal_media->create($this->user['id'], $new_filerelpath);
if (!$new_media_id)
{
throw new \Exception('Cannot save into db media : ' . json_encode($media));
}
$media_ids[] = $new_media_id;
}
catch (\Throwable $t)
{
$this->logger->error($t->getMessage());
continue;
}
}
}
$response = $this->internal_received->receive($this->user['id'], $id_phone, $sms['text'], $sms['origin'], $sms['at'], \models\Received::STATUS_UNREAD, $mms, $media_ids);
$response = $this->internal_received->receive($this->user['id'], $id_phone, $sms['text'], $sms['origin'], $sms['at'], \models\Received::STATUS_UNREAD, $mms, $medias);
if ($response['error'])
{
$this->logger->error('Failed receive message : ' . json_encode($sms) . ' with error : ' . $response['error_message']);

View file

@ -120,7 +120,7 @@ namespace controllers\publics;
$medias = $this->internal_media->gets_for_sended($sended['id']);
foreach ($medias as &$media)
{
$media = HTTP_PWD_DATA . '/' . $media['path'];
$media = HTTP_PWD_DATA_PUBLIC . '/' . $media['path'];
}
}
@ -149,7 +149,7 @@ namespace controllers\publics;
$medias = $this->internal_media->gets_for_received($received['id']);
foreach ($medias as &$media)
{
$media = HTTP_PWD_DATA . '/' . $media['path'];
$media = HTTP_PWD_DATA_PUBLIC . '/' . $media['path'];
}
}
@ -170,7 +170,7 @@ namespace controllers\publics;
$medias = $this->internal_media->gets_for_scheduled($scheduled['id']);
foreach ($medias as &$media)
{
$media = HTTP_PWD_DATA . '/' . $media['path'];
$media = HTTP_PWD_DATA_PUBLIC . '/' . $media['path'];
}
}
@ -288,11 +288,14 @@ namespace controllers\publics;
{
foreach ($files_arrays as $file)
{
$new_media_id = $this->internal_media->upload_and_create_for_user($_SESSION['user']['id'], $file);
if (!$new_media_id)
try
{
$new_media_id = $this->internal_media->create_from_uploaded_file_for_user($_SESSION['user']['id'], $file);
}
catch (\Exception $e)
{
$return['success'] = false;
$return['message'] = 'Impossible d\'upload et d\'enregistrer le fichier ' . $file['name'];
$return['message'] = $e->getMessage();
echo json_encode($return);
return false;

View file

@ -332,10 +332,13 @@ namespace controllers\publics;
{
foreach ($files_arrays as $file)
{
$new_media_id = $this->internal_media->upload_and_create_for_user($_SESSION['user']['id'], $file);
if (!$new_media_id)
try
{
\FlashMessage\FlashMessage::push('danger', 'Impossible d\'upload et d\'enregistrer le fichier ' . $file['name']);
$new_media_id = $this->internal_media->create_from_uploaded_file_for_user($_SESSION['user']['id'], $file);
}
catch (\Exception $e)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible d\'upload et d\'enregistrer le fichier ' . $file['name'] . ':' . $e->getMessage());
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
@ -459,8 +462,11 @@ namespace controllers\publics;
{
foreach ($files_arrays as $file)
{
$new_media_id = $this->internal_media->upload_and_create_for_user($_SESSION['user']['id'], $file);
if (!$new_media_id)
try
{
$new_media_id = $this->internal_media->create_from_uploaded_file_for_user($_SESSION['user']['id'], $file);
}
catch (\Exception $e)
{
continue 2;
}