1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CosmicRadioTV\Podcast\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use October\Rain\Database\Model; |
7
|
|
|
use October\Rain\Database\Traits\Validation; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Release |
11
|
|
|
* |
12
|
|
|
* @package CosmicRadioTV\Podcast\Models |
13
|
|
|
* @property int $id ID |
14
|
|
|
* @property int $episode_id Episode ID |
15
|
|
|
* @property int $release_type_id Release type ID |
16
|
|
|
* @property string $url Release URL |
17
|
|
|
* @property int $size Release size in bytes |
18
|
|
|
* @property string $description Show description |
19
|
|
|
* @property Carbon $created_at Show creation time |
20
|
|
|
* @property Carbon $updated_at Show update time |
21
|
|
|
* @property-read Episode $episode Episode |
22
|
|
|
* @property-read ReleaseType $release_type Release type |
23
|
|
|
* @method \October\Rain\Database\Relations\BelongsTo episode() |
24
|
|
|
* @method \October\Rain\Database\Relations\BelongsTo release_type() |
25
|
|
|
*/ |
26
|
|
|
class Release extends Model |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
//use Validation; |
30
|
|
|
|
31
|
|
|
protected $table = 'cosmicradiotv_podcast_releases'; |
32
|
|
|
|
33
|
|
|
protected $fillable = ['episode', 'release_type', 'url', 'size']; |
34
|
|
|
|
35
|
|
|
public $rules = [ |
36
|
|
|
'episode_id' => ['required', 'exists:cosmicradiotv_podcast_episodes,id'], |
37
|
|
|
'release_type_id' => ['required', 'exists:cosmicradiotv_podcast_release_types,id'], |
38
|
|
|
'url' => ['require', 'url'], |
39
|
|
|
'size' => ['numeric'], |
40
|
|
|
'description' => [], |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/* |
44
|
|
|
* Relations |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
public $belongsTo = [ |
48
|
|
|
'episode' => ['CosmicRadioTV\Podcast\Models\Episode'], |
49
|
|
|
'release_type' => ['CosmicRadioTV\Podcast\Models\ReleaseType'], |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Generates URL from release type that can be used in embed |
54
|
|
|
*/ |
55
|
|
|
public function getEmbedUrlAttribute() |
56
|
|
|
{ |
57
|
|
|
switch ($this->release_type->type) { |
58
|
|
|
case 'audio': |
59
|
|
|
case 'video': |
60
|
|
|
return $this->url; |
61
|
|
|
break; |
62
|
|
|
case 'youtube': |
63
|
|
|
// http://stackoverflow.com/a/8260383 |
64
|
|
|
if (preg_match('/.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/', $this->url, $matches)) { |
65
|
|
|
return 'https://youtube.com/embed/' . $matches[1]; |
66
|
|
|
} else { |
67
|
|
|
return $this->url; |
68
|
|
|
} |
69
|
|
|
break; |
70
|
|
|
default: |
71
|
|
|
return $this->url; |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Used to automatically set the size of the release (if size is set to 0) |
78
|
|
|
*/ |
79
|
|
|
public function beforeSave() { |
80
|
|
|
$dirty = $this->getDirty(); |
81
|
|
|
if (in_array($this->release_type->type,['video','audio']) && $this->size == 0 && (isset($dirty['size']) || isset($dirty['url']))) { |
82
|
|
|
$ch = curl_init(); |
83
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->url); |
84
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 20); |
85
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true); |
86
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
87
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, true); |
88
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
89
|
|
|
curl_exec($ch); |
90
|
|
|
|
91
|
|
|
if (!curl_errno($ch)) { |
92
|
|
|
$this->size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); |
93
|
|
|
} |
94
|
|
|
curl_close($ch); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |