Passed
Push — master ( 83ad03...1c456b )
by Stephen
14:09 queued 10s
created

S3::urlTemp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sfneal\Helpers\Aws\S3;
4
5
use DateTimeInterface;
6
use Illuminate\Contracts\Filesystem\FileNotFoundException;
7
use Illuminate\Support\Facades\Response;
8
use Illuminate\Support\Facades\Storage;
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.
29
     *
30
     * @param bool $temp
31
     * @param DateTimeInterface|null $expiration
32
     * @return string
33
     */
34
    public function url(bool $temp = true, DateTimeInterface $expiration = null): string
35
    {
36
        if ($temp) {
37
            return Storage::disk('s3')->temporaryUrl($this->s3_key, $expiration ?? now()->addMinutes(60));
38
        } else {
39
            return Storage::disk('s3')->url($this->s3_key);
40
        }
41
    }
42
43
    /**
44
     * Return either a temporary S3 file url.
45
     *
46
     * @param DateTimeInterface|null $expiration
47
     * @return string
48
     */
49
    public function urlTemp(DateTimeInterface $expiration = null): string
50
    {
51
        return $this->url(true, $expiration);
52
    }
53
54
    /**
55
     * Determine if an S3 file exists.
56
     *
57
     * @return bool
58
     */
59
    public function exists(): bool
60
    {
61
        return Storage::disk('s3')->exists($this->s3_key);
62
    }
63
64
    /**
65
     * Upload a file to an S3 bucket.
66
     *
67
     * @param string $file_path
68
     * @param string|null $acl
69
     * @return string
70
     */
71
    public function upload(string $file_path, string $acl = null): string
72
    {
73
        if (is_null($acl)) {
74
            Storage::disk('s3')->put($this->s3_key, fopen($file_path, 'r+'));
75
        } else {
76
            Storage::disk('s3')->put($this->s3_key, fopen($file_path, 'r+'), $acl);
77
        }
78
79
        return $this->url();
80
    }
81
82
    /**
83
     * Upload raw file contents to an S3 bucket.
84
     *
85
     * @param string $file_contents
86
     * @param string|null $acl
87
     * @return string
88
     */
89
    public function upload_raw(string $file_contents, string $acl = null): string
90
    {
91
        if (is_null($acl)) {
92
            Storage::disk('s3')->put($this->s3_key, $file_contents);
93
        } else {
94
            Storage::disk('s3')->put($this->s3_key, $file_contents, $acl);
95
        }
96
97
        return $this->url();
98
    }
99
100
    /**
101
     * Download a file from an S3 bucket.
102
     *
103
     * @param string|null $file_name
104
     * @return \Illuminate\Http\Response
105
     * @throws FileNotFoundException|\League\Flysystem\FileNotFoundException
106
     */
107
    public function download(string $file_name = null): \Illuminate\Http\Response
108
    {
109
        if (is_null($file_name)) {
110
            $file_name = basename($this->s3_key);
111
        }
112
113
        $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

113
        $mime = Storage::disk('s3')->/** @scrutinizer ignore-call */ getMimetype($this->s3_key);
Loading history...
114
        $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

114
        $size = Storage::disk('s3')->/** @scrutinizer ignore-call */ getSize($this->s3_key);
Loading history...
115
116
        $response = [
117
            'Content-Type' => $mime,
118
            'Content-Length' => $size,
119
            'Content-Description' => 'File Transfer',
120
            'Content-Disposition' => "attachment; filename={$file_name}",
121
            'Content-Transfer-Encoding' => 'binary',
122
        ];
123
124
        return Response::make(Storage::disk('s3')->get($this->s3_key), 200, $response);
125
    }
126
127
    /**
128
     * Delete a file or folder from an S3 bucket.
129
     *
130
     * @return bool
131
     */
132
    public function delete(): bool
133
    {
134
        return Storage::disk('s3')->delete($this->s3_key);
135
    }
136
137
    /**
138
     * List all of the files in an S3 directory.
139
     *
140
     * @return array
141
     */
142
    public function list(): array
143
    {
144
        $storage = Storage::disk('s3');
145
        $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

145
        $client = $storage->/** @scrutinizer ignore-call */ getAdapter()->getClient();
Loading history...
146
        $command = $client->getCommand('ListObjects');
147
        $command['Bucket'] = $storage->getAdapter()->getBucket();
148
        $command['Prefix'] = $this->s3_key;
149
        $result = $client->execute($command);
150
151
        $files = [];
152
        if (isset($result['Contents']) && ! empty($result['Contents'])) {
153
            foreach ($result['Contents'] as $content) {
154
                $url = fileURL($content['Key']);
155
                $parts = explode('/', explode('?', $url, 2)[0]);
156
                $files[] = [
157
                    'name' => end($parts),
158
                    'url' => $url,
159
                    'key' => $content['Key'],
160
                ];
161
            }
162
        }
163
164
        return $files;
165
    }
166
}
167