|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cion\TextToSpeech\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Cion\TextToSpeech\Contracts\Formatter; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
7
|
|
|
|
|
8
|
|
|
trait Storable |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Determines where to save the converted file. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $disk; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Determines the path where to save the converted file. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $path; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Set where to store the converted file. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $disk |
|
28
|
|
|
* |
|
29
|
|
|
* @return $this |
|
30
|
|
|
*/ |
|
31
|
|
|
public function disk(string $disk) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->disk = $disk; |
|
34
|
|
|
|
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Set path to where to store the converted file. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $path |
|
42
|
|
|
* |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function saveTo(string $path) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->path = $path; |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Execute the store. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $text |
|
56
|
|
|
* @param mixed $resultContent |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function store($text, $resultContent) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->ensurePathIsNotNull($text, $resultContent); |
|
63
|
|
|
|
|
64
|
|
|
$storage = Storage::disk($this->disk ?: config('tts.disk')); |
|
65
|
|
|
|
|
66
|
|
|
$storage->put($this->path, $resultContent); |
|
67
|
|
|
|
|
68
|
|
|
return $this->path; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Ensures the path not to be null if it is null it will set a default path. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $text |
|
75
|
|
|
* @param mixed $audio |
|
76
|
|
|
* |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function ensurePathIsNotNull($text, $audio) |
|
80
|
|
|
{ |
|
81
|
|
|
$filename = $this->path ?: $this->getDefaultPath() . '/' . $this->getDefaultFilename($text, $audio); |
|
82
|
|
|
|
|
83
|
|
|
if (! $this->hasExtension($filename)) { |
|
84
|
|
|
$filename .= '.'.$this->getExtension(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->path = $filename; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Determine if filename includes file extension. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $filename |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function hasExtension($filename) |
|
97
|
|
|
{ |
|
98
|
|
|
return (bool) pathinfo($filename, PATHINFO_EXTENSION); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get audio file extension. |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getExtension() |
|
107
|
|
|
{ |
|
108
|
|
|
return config('tts.output_format'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get default filename returned in Filename Formatter. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $text |
|
115
|
|
|
* @param mixed $audio |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function getDefaultFilename($text, $audio) |
|
120
|
|
|
{ |
|
121
|
|
|
return app(Formatter::class)->handle($text, $audio); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get the default path. |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function getDefaultPath() |
|
130
|
|
|
{ |
|
131
|
|
|
return config('tts.audio.path'); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|