Update api for scheduled + update get medias for + update htaccess to make medias accessibles

This commit is contained in:
osaajani 2021-03-21 17:08:05 +01:00
parent 136b3f76ce
commit 0dac72cf54
6 changed files with 99 additions and 62 deletions

1
.gitignore vendored
View File

@ -11,5 +11,6 @@ phinx.*
data/test_write_sms.json
data/test_read_sms.json
data/medias/
!*.dist

View File

@ -1,4 +1,5 @@
RewriteEngine on
RewriteRule ^assets - [L]
RewriteRule ^.well-known - [L]
RewriteRule ^data/medias - [L]
RewriteRule . index.php

View File

@ -23,7 +23,7 @@ namespace controllers\internals;
*
* @return mixed bool|int : false on error, new media id else
*/
public function create(int $id_user, string $path): bool
public function create(int $id_user, string $path)
{
$data = [
'path' => $path,
@ -41,7 +41,7 @@ namespace controllers\internals;
*
* @return mixed bool|int : false on error, the new link id else
*/
public function link_to(int $id_media, int $resource_type, int $resource_id)
public function link_to(int $id_media, string $resource_type, int $resource_id)
{
switch ($resource_type)
{
@ -160,42 +160,39 @@ namespace controllers\internals;
}
/**
* Find medias for a scheduled and a user.
* Find medias for a scheduled.
*
* @param int $id_user : User id
* @param int $id_scheduled : Scheduled id to fin medias for
*
* @return mixed : Medias || false
*/
public function gets_for_scheduled_and_user(int $id_user, int $id_scheduled)
public function gets_for_scheduled(int $id_scheduled)
{
return $this->get_model()->gets_for_scheduled_and_user($id_user, $id_scheduled);
return $this->get_model()->gets_for_scheduled($id_scheduled);
}
/**
* Find medias for a sended and a user.
*
* @param int $id_user : User id
* @param int $id_sended : Scheduled id to fin medias for
*
* @return mixed : Medias || false
*/
public function gets_for_sended_and_user(int $id_user, int $id_sended)
public function gets_for_sended(int $id_sended)
{
return $this->get_model()->gets_for_sended_and_user($id_user, $id_sended);
return $this->get_model()->gets_for_sended($id_sended);
}
/**
* Find medias for a received and a user.
*
* @param int $id_user : User id
* @param int $id_received : Scheduled id to fin medias for
*
* @return mixed : Medias || false
*/
public function gets_for_received_and_user(int $id_user, int $id_received)
public function gets_for_received(int $id_received)
{
return $this->get_model()->gets_for_received_and_user($id_user, $id_received);
return $this->get_model()->gets_for_received($id_received);
}
/**

View File

@ -297,7 +297,7 @@ namespace controllers\internals;
if ($scheduled['mms'])
{
$internal_media = new Media($this->bdd);
$scheduled['medias'] = $internal_media->gets_for_scheduled_and_user($scheduled['id_user'], $scheduled['id']);
$scheduled['medias'] = $internal_media->gets_for_scheduled($scheduled['id']);
}
$phone_to_use = null;

View File

@ -151,6 +151,21 @@ namespace controllers\publics;
$entries[$key]['contacts'] = $this->internal_scheduled->get_contacts($entry['id']);
$entries[$key]['groups'] = $this->internal_scheduled->get_groups($entry['id']);
$entries[$key]['conditional_groups'] = $this->internal_scheduled->get_conditional_groups($entry['id']);
$entries[$key]['medias'] = $this->internal_media->gets_for_scheduled($entry['id']);
}
}
elseif ('received' === $entry_type)
{
foreach ($entries as $key => $entry)
{
$entries[$key]['medias'] = $this->internal_media->gets_for_received($entry['id']);
}
}
elseif ('sended' === $entry_type)
{
foreach ($entries as $key => $entry)
{
$entries[$key]['medias'] = $this->internal_media->gets_for_sended($entry['id']);
}
}
//Special case for group we must add contact because its a join
@ -206,13 +221,40 @@ namespace controllers\publics;
$contacts = $_POST['contacts'] ?? [];
$groups = $_POST['groups'] ?? [];
$conditional_groups = $_POST['conditional_groups'] ?? [];
$files = $_FILES ?? [];
$files = $_FILES['medias'] ?? false;
$numbers = \is_array($numbers) ? $numbers : [$numbers];
$contacts = \is_array($contacts) ? $contacts : [$contacts];
$groups = \is_array($groups) ? $groups : [$groups];
$conditional_groups = \is_array($conditional_groups) ? $conditional_groups : [$conditional_groups];
//Iterate over files to re-create individual $_FILES array
$files_arrays = [];
if ($files === false)
{
$files_arrays = [];
}
elseif (!is_array($files['name'])) //Only one file uploaded
{
$files_arrays[] = $files;
}
else //multiple files
{
foreach ($files as $property_name => $files_values)
{
foreach ($files_values as $file_key => $property_value)
{
if (!isset($files_arrays[$file_key]))
{
$files_arrays[$file_key] = [];
}
$files_arrays[$file_key][$property_name] = $property_value;
}
}
}
$media_ids = [];
if (!$at)
@ -316,45 +358,50 @@ namespace controllers\publics;
}
}
foreach ($files as $file)
if ($mms)
{
$user_media_path = PWD_DATA . '/medias/' . $this->user['id'];
//Create user medias dir if not exists
if (!file_exists($user_media_path))
foreach ($files_arrays as $file)
{
if (!mkdir($user_media_path, fileperms(PWD_DATA), true))
$user_media_path = PWD_DATA . '/medias/' . $this->user['id'];
//Create user medias dir if not exists
if (!file_exists($user_media_path))
{
if (!mkdir($user_media_path, fileperms(PWD_DATA), true))
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['CANNOT_UPLOAD_FILE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_UPLOAD_FILE'] . ' : Because cannot create medias dir on server for the user.';
$this->auto_http_code(false);
return $this->json($return);
}
}
$result = \controllers\internals\Tool::save_uploaded_file($file, $user_media_path);
if ($result['success'] !== true)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['CANNOT_UPLOAD_FILE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_UPLOAD_FILE'] . ' : Because cannot create medias dir on server for the user.';
$return['message'] = self::ERROR_MESSAGES['CANNOT_UPLOAD_FILE'] . $file['name'] . ' with error : ' . $result['content'];
$this->auto_http_code(false);
return $this->json($return);
}
}
$result = \controllers\internals\Tool::save_uploaded_file($file, $user_media_path);
if ($result['success'] !== true)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['CANNOT_UPLOAD_FILE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_UPLOAD_FILE'] . $file['name'] . ' with error : ' . $result['content'];
$this->auto_http_code(false);
$new_filepath = 'medias/' . $this->user['id'] . '/' . $result['content'];
$new_media_id = $this->internal_media->create($this->user['id'], $new_filepath);
if (!$new_media_id)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['CANNOT_CREATE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_CREATE'];
$this->auto_http_code(false);
return $this->json($return);
}
return $this->json($return);
}
$new_filepath = 'medias/' . $this->user['id'] . '/' . $result['content'];
$new_media_id = $this->internal_media->create($this->user['id'], $new_filepath);
if (!$new_media_id)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['CANNOT_CREATE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_CREATE'];
$this->auto_http_code(false);
return $this->json($return);
$media_ids[] = $new_media_id;
}
}

View File

@ -17,26 +17,23 @@ namespace models;
class Media extends StandardModel
{
/**
* Return a media for a user and a scheduled.
* Return all medias for a scheduled.
*
* @param int $id_user : user id
* @param int $id_scheduled : scheduled id
*
* @return array
*/
public function gets_for_scheduled_and_user(int $id_user, int $id_scheduled)
public function gets_for_scheduled(int $id_scheduled)
{
$query = '
SELECT m.id as id, m.user_id as user_id, m.path as path
SELECT m.id as id, m.id_user as id_user, m.path as path
FROM `' . $this->get_table_name() . '` as m
INNER JOIN media_scheduled as ms
ON m.id = ms.id_media
WHERE m.id_user = :id_user
AND ms.id_scheduled = :id_scheduled
WHERE ms.id_scheduled = :id_scheduled
';
$params = [
'id_user' => $id_user,
'id_scheduled' => $id_scheduled,
];
@ -44,26 +41,23 @@ namespace models;
}
/**
* Return a media for a user and a sended.
* Return all medias for a sended.
*
* @param int $id_user : user id
* @param int $id_sended : sended id
*
* @return array
*/
public function gets_for_sended_and_user(int $id_user, int $id_sended)
public function gets_for_sended(int $id_sended)
{
$query = '
SELECT m.id as id, m.user_id as user_id, m.path as path
SELECT m.id as id, m.id_user as id_user, m.path as path
FROM `' . $this->get_table_name() . '` as m
INNER JOIN media_sended as ms
ON m.id = ms.id_media
WHERE m.id_user = :id_user
AND ms.id_sended = :id_sended
WHERE ms.id_sended = :id_sended
';
$params = [
'id_user' => $id_user,
'id_sended' => $id_sended,
];
@ -71,26 +65,23 @@ namespace models;
}
/**
* Return a media for a user and a received.
* Return all medias for a received.
*
* @param int $id_user : user id
* @param int $id_received : received id
*
* @return array
*/
public function gets_for_received_and_user(int $id_user, int $id_received)
public function gets_for_received(int $id_received)
{
$query = '
SELECT m.id as id, m.user_id as user_id, m.path as path
SELECT m.id as id, m.id_user as id_user, m.path as path
FROM `' . $this->get_table_name() . '` as m
INNER JOIN media_received as mr
ON m.id = mr.id_media
WHERE m.id_user = :id_user
AND mr.id_received = :id_received
WHERE mr.id_received = :id_received
';
$params = [
'id_user' => $id_user,
'id_received' => $id_received,
];