Passed
Push — master ( f2ff12...14e56a )
by Stephen
02:36
created

S3::upload_raw()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Sfneal\Helpers\Aws\S3;
4
5
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6
use Illuminate\Support\Facades\Response;
7
use Illuminate\Support\Facades\Storage;
8
9
10
class S3
11
{
12
    /**
13
     * @var string
14
     */
15
    private $s3_key;
16
17
    /**
18
     * S3 constructor.
19
     *
20
     * @param string $s3_key
21
     */
22
    public function __construct(string $s3_key)
23
    {
24
        $this->s3_key = $s3_key;
25
    }
26
27
    /**
28
     * Return either an S3 file url or a local file url
29
     *
30
     * @param bool $temp
31
     * @return mixed
32
     */
33
    public function url(bool $temp = true) {
34
        if ($temp) {
35
            return Storage::disk('s3')->temporaryUrl($this->s3_key, now()->addMinutes(60));
36
        } else {
37
            return Storage::disk('s3')->url($this->s3_key);
38
        }
39
    }
40
41
    /**
42
     * Determine if an S3 file exists
43
     *
44
     * @return bool
45
     */
46
    public function exists() {
47
        return Storage::disk('s3')->exists($this->s3_key);
48
    }
49
50
    /**
51
     * Upload a file to an S3 bucket
52
     *
53
     * @param $file_path
54
     * @param string|null $acl
55
     * @return mixed
56
     */
57
    public function upload(string $file_path, string $acl = null) {
58
        if(is_null($acl)) {
59
            Storage::disk('s3')->put($this->s3_key, fopen($file_path, 'r+'));
0 ignored issues
show
Bug introduced by
It seems like fopen($file_path, 'r+') can also be of type false; however, parameter $contents of Illuminate\Filesystem\FilesystemAdapter::put() does only seem to accept 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

59
            Storage::disk('s3')->put($this->s3_key, /** @scrutinizer ignore-type */ fopen($file_path, 'r+'));
Loading history...
60
        } else {
61
            Storage::disk('s3')->put($this->s3_key, fopen($file_path, 'r+'), $acl);
62
        }
63
        return $this->url();
64
    }
65
66
    /**
67
     * Upload raw file contents to an S3 bucket
68
     *
69
     * @param string $file_contents
70
     * @param string|null $acl
71
     * @return mixed
72
     */
73
    function upload_raw(string $file_contents, string $acl = null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
74
        if(is_null($acl)) {
75
            Storage::disk('s3')->put($this->s3_key, $file_contents);
76
        } else {
77
            Storage::disk('s3')->put($this->s3_key, $file_contents, $acl);
78
        }
79
        return $this->url();
80
    }
81
82
    /**
83
     * Download a file from an S3 bucket
84
     *
85
     * @param string|null $file_name
86
     * @return \Illuminate\Http\Response
87
     * @throws FileNotFoundException
88
     */
89
    public function download(string $file_name = null) {
90
        if (is_null($file_name)) {
91
            $file_name = basename($this->s3_key);
92
        }
93
94
        $mime = Storage::disk('s3')->getMimetype($this->s3_key);
0 ignored issues
show
Bug introduced by
The method getMimetype() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

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

94
        $mime = Storage::disk('s3')->/** @scrutinizer ignore-call */ getMimetype($this->s3_key);
Loading history...
95
        $size = Storage::disk('s3')->getSize($this->s3_key);
0 ignored issues
show
Bug introduced by
The method getSize() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

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

95
        $size = Storage::disk('s3')->/** @scrutinizer ignore-call */ getSize($this->s3_key);
Loading history...
96
97
        $response = [
98
            'Content-Type' => $mime,
99
            'Content-Length' => $size,
100
            'Content-Description' => 'File Transfer',
101
            'Content-Disposition' => "attachment; filename={$file_name}",
102
            'Content-Transfer-Encoding' => 'binary',
103
        ];
104
105
        return Response::make(Storage::disk('s3')->get($this->s3_key), 200, $response);
106
    }
107
108
    /**
109
     * Delete a file or folder from an S3 bucket
110
     *
111
     * @return bool
112
     */
113
    public function delete(): bool {
114
        return Storage::disk('s3')->delete($this->s3_key);
115
    }
116
117
    /**
118
     * List all of the files in an S3 directory
119
     *
120
     * @return array
121
     */
122
    public function list()
123
    {
124
        $storage = Storage::disk('s3');
125
        $client = $storage->getAdapter()->getClient();
0 ignored issues
show
Bug introduced by
The method getAdapter() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

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

125
        $client = $storage->/** @scrutinizer ignore-call */ getAdapter()->getClient();
Loading history...
126
        $command = $client->getCommand('ListObjects');
127
        $command['Bucket'] = $storage->getAdapter()->getBucket();
128
        $command['Prefix'] = $this->s3_key;
129
        $result = $client->execute($command);
130
131
        $files = array();
132
        if (isset($result['Contents']) && !empty($result['Contents'])) {
133
            foreach ($result['Contents'] as $content) {
134
                $url = fileURL($content['Key']);
135
                $parts = explode('/', explode('?', $url, 2)[0]);
136
                $files[] = [
137
                    'name' => end($parts),
138
                    'url' => $url,
139
                    'key' => $content['Key']
140
                ];
141
            }
142
        }
143
        return $files;
144
    }
145
}
146