Test Failed
Push — master ( bbdbac...8ae349 )
by Stephen
01:06 queued 12s
created

S3::urlTemp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Sfneal\Helpers\Aws\S3\Utils;
4
5
use Closure;
6
use DateTimeInterface;
7
use Illuminate\Contracts\Filesystem\FileNotFoundException;
8
use Illuminate\Http\File;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Facades\Response;
11
use Sfneal\Helpers\Aws\S3\Interfaces\S3Filesystem;
12
13
class S3 extends CloudStorage implements S3Filesystem
14
{
15
    /**
16
     * Return either an S3 file url.
17
     *
18
     * @return string
19
     */
20
    public function url(): string
21
    {
22
        return $this->storageDisk()->url($this->s3Key);
23
    }
24
25
    /**
26
     * Return either a temporary S3 file url.
27
     *
28
     * @param DateTimeInterface|null $expiration
29
     * @return string
30
     */
31
    public function urlTemp(DateTimeInterface $expiration = null): string
32
    {
33
        return $this->storageDisk()->temporaryUrl(
34
            $this->s3Key,
35
            $expiration ?? config('s3-helpers.expiration')
36
        );
37
    }
38
39
    /**
40
     * Upload a file to S3 using automatic streaming or raw file uploading.
41
     *
42
     * @param string $localFilePath
43
     * @param string|null $acl
44
     * @return self
45
     */
46
    public function upload(string $localFilePath, string $acl = null): self
47
    {
48
        // Use streaming for improved performance if enabled
49
        if ($this->isStreamingEnabled()) {
50
            return $this->uploadStream($localFilePath, $acl);
51
        }
52
53
        // Use standard file uploading
54
        else {
55
            return $this->uploadRaw(fopen($localFilePath, 'r+'), $acl);
56
        }
57
    }
58
59
    /**
60
     * Upload raw file contents to an S3 bucket.
61
     *
62
     * @param $fileContents
63
     * @param string|null $acl
64
     * @return self
65
     */
66
    public function uploadRaw($fileContents, string $acl = null): self
67
    {
68
        $this->storageDisk()->put($this->s3Key, $fileContents, $acl);
69
70
        return $this;
71
    }
72
73
    /**
74
     * Upload a file to S3 using automatic streaming.
75
     *
76
     * @param string $localFilePath
77
     * @param string|null $acl
78
     * @return $this
79
     */
80
    protected function uploadStream(string $localFilePath, string $acl = null): self
81
    {
82
        $this->storageDisk()->putFileAs(
83
            dirname($this->s3Key),
84
            new File($localFilePath),
85
            basename($this->s3Key),
86
            $acl
87
        );
88
89
        return $this;
90
    }
91
92
    /**
93
     * Download a file from an S3 bucket.
94
     *
95
     * @param string|null $fileName
96
     * @return \Illuminate\Http\Response
97
     * @throws FileNotFoundException|\League\Flysystem\FileNotFoundException
98
     */
99
    public function download(string $fileName = null): \Illuminate\Http\Response
100
    {
101
        $fileName = $fileName ?? basename($this->s3Key);
102
103
        return Response::make(
104
            $this->storageDisk()->get($this->s3Key),
105
            200,
106
            [
107
                'Content-Type' => $this->storageDisk()->getMimetype($this->s3Key),
108
                'Content-Length' => $this->storageDisk()->getSize($this->s3Key),
109
                'Content-Description' => 'File Transfer',
110
                'Content-Disposition' => "attachment; filename={$fileName}",
111
                'Content-Transfer-Encoding' => 'binary',
112
            ]
113
        );
114
    }
115
116
    /**
117
     * Autocomplete an S3 path by providing the known start of a path.
118
     *
119
     * - once path autocompletion is resolved the $s3_key property is replaced with the found path
120
     *
121
     * @return $this
122
     */
123
    public function autocompletePath(): self
124
    {
125
        // Extract the known $base of the path & the $wildcard
126
        $directory = dirname($this->s3Key);
127
128
        // Get all of the folders in the base directory
129
        $folders = $this->storageDisk()->directories($directory);
130
131
        // Filter folders to find the wildcard path
132
        $folders = array_filter($folders, function ($value) {
133
            return str_starts_with($value, $this->s3Key);
134
        });
135
136
        // return the resolved path
137
        $this->s3Key = collect($folders)->values()->first();
138
139
        return $this;
140
    }
141
142
    /**
143
     * Retrieve an array of all files in a directory with an optional filtering closure.
144
     *
145
     * @param Closure|null $closure
146
     * @return Collection
147
     */
148
    public function allFiles(Closure $closure = null): Collection
149
    {
150
        // Create array of all files
151
        $allFiles = collect($this->storageDisk()->allFiles($this->s3Key));
152
153
        // Apply filtering closure
154
        if (isset($closure)) {
155
            return $allFiles->filter($closure);
156
        }
157
158
        return $allFiles;
159
    }
160
161
    /**
162
     * Retrieve an array of all directories within another directory with an optional filtering closure.
163
     *
164
     * @param Closure|null $closure
165
     * @return Collection
166
     */
167
    public function allDirectories(Closure $closure = null): Collection
168
    {
169
        // Create array of all directories
170
        $allDirectories = collect($this->storageDisk()->allDirectories($this->s3Key));
171
172
        // Apply filtering closure
173
        if (isset($closure)) {
174
            return $allDirectories->filter($closure);
175
        }
176
177
        return $allDirectories;
178
    }
179
}
180