S3::allDirectories()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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