Joywork/constructor/Controllers/BaseController.php
2026-05-22 21:21:54 +03:00

148 lines
4.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
abstract class BaseController
{
public $post = [];
public $query = [];
public $files = [];
public function __construct()
{
$this->post = json_decode(file_get_contents("php://input"));
$url = $_SERVER['REQUEST_URI'];
$this->query = $_GET;
$this->files = $_FILES;
// var_dump($_FILES);die;
}
public function response($data, $status = true)
{
$result = [
'status' => $status,
];
if (!$status) {
$result['errors'] = $data;
} else {
$result['data'] = $data;
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
exit;
}
public function getUploadedFile($fileKey = 'photo')
{
var_dump($this->files);
die;
if (isset($this->files[$fileKey])) {
return $this->files[$fileKey];
} else {
return null;
}
}
public function getUploadedFiles($fileKeys, $folder)
{
$files = [];
$folder = 'constructor';
$uploadedPaths = [];
foreach ($fileKeys as $fileKey) {
if (isset($this->files[$fileKey])) {
if (is_array($this->files[$fileKey]['name'])) {
$fileCount = count($this->files[$fileKey]['name']);
for ($i = 0; $i < $fileCount; $i++) {
$fileData = [
'name' => $this->files[$fileKey]['name'][$i],
'type' => $this->files[$fileKey]['type'][$i],
'size' => $this->files[$fileKey]['size'][$i],
'tmp_name' => $this->files[$fileKey]['tmp_name'][$i],
'error' => $this->files[$fileKey]['error'][$i]
];
// var_dump('test');die;
$uploadResult = $this->uploadFile($fileData, $folder);
if ($uploadResult) {
$uploadedPaths[$fileKey][] = $uploadResult;
}
}
} else {
$fileData = [
'name' => $this->files[$fileKey]['name'],
'type' => $this->files[$fileKey]['type'],
'size' => $this->files[$fileKey]['size'],
'tmp_name' => $this->files[$fileKey]['tmp_name'],
'error' => $this->files[$fileKey]['error']
];
$uploadResult = $this->uploadFile($fileData, $folder);
if ($uploadResult) {
$uploadedPaths[$fileKey][] = $uploadResult;
}
// var_dump($uploadResult);die;
}
}
}
return $uploadedPaths;
}
public function uploadFile($file, $folder)
{
$Selectel = SelectelApi::getTokenAndUrl();
$token = $Selectel[0];
$storageUrl = $Selectel[1];
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/files/$folder/";
$newPhotos = [];
$originalName = str_replace("0:/", "", $file['name']);
$strArray = explode(".", $originalName);
$ext = end($strArray);
$ext = mb_strtolower($ext);
$name = md5(time() . $originalName) . "." . $ext;
$one = mb_substr($name, 0, 1);
$two = mb_substr($name, 1, 1);
$three = mb_substr($name, 3, 1);
$new_path = "$one/$two/$three/$name";
$fp = $uploaddir . $new_path;
if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/files/")) {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/files/");
}
if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/files/" . "$folder")) {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/files/$folder");
}
if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/files/$folder/$one")) {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/files/$folder/$one");
}
$two = mb_substr($name, 1, 1);
if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/files/$folder/$one/$two")) {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/files/$folder/$one/$two");
}
$three = mb_substr($name, 3, 1);
if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/files/$folder/$one/$two/$three")) {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/files/$folder/$one/$two/$three");
}
if (is_uploaded_file($file['tmp_name'])) {
move_uploaded_file($file['tmp_name'], $fp);
} else {
rename($file['tmp_name'], $fp);
}
$SelectelApi = new SelectelApi();
$r = $SelectelApi->uploadFile($storageUrl, $token, $fp, "/$folder/" . $new_path, 'image/' . $ext);
//если выложили в Selectel
if ($r) {
$new_path = "https://data.joywork.ru/$folder/" . $new_path;
//удаляем файл с локального сервера
unlink($fp);
// var_dump($new_path);die;
return $new_path;
}
return null;
}
}