-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathFilterBuilder.php
More file actions
389 lines (325 loc) · 9.13 KB
/
Copy pathFilterBuilder.php
File metadata and controls
389 lines (325 loc) · 9.13 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
declare(strict_types=1);
namespace Verseles\Flyclone;
/**
* Fluent builder for rclone filter patterns.
*
* Provides a clean API for building include/exclude filter rules
* that are passed to rclone commands.
*/
class FilterBuilder
{
/** @var array<int, string> Include patterns */
private array $includes = [];
/** @var array<int, string> Exclude patterns */
private array $excludes = [];
/** @var int|null Minimum file size in bytes */
private ?int $minSize = null;
/** @var int|null Maximum file size in bytes */
private ?int $maxSize = null;
/** @var int|null Minimum file age in seconds */
private ?int $minAge = null;
/** @var int|null Maximum file age in seconds */
private ?int $maxAge = null;
/** @var bool Whether to delete excluded files */
private bool $deleteExcluded = false;
/**
* Create a new filter builder.
*/
public static function create(): self
{
return new self();
}
/**
* Add an include pattern.
*
* @param string $pattern Glob pattern (e.g., "*.txt", "photos/**")
*/
public function include(string $pattern): self
{
$this->includes[] = $pattern;
return $this;
}
/**
* Add multiple include patterns.
*
* @param array<int, string> $patterns Array of glob patterns
*/
public function includeMany(array $patterns): self
{
foreach ($patterns as $pattern) {
$this->include($pattern);
}
return $this;
}
/**
* Add an exclude pattern.
*
* @param string $pattern Glob pattern (e.g., "*.tmp", "node_modules/**")
*/
public function exclude(string $pattern): self
{
$this->excludes[] = $pattern;
return $this;
}
/**
* Add multiple exclude patterns.
*
* @param array<int, string> $patterns Array of glob patterns
*/
public function excludeMany(array $patterns): self
{
foreach ($patterns as $pattern) {
$this->exclude($pattern);
}
return $this;
}
/**
* Include only files with specific extensions.
*
* @param string|array<int, string> $extensions Extension(s) without dot (e.g., "jpg", ["jpg", "png"])
*/
public function extensions(string|array $extensions): self
{
$exts = is_array($extensions) ? $extensions : [$extensions];
foreach ($exts as $ext) {
$this->include('*.' . ltrim($ext, '.'));
}
return $this;
}
/**
* Exclude common temporary and system files.
*/
public function excludeCommon(): self
{
return $this->excludeMany([
'*.tmp',
'*.temp',
'*.bak',
'*~',
'.DS_Store',
'Thumbs.db',
'.git/**',
'.svn/**',
'node_modules/**',
'__pycache__/**',
'*.pyc',
]);
}
/**
* Exclude hidden files and directories.
*/
public function excludeHidden(): self
{
return $this->exclude('.*');
}
/**
* Set minimum file size.
*
* @param int|string $size Size in bytes or human-readable (e.g., "1M", "500K")
*/
public function minSize(int|string $size): self
{
$this->minSize = $this->parseSize($size);
return $this;
}
/**
* Set maximum file size.
*
* @param int|string $size Size in bytes or human-readable (e.g., "100M", "1G")
*/
public function maxSize(int|string $size): self
{
$this->maxSize = $this->parseSize($size);
return $this;
}
/**
* Only include files older than the specified age.
*
* @param string $age Age string (e.g., "1d", "2h", "30m")
*/
public function olderThan(string $age): self
{
$this->minAge = $this->parseAge($age);
return $this;
}
/**
* Only include files newer than the specified age.
*
* @param string $age Age string (e.g., "1d", "2h", "30m")
*/
public function newerThan(string $age): self
{
$this->maxAge = $this->parseAge($age);
return $this;
}
/**
* Enable deletion of excluded files on sync operations.
*/
public function deleteExcluded(bool $delete = true): self
{
$this->deleteExcluded = $delete;
return $this;
}
/**
* Convert the filter to rclone flags array.
*
* @return array<string, mixed> Flags to be merged with operation flags
*/
public function toFlags(): array
{
$flags = [];
// Include patterns
foreach ($this->includes as $pattern) {
$flags['include'][] = $pattern;
}
// Exclude patterns
foreach ($this->excludes as $pattern) {
$flags['exclude'][] = $pattern;
}
// Size filters
if ($this->minSize !== null) {
$flags['min-size'] = $this->formatSize($this->minSize);
}
if ($this->maxSize !== null) {
$flags['max-size'] = $this->formatSize($this->maxSize);
}
// Age filters
if ($this->minAge !== null) {
$flags['min-age'] = $this->formatAge($this->minAge);
}
if ($this->maxAge !== null) {
$flags['max-age'] = $this->formatAge($this->maxAge);
}
// Delete excluded
if ($this->deleteExcluded) {
$flags['delete-excluded'] = true;
}
return $flags;
}
/**
* Convert to command line arguments.
*
* @return array<int, string> Command line arguments
*/
public function toArgs(): array
{
$args = [];
foreach ($this->includes as $pattern) {
$args[] = '--include';
$args[] = $pattern;
}
foreach ($this->excludes as $pattern) {
$args[] = '--exclude';
$args[] = $pattern;
}
if ($this->minSize !== null) {
$args[] = '--min-size';
$args[] = $this->formatSize($this->minSize);
}
if ($this->maxSize !== null) {
$args[] = '--max-size';
$args[] = $this->formatSize($this->maxSize);
}
if ($this->minAge !== null) {
$args[] = '--min-age';
$args[] = $this->formatAge($this->minAge);
}
if ($this->maxAge !== null) {
$args[] = '--max-age';
$args[] = $this->formatAge($this->maxAge);
}
if ($this->deleteExcluded) {
$args[] = '--delete-excluded';
}
return $args;
}
/**
* Check if any filters are defined.
*/
public function hasFilters(): bool
{
return ! empty($this->includes)
|| ! empty($this->excludes)
|| $this->minSize !== null
|| $this->maxSize !== null
|| $this->minAge !== null
|| $this->maxAge !== null;
}
/**
* Reset all filters.
*/
public function reset(): self
{
$this->includes = [];
$this->excludes = [];
$this->minSize = null;
$this->maxSize = null;
$this->minAge = null;
$this->maxAge = null;
$this->deleteExcluded = false;
return $this;
}
/**
* Parse a size string to bytes.
*/
private function parseSize(int|string $size): int
{
if (is_int($size)) {
return $size;
}
$units = ['B' => 1, 'K' => 1024, 'M' => 1048576, 'G' => 1073741824, 'T' => 1099511627776];
$size = strtoupper(trim($size));
if (preg_match('/^([\d.]+)\s*([BKMGT])?$/i', $size, $matches)) {
$value = (float) $matches[1];
$unit = $matches[2] ?? 'B';
return (int) ($value * ($units[$unit] ?? 1));
}
return (int) $size;
}
/**
* Format bytes to human-readable size for rclone.
*/
private function formatSize(int $bytes): string
{
if ($bytes >= 1073741824) {
return round($bytes / 1073741824, 2) . 'G';
}
if ($bytes >= 1048576) {
return round($bytes / 1048576, 2) . 'M';
}
if ($bytes >= 1024) {
return round($bytes / 1024, 2) . 'K';
}
return $bytes . 'B';
}
/**
* Parse an age string to seconds.
*/
private function parseAge(string $age): int
{
$units = ['s' => 1, 'm' => 60, 'h' => 3600, 'd' => 86400, 'w' => 604800, 'M' => 2592000, 'y' => 31536000];
if (preg_match('/^(\d+)\s*([smhdwMy])?$/i', trim($age), $matches)) {
$value = (int) $matches[1];
$unit = $matches[2] ?? 's';
return $value * ($units[$unit] ?? 1);
}
return (int) $age;
}
/**
* Format seconds to age string for rclone.
*/
private function formatAge(int $seconds): string
{
if ($seconds >= 86400) {
return (int) ($seconds / 86400) . 'd';
}
if ($seconds >= 3600) {
return (int) ($seconds / 3600) . 'h';
}
if ($seconds >= 60) {
return (int) ($seconds / 60) . 'm';
}
return $seconds . 's';
}
}