2023-03-23 21:55:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace brain\api\v1;
|
|
|
|
|
|
|
|
use brain\utility\FileUploader;
|
2023-03-26 00:44:35 +01:00
|
|
|
use brain\data\Settings;
|
|
|
|
use brain\data\Member;
|
2023-03-23 21:55:34 +01:00
|
|
|
|
|
|
|
class FilesAPI
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function uploadFiles($request, $type = null)
|
|
|
|
{
|
|
|
|
$upload = $request->getUploadedFiles(); //grab uploaded files
|
2023-03-26 00:44:35 +01:00
|
|
|
$options = $request->getParsedBody();
|
2023-03-23 21:55:34 +01:00
|
|
|
$file = $upload['upload_files'][0]; //front end sends one by one for progress tracking, so grab first
|
|
|
|
$type = $file->getClientMediaType();
|
|
|
|
$filesPath = '';
|
|
|
|
$path = date('Y') . '/' . date('m');
|
|
|
|
$response = [];
|
2023-03-26 00:44:35 +01:00
|
|
|
|
2023-03-23 21:55:34 +01:00
|
|
|
switch ($type) {
|
|
|
|
case 'image/jpeg':
|
|
|
|
case 'image/png':
|
|
|
|
case 'image/gif':
|
|
|
|
case 'image/svg':
|
2023-03-26 00:44:35 +01:00
|
|
|
if (isset($options["source"])) {
|
|
|
|
if ($options["source"] == "avatar-upload") {
|
|
|
|
$filesPath = '/assets/images/user/' . $path . '/';
|
|
|
|
Member::updateData(
|
|
|
|
'avi',
|
|
|
|
$filesPath . $file->getClientFileName()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$filesPath = '/assets/images/user/' . $path . '/';
|
|
|
|
Settings::updateGlobalData(
|
|
|
|
'background',
|
|
|
|
$filesPath . '/' . $file->getClientFileName()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$filesPath = '/assets/images/blog/' . $path . '/';
|
|
|
|
}
|
2023-03-23 21:55:34 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case 'video/mp4':
|
|
|
|
$filesPath = '/assets/video/blog/' . $path . '/';
|
|
|
|
break;
|
|
|
|
case 'audio/mpeg':
|
|
|
|
$filesPath = '/assets/sound/blog/' . $path . '/';
|
|
|
|
break;
|
|
|
|
case 'application/pdf':
|
|
|
|
case 'text/plain':
|
|
|
|
case 'text/rtf':
|
|
|
|
$filesPath = '/assets/docs/blog/' . $path . '/';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileUploader::uploadFile('../public' . $filesPath, $file);
|
|
|
|
|
|
|
|
$response = [
|
|
|
|
'message' => "File Uploaded. Great!",
|
|
|
|
"filePath" => $filesPath . urlencode($file->getClientFileName()),
|
|
|
|
"fileName" => urlencode($file->getClientFileName()),
|
|
|
|
'type' => $type,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|