Issues (64)

src/Clouds/MicrosoftAzure.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * This file is part of the PHP-FFmpeg-video-streaming package.
5
 *
6
 * (c) Amin Yazdanpanah <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
13
namespace Streaming\Clouds;
14
15
16
use Streaming\Exception\InvalidArgumentException;
17
use Streaming\Exception\RuntimeException;
18
use Streaming\File;
19
20
class MicrosoftAzure implements CloudInterface
21
{
22
    /** @var \MicrosoftAzure\Storage\Blob\BlobRestProxy*/
23
    private $blobClient;
24
25
    /**
26
     * MicrosoftAzure constructor.
27
     * @param  $connection
28
     * @param array $options
29
     */
30
    public function __construct($connection, array $options = [])
31
    {
32
        if(!class_exists('\MicrosoftAzure\Storage\Blob\BlobRestProxy')){
33
            throw new RuntimeException('MicrosoftAzure\Storage\Blob\BlobRestProxy not found. make sure the package is installed: composer require microsoft/azure-storage-blob');
34
        }
35
36
        $this->blobClient = \MicrosoftAzure\Storage\Blob\BlobRestProxy::createBlobService($connection, $options);
37
    }
38
39
    /**
40
     * Upload a entire directory to a cloud
41
     * @param  string $dir
42
     * @param  array $options
43
     */
44
    public function uploadDirectory(string $dir, array $options): void
45
    {
46
        if(!isset($options['container'])){
47
            throw new InvalidArgumentException("You should set the container in the array");
48
        }
49
50
        unset($options['filename']);
51
52
        try {
53
            foreach (scandir($dir) as $filename) {
54
                $path = $dir . DIRECTORY_SEPARATOR . $filename;
55
56
                if (is_file($path)) {
57
                    $this->blobClient->createBlockBlob($options['container'], $filename, fopen($path, "r"), $options['CreateBlockBlobOptions'] ?? null);
0 ignored issues
show
It seems like fopen($path, 'r') can also be of type false; however, parameter $content of MicrosoftAzure\Storage\B...roxy::createBlockBlob() does only seem to accept Psr\Http\Message\StreamInterface|resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
                    $this->blobClient->createBlockBlob($options['container'], $filename, /** @scrutinizer ignore-type */ fopen($path, "r"), $options['CreateBlockBlobOptions'] ?? null);
Loading history...
58
                }
59
            }
60
        } catch (\MicrosoftAzure\Storage\Common\Exceptions\ServiceException $e) {
61
            throw new RuntimeException(sprintf("An error occurred while uploading files: %s", $e->getMessage()), $e->getCode(), $e);
62
        }
63
    }
64
65
    /**
66
     * Download a file from a cloud
67
     * @param  string $save_to
68
     * @param  array $options
69
     */
70
    public function download(string $save_to, array $options): void
71
    {
72
        if(!isset($options['container']) ||  !isset($options['blob'])){
73
            throw new InvalidArgumentException("You should set the container and blob in the array");
74
        }
75
76
        try {
77
            $blob = $this->blobClient->getBlob($options['container'], $options['blob']);
78
            File::put($save_to, $blob->getContentStream());
79
        } catch (\MicrosoftAzure\Storage\Common\Exceptions\ServiceException $e) {
80
            throw new RuntimeException(sprintf("An error occurred while downloading the file: %s", $e->getMessage()), $e->getCode(), $e);
81
        }
82
    }
83
}