Files
Admin Nasledstvo ac168868ee Initial import
2026-05-01 20:52:04 +03:00

153 lines
4.6 KiB
PHP

<?php
namespace app\services;
use app\models\register\Collections;
use app\models\RegisterObjects;
class ZipCollection
{
public static function zipByCollectionId()
{
if (!empty(empty($_GET['id']))) {
echo json_encode(['error' => true, 'missing token_id']);
exit;
}
if (!Auth::getUser()) {
echo json_encode(['error' => true, 'User is not authorised']);
exit;
}
$decode = JWT::decode($_GET['id'], JWT::SECRET_KEY);
$user_id = Auth::getUser()->id;
$cDir = $_SERVER['DOCUMENT_ROOT'] . '/_temp_files';
$object = RegisterObjects::findOne($decode->object_id);
$object_name = $user_id . '-' . $object->id;
$object_dir = $cDir . '/' . $object_name;
$files_to_remove_later = [];
if (!file_exists($object_dir))
mkdir($object_dir, 0777, true);
$object_path = $object_dir . '/' . Formatter::cyrillicTrans($object->name);
if (!file_exists($object_path))
mkdir($object_path, 0777, true);
if ($object) {
foreach ($object->registerObjectImages as $image) {
$files_to_remove_later[] = $image->getCopyImg($object_path);
}
}
$zipFile = $object_path . '.zip';
$zipName = Formatter::cyrillicTrans($object->name) . '.zip';
$zip = new \ZipArchive();
if ($zip->open($zipFile, \ZipArchive::CREATE) === TRUE) {
// Add all files and subdirectories to the zip file
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($object_dir));
foreach ($files as $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($object_path) + 1);
$zip->addFile($filePath, $relativePath);
}
}
// Close the zip file
$zip->close();
}
echo json_encode(['success' => true, 'zipName' => $zipName, 'zipFile' => $zipFile, 'object_dir' => $object_dir]);
exit;
}
public static function downloadZip() {
foreach (['zipName', 'zipFile', 'object_dir'] as $param) {
if(empty($_GET[$param])) {
echo 'missing: '. $param;
exit;
}
}
$zipFile = $_GET['zipFile'];
$zipName = $_GET['zipName'];
$object_dir = $_GET['object_dir'];
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $zipName . '"');
header('Content-Length: ' . filesize($zipFile));
readfile($zipFile);
if(file_exists($zipFile))
unlink($zipFile);
if (is_dir($object_dir)) {
$files = array_diff(scandir($object_dir), array('.', '..'));
foreach ($files as $file) {
$path = $object_dir . '/' . $file;
if (is_dir($path)) {
self::rrmdir($path);
} else {
unlink($path);
}
}
rmdir($object_dir);
}
echo json_encode(['success' => true]);
exit;
}
private static function compressImage($tempPath, $originalPath, $imageQuality)
{
//return $tempPath. '<br>'. $originalPath . '<br>';
// Get image info
$imgInfo = getimagesize($tempPath);
$mime = $imgInfo['mime'];
// Create a new image from file
switch ($mime) {
case 'image/jpeg':
$image = imagecreatefromjpeg($tempPath);
break;
case 'image/png':
$image = imagecreatefrompng($tempPath);
break;
case 'image/gif':
$image = imagecreatefromgif($tempPath);
break;
default:
$image = imagecreatefromjpeg($tempPath);
}
//$imgResized = imagescale($image, 350);
// Save image
imagejpeg($image, $originalPath, $imageQuality);
//imagedestroy($image);
// Return compressed image
//return $originalPath;
}
// Recursive function to delete a directory and its contents
private static function rrmdir($dir)
{
if (is_dir($dir)) {
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
$path = $dir . '/' . $file;
if (is_dir($path)) {
self::rrmdir($path);
} else {
unlink($path);
}
}
rmdir($dir);
}
}
}