Completed
Push — master ( 618bf0...ad9644 )
by Jeff
04:46
created

HostedVideo::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace app\models\types;
4
5
use Yii;
6
use yii\web\UploadedFile;
7
use yii\helpers\FileHelper;
8
use YoutubeDl\YoutubeDl;
9
10
/**
11
 * This is the model class for HostedVideo content type.
12
 */
13
class HostedVideo extends Video
14
{
15
    public static $typeName = 'Hosted video';
16
    public static $typeDescription = 'Upload a video to servers.';
17
    public static $html = '<iframe src="%data%" />';
18
    public static $css = '%field% > * { height: 100%; width: 100%; }';
19
    public static $appendParams = '_win=%x1%,%y1%,%x2%,%y2%;_aspect-mode=letterbox';
20
    public static $input = 'file';
21
    public static $output = 'url';
22
    public static $usable = true;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function rules()
28
    {
29
        return Media::rules();
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function sideload($url)
36
    {
37
        if (!self::validateUrl($url)) {
38
            $this->addError(static::TYPE, Yii::t('app', 'Empty or incorrect URL'));
39
40
            return false;
41
        }
42
43
        $dl = new YoutubeDl([
44
            'proxy' => Yii::$app->params['proxy'],
45
            'format' => 'best[ext=mp4]/best[ext=flv]',
46
        ]);
47
        $dl->setDownloadPath(sys_get_temp_dir());
48
49
        try {
50
            $video = $dl->download($url);
51
            if (!$video) {
52
                $this->addError(static::TYPE, Yii::t('app', 'Downloading failed'));
53
54
                return false;
55
            }
56
        } catch (\Symfony\Component\Process\Exception\ProcessFailedException $e) {
57
            $this->addError(static::TYPE, Yii::t('app', 'Downloading exception'));
58
59
            return false;
60
        }
61
62
        $this->filename = $video->getFilename();
63
        $tmpFilepath = sys_get_temp_dir().'/'.$this->filename;
64
65
        $fileInstance = new UploadedFile();
66
        $fileInstance->name = $this->filename;
67
        $fileInstance->tempName = $tmpFilepath;
68
        $fileInstance->type = FileHelper::getMimeType($fileInstance->tempName);
69
        $fileInstance->size = $video->getFile()->getSize();
70
        $this->upload = $fileInstance;
71
72
        if ($this->validate(['upload'])) {
73 View Code Duplication
            if (static::validateFile($fileInstance->tempName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
                return ['filename' => $this->filename, 'tmppath' => $tmpFilepath, 'duration' => static::getDuration($tmpFilepath)];
75
            }
76
            $this->addError(static::TYPE, Yii::t('app', 'Invalid file'));
77
        } else {
78
            $this->addError(static::TYPE, Yii::t('app', 'Cannot save file'));
79
        }
80
        unlink($fileInstance->tempName);
81
82
        return false;
83
    }
84
}
85