-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathFileOrDirectoryCompressTask.php
More file actions
92 lines (83 loc) · 3.14 KB
/
Copy pathFileOrDirectoryCompressTask.php
File metadata and controls
92 lines (83 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* _ _ _ _
* | (_) | (_)
* | |_| |__ __ _ ___ _ _ _ __ ___ _ ___
* | | | '_ \ / _` / __| | | | '_ \ / __| |/ _ \
* | | | |_) | (_| \__ \ |_| | | | | (__| | (_) |
* |_|_|_.__/ \__,_|___/\__, |_| |_|\___|_|\___/
* __/ |
* |___/
*
* Copyright (C) 2016-2026 NetherGames Network
*
* This is private software, you cannot redistribute and/or modify it in any way
* unless given explicit permission to do so. If you have not been given explicit
* permission to view or modify this software you should take the appropriate actions
* to remove this software from your device immediately.
*
* @author driesboy
*
*/
declare(strict_types=1);
namespace libasyncio;
use GlobalLogger;
use InvalidArgumentException;
use libasyncio\compression\CompressionFormat;
use libasyncio\compression\Compressor;
use pocketmine\Server;
use RuntimeException;
class FileOrDirectoryCompressTask extends FileOperationTask
{
/** @var string */
private string $input;
/** @var string */
private string $output;
/** @var int|null */
private ?int $compressionLevel;
/** @var Compressor */
private Compressor $compressor;
/**
* FileOrDirectoryCompressTask constructor.
*
* @param string $input
* @param string $output
* @param callable $callable
* @param int|null $compressionLevel
* @param CompressionFormat|null $format
*/
public function __construct(string $input, string $output, callable $callable, ?int $compressionLevel = null, ?CompressionFormat $format = null)
{
if ($format !== null && !$format->isCompatible()) {
throw new InvalidArgumentException('Compression format ' . $format->name . ' is not compatible');
}
$this->compressor = ($format ?? CompressionFormat::auto())->getCompressor();
$this->input = $input;
$this->output = ($outputFormat = CompressionFormat::fromPath($output)) !== null ? substr($output, 0, -strlen('.' . $outputFormat->getFileExtension())) : $output;
$this->compressionLevel = $compressionLevel;
parent::__construct($input, $callable);
}
/**
* @inheritDoc
*/
public function onRun(): void
{
parent::onRun();
try {
$this->setSuccess(RecursiveCompressor::compress($this->input, $this->output, $this->compressionLevel, $this->compressor->getFormat()));
} catch (RuntimeException $e) {
GlobalLogger::get()->critical("Compression failed for {$this->input}: " . $e->getMessage());
GlobalLogger::get()->logException($e);
$this->setSuccess(false);
}
}
protected function checkSuccess(): void
{
$outputLocation = $this->output . '.' . $this->compressor->getFormat()->getFileExtension();
if ($this->getSuccess()) {
Server::getInstance()->getLogger()->debug("Compressed directory/file {$this->input} to {$outputLocation}");
} else {
Server::getInstance()->getLogger()->error("Unable to compress file {$this->input} to {$outputLocation}");
}
}
}