|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Features\Blogging\Models; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Framework\Exceptions\FileNotFoundException; |
|
8
|
|
|
use Hyde\Hyde; |
|
9
|
|
|
use function str_starts_with; |
|
10
|
|
|
use function substr; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A featured image object, for a file stored locally. |
|
14
|
|
|
* |
|
15
|
|
|
* The internal data structure forces the image source to reference a file in the _media directory, |
|
16
|
|
|
* and thus that is what is required for the input. However, when outputting data, the data will |
|
17
|
|
|
* be used for the _site/media directory, so it will provide data relative to the site root. |
|
18
|
|
|
* |
|
19
|
|
|
* The source information is stored in $this->source, which is a file in the _media directory. |
|
20
|
|
|
*/ |
|
21
|
|
|
class LocalFeaturedImage extends FeaturedImage |
|
22
|
|
|
{ |
|
23
|
|
|
protected function setSource(string $source): string |
|
24
|
|
|
{ |
|
25
|
|
|
if (str_starts_with($source, '_media/')) { |
|
26
|
|
|
$source = substr($source, 7); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// We could also validate the file exists here if we want. We might also want to just send a warning. |
|
30
|
|
|
|
|
31
|
|
|
return $source; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getSource(): string |
|
35
|
|
|
{ |
|
36
|
|
|
// Return value is relative to the site's root. |
|
37
|
|
|
return Hyde::relativeLink("media/$this->source"); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getContentLength(): int |
|
41
|
|
|
{ |
|
42
|
|
|
return filesize($this->validatedStoragePath()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function storagePath(): string |
|
46
|
|
|
{ |
|
47
|
|
|
return Hyde::path("_media/$this->source"); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function validatedStoragePath(): string |
|
51
|
|
|
{ |
|
52
|
|
|
if (! file_exists($this->storagePath())) { |
|
53
|
|
|
throw new FileNotFoundException(sprintf('Image at %s does not exist', Hyde::pathToRelative($this->storagePath()))); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->storagePath(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|