Skip to content

Commit 4adf8a7

Browse files
committed
Extract returning media & processing to the Files class
1 parent 0addcb2 commit 4adf8a7

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

src/main/php/de/thekid/dialog/import/Source.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,22 @@ public function parent(): ?string { return strstr($this->name(), '/', true) ?: n
4949
/** Sets a parent for this source */
5050
public function nestedIn(string $parent): self { $this->name= $parent.'/'.$this->name; return $this; }
5151

52+
/** Returns this source's origin */
53+
public function origin(): Folder { return $this->origin; }
54+
5255
/** Yields all the media files in this source */
5356
protected function mediaIn(Files $files): iterable {
54-
static $processed= '/^(thumb|preview|full|video|screen)-/';
55-
5657
$images= [];
5758
foreach ($this->entry['images'] ?? [] as $image) {
5859
$images[$image['name']]= $image;
5960
}
6061

61-
foreach ($this->origin->entries() as $path) {
62-
$name= $path->name();
63-
if ($path->isFile() && !preg_match($processed, $name) && ($processing= $files->processing($name))) {
64-
$file= $path->asFile();
65-
$name= $file->filename;
66-
67-
if (!isset($images[$name]) || $file->lastModified() > $images[$name]['modified']) {
68-
yield new UploadMedia($this->entry['slug'], $file, $processing);
69-
}
70-
unset($images[$name]);
62+
foreach ($files->in($this->origin) as $file => $processing) {
63+
$name= $file->filename;
64+
if (!isset($images[$name]) || $file->lastModified() > $images[$name]['modified']) {
65+
yield new UploadMedia($this->entry['slug'], $file, $processing);
7166
}
67+
unset($images[$name]);
7268
}
7369

7470
foreach ($images as $rest) {
Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
<?php namespace de\thekid\dialog\processing;
22

3+
use io\Folder;
4+
35
/** @test de.thekid.dialog.unittest.FilesTest */
46
class Files {
57
private $patterns= [];
8+
private $processed= null;
69

710
/** Maps file extensions to a processing instance */
811
public function matching(array<string> $extensions, Processing $processing): self {
912
$this->patterns['/('.implode('|', array_map(preg_quote(...), $extensions)).')$/i']= $processing;
13+
$this->processed= null;
1014
return $this;
1115
}
1216

17+
/** Returns a (cached) pattern to match all processed files */
18+
public function processed(): string {
19+
if (null !== $this->processed) return $this->processed;
20+
$prefixes= [];
21+
foreach ($this->patterns as $processing) {
22+
foreach ($processing->prefixes() as $prefix) {
23+
$prefixes[$prefix]= true;
24+
}
25+
}
26+
return $this->processed= '/^('.implode('|', array_keys($prefixes)).')-/';
27+
}
28+
1329
/** Returns processing instance based on filename, or NULL */
1430
public function processing(string $filename): ?Processing {
15-
foreach ($this->patterns as $pattern => $processing) {
16-
if (preg_match($pattern, $filename)) return $processing;
31+
if (!preg_match($this->processed(), $filename)) {
32+
foreach ($this->patterns as $pattern => $processing) {
33+
if (preg_match($pattern, $filename)) return $processing;
34+
}
1735
}
1836
return null;
1937
}
38+
39+
/** Yields files and their associated processing in a given folder */
40+
public function in(Folder $origin): iterable {
41+
foreach ($origin->entries() as $path) {
42+
if ($path->isFile() && ($processing= $this->processing($path->name()))) {
43+
yield $path->asFile() => $processing;
44+
}
45+
}
46+
}
2047
}

src/main/php/de/thekid/dialog/processing/Processing.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ abstract class Processing {
1010
/** Returns processing kind */
1111
public abstract function kind(): string;
1212

13+
/** Returns prefixes used by the targets */
14+
public function prefixes(): array<string> { return array_keys($this->targets); }
15+
1316
/**
1417
* Adds a conversion target with a given prefix and conversion target.
1518
* Fluent interface.

src/main/php/de/thekid/dialog/processing/Videos.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ class Videos extends Processing {
1616

1717
public function __construct(private string $executable= 'ffmpeg') { }
1818

19+
/** Returns processing kind */
1920
public function kind(): string { return 'video'; }
2021

22+
/** Returns prefixes used by the targets */
23+
public function prefixes(): array<string> { return [...parent::prefixes(), 'video', 'screen']; }
24+
2125
/** Executes a given external command and returns its exit code */
2226
private function execute(string $command, array<string> $args): void {
2327
$p= new Process($command, $args, null, null, [STDIN, STDOUT, STDERR]);
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<?php namespace de\thekid\dialog\unittest;
22

3-
use de\thekid\dialog\processing\{Files, Images};
3+
use de\thekid\dialog\processing\{Files, Images, ResizeTo, Processing};
44
use test\{Assert, Expect, Test, Values};
55

66
class FilesTest {
77

8+
/** Returns a fixture with a given processing instance */
9+
private function fixtureWith(Processing $processing): Files {
10+
return new Files()->matching(['.jpg', '.jpeg'], $processing);
11+
}
12+
813
#[Test]
914
public function can_create() {
1015
new Files();
@@ -13,16 +18,18 @@ public function can_create() {
1318
#[Test, Values(['test.jpg', 'IMG_1234.JPG', '20221119-iOS.jpeg'])]
1419
public function matching_jpeg_files($filename) {
1520
$processing= new Images();
16-
$fixture= new Files()->matching(['.jpg', '.jpeg'], $processing);
17-
18-
Assert::equals($processing, $fixture->processing($filename));
21+
Assert::equals($processing, $this->fixtureWith($processing)->processing($filename));
1922
}
2023

2124
#[Test, Values(['test-jpg', 'IMG_1234JPG', 'jpeg', '.jpeg-file'])]
2225
public function unmatched_jpeg_files($filename) {
2326
$processing= new Images();
24-
$fixture= new Files()->matching(['.jpg', '.jpeg'], $processing);
27+
Assert::null($this->fixtureWith($processing)->processing($filename));
28+
}
2529

26-
Assert::null($fixture->processing($filename));
30+
#[Test]
31+
public function processed_pattern() {
32+
$processing= new Images()->targeting('preview', new ResizeTo(720, 'jpg'));
33+
Assert::equals('/^(preview)-/', $this->fixtureWith($processing)->processed());
2734
}
2835
}

0 commit comments

Comments
 (0)