1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\Media\Traits; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use League\Flysystem\File as FileLeague; |
7
|
|
|
use Nip\Filesystem\File; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait FileMethodsTrait. |
11
|
|
|
*/ |
12
|
|
|
trait FileMethodsTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var File|FileLeague |
16
|
|
|
*/ |
17
|
|
|
protected $file; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return File |
21
|
|
|
*/ |
22
|
7 |
|
public function getFile() |
23
|
|
|
{ |
24
|
7 |
|
return $this->file; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param File $file |
29
|
|
|
*/ |
30
|
7 |
|
public function setFile(File $file) |
31
|
|
|
{ |
32
|
7 |
|
$this->file = $file; |
33
|
7 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
2 |
|
public function hasFile() |
39
|
|
|
{ |
40
|
2 |
|
return $this->getFile() instanceof File; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $path |
45
|
|
|
* @param $contents |
46
|
|
|
*/ |
47
|
2 |
|
public function generateFileFromContent($path, $contents) |
48
|
|
|
{ |
49
|
2 |
|
$this->getCollection()->getFilesystem()->put( |
|
|
|
|
50
|
2 |
|
$path, |
51
|
2 |
|
$contents |
52
|
|
|
); |
53
|
|
|
|
54
|
2 |
|
$file = new File($this->getCollection()->getFilesystem(), $path); |
55
|
2 |
|
$this->setFile($file); |
56
|
2 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
7 |
|
public function getName() |
62
|
|
|
{ |
63
|
7 |
|
return $this->getFile()->getName(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
1 |
|
public function read() |
70
|
|
|
{ |
71
|
1 |
|
return $this->getFile()->read(); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
2 |
|
public function delete() |
78
|
|
|
{ |
79
|
2 |
|
$converstions = $this->getConversionNames(); |
|
|
|
|
80
|
2 |
|
$converstions[] = 'full'; |
81
|
2 |
|
$filesystem = $this->getFile()->getFilesystem(); |
82
|
2 |
|
foreach ($converstions as $converstion) { |
83
|
2 |
|
$path = $this->getPath($converstion); |
84
|
2 |
|
if ($filesystem->has($path)) { |
85
|
|
|
$filesystem->delete($path); |
86
|
|
|
} |
87
|
|
|
} |
88
|
2 |
|
if ($this->getFile()->exists()) { |
89
|
2 |
|
$this->getFile()->delete(); |
90
|
|
|
} |
91
|
2 |
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the path to the original media file. |
96
|
|
|
* |
97
|
|
|
* @param string $conversionName |
98
|
|
|
* |
99
|
|
|
* @throws Exception |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
2 |
|
public function getPath(string $conversionName = ''): string |
104
|
|
|
{ |
105
|
2 |
|
if (!$this->hasFile()) { |
106
|
|
|
throw new Exception('Error getting path for media with no file'); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
if ($conversionName) { |
110
|
2 |
|
return $this->getBasePath($conversionName) . DIRECTORY_SEPARATOR . $this->getName(); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->getFile()->getPath(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|