fipamo/app/Services/FileUploadService.php
ro 6ce5e91624
file upload service added
ported over the new file uploader from the old build and made it a
service make for from some additonal file processing
i.e. image optimization or video converstion before it is saved to the system
2024-03-11 16:32:38 -06:00

78 lines
2.7 KiB
PHP

<?php
namespace App\Services;
class FileUploadService
{
public function __construct()
{
}
public static function handleFile($request, $type = null)
{
//$upload = $request->getUploadedFiles(); //grab uploaded files
//$options = $request->getParsedBody();
//$file = $upload['upload_files'][0];
//front end sends one by one for progress tracking, so grab first
$file = $request->file('upload_files');
$type = $file[0]->extension();
$public_path = '../public';
$filesPath = '';
$path = date('Y') . '/' . date('m');
$response = [];
switch ($type) {
case 'jpeg':
case 'jpg':
case 'png':
case 'gif':
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 . '/';
}
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 = [];
try {
// if does not exist, so lets create it.
//var_dump($filesPath);
if (!is_dir($public_path . $filesPath)) {
mkdir($public_path . $filesPath, 0755, true);
}
$filename = urlencode($file[0]->getClientOriginalName());
$file[0]->move($public_path . $filesPath, $filename);
} catch (RuntimeException $e) {
echo 'ERROR ' . $e->getMessage();
}
$response = [
'message' => "File Uploaded. Great!",
"filePath" => $filesPath . urlencode($file[0]->getClientOriginalName()),
"fileName" => urlencode($file[0]->getClientOriginalName()),
'type' => $type,
];
return $response;
}
}