fipamo/app/Services/Assets/FileUploadService.php
ro 321feb9b92
Page Admin API, part 1
API accessible tasks (create, update, delete) have been updated to the
new format.

The controller needs to be cleaned up because it's a bit heavy and the
new API flow still needs to be properly documented, but it's a good
start
2024-07-23 17:46:45 -06:00

83 lines
2.7 KiB
PHP

<?php
namespace App\Services\Assets;
class FileUploadService
{
public function __construct()
{
}
public static function handleFile($request, $fileMode = true)
{
//$options = $request->getParsedBody();
//front end sends one by one for progress tracking, so grab first
if ($fileMode) {
$file = $request->file('upload_files')[0];
} else {
$file = $request;
}
$type = $file->extension();
$public_path = '../public';
$filesPath = '';
$path = date('Y') . '/' . date('m');
$response = [];
switch ($type) {
case 'jpeg':
case 'jpg':
case 'png':
case 'gif':
if ($fileMode) {
if (isset($request["source"])) {
if ($request["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 . '/';
}
} else {
$filesPath = '/assets/images/blog/' . $path . '/';
}
break;
case 'mkv':
case 'mp4':
$filesPath = '/assets/video/blog/' . $path . '/';
break;
case 'ogg':
case 'mp3':
$filesPath = '/assets/sound/blog/' . $path . '/';
break;
case 'pdf':
case 'txt':
case 'rtf':
$filesPath = '/assets/docs/blog/' . $path . '/';
break;
}
try {
// if does not exist, so lets create it.
if (!is_dir($public_path . $filesPath)) {
mkdir($public_path . $filesPath, 0755, true);
}
$filename = urlencode($file->getClientOriginalName());
$file->move($public_path . $filesPath, $filename);
} catch (RuntimeException $e) {
echo 'ERROR ' . $e->getMessage();
}
$response = [
'message' => "File Uploaded. Great!",
"filePath" => $filesPath . urlencode($file->getClientOriginalName()),
"fileName" => urlencode($file->getClientOriginalName()),
'type' => $type,
];
return $response;
}
}