Passed
Push — master ( e51508...29855b )
by Stephen
02:42
created

s3_exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
use Illuminate\Contracts\Filesystem\FileNotFoundException;
4
use Illuminate\Support\Facades\Response;
5
use Illuminate\Support\Facades\Storage;
6
7
8
/**
9
 * Return either an S3 file url or a local file url
10
 *
11
 * @param $path
12
 * @param bool $temp
13
 * @return mixed
14
 */
15
function fileURL($path, $temp=true) {
16
    if ($temp) {
17
        return Storage::disk('s3')->temporaryUrl($path, now()->addMinutes(60));
18
    } else {
19
        return Storage::disk('s3')->url($path);
20
    }
21
}
22
23
24
/**
25
 * Determine if an S3 file exists
26
 *
27
 * @param string $s3_key
28
 * @return bool
29
 */
30
function s3_exists(string $s3_key) {
31
    return Storage::disk('s3')->exists($s3_key);
32
}
33
34
35
/**
36
 * Upload a file to an S3 bucket
37
 *
38
 * @param $s3_key
39
 * @param $file_path
40
 * @param null $acl
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $acl is correct as it would always require null to be passed?
Loading history...
41
 * @return mixed
42
 */
43
function s3_upload($s3_key, $file_path, $acl=null) {
44
    if(is_null($acl)) {
0 ignored issues
show
introduced by
The condition is_null($acl) is always true.
Loading history...
45
        Storage::disk('s3')->put($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

45
        Storage::disk('s3')->put($s3_key, /** @scrutinizer ignore-type */ fopen($file_path, 'r+'));
Loading history...
46
    } else {
47
        Storage::disk('s3')->put($s3_key, fopen($file_path, 'r+'), $acl);
48
    }
49
    return fileURL($s3_key);
50
}
51
52
53
/**
54
 * Download a file from an S3 bucket
55
 *
56
 * @param $file_url
57
 * @param null $file_name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $file_name is correct as it would always require null to be passed?
Loading history...
58
 * @return \Illuminate\Http\Response
59
 * @throws FileNotFoundException
60
 */
61
function s3_download($file_url, $file_name=null) {
62
    if (is_null($file_name)) {
0 ignored issues
show
introduced by
The condition is_null($file_name) is always true.
Loading history...
63
        $file_name = basename($file_url);
64
    }
65
66
    $mime = Storage::disk('s3')->getMimetype($file_url);
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

66
    $mime = Storage::disk('s3')->/** @scrutinizer ignore-call */ getMimetype($file_url);
Loading history...
67
    $size = Storage::disk('s3')->getSize($file_url);
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

67
    $size = Storage::disk('s3')->/** @scrutinizer ignore-call */ getSize($file_url);
Loading history...
68
69
    $response = [
70
        'Content-Type' => $mime,
71
        'Content-Length' => $size,
72
        'Content-Description' => 'File Transfer',
73
        'Content-Disposition' => "attachment; filename={$file_name}",
74
        'Content-Transfer-Encoding' => 'binary',
75
    ];
76
77
    return Response::make(Storage::disk('s3')->get($file_url), 200, $response);
78
}
79
80
81
/**
82
 * Delete a file or folder from an S3 bucket
83
 *
84
 * @param $s3_key
85
 */
86
function s3_delete($s3_key) {
87
    Storage::disk('s3')->delete($s3_key);
88
}
89
90
91
/**
92
 * Upload raw file contents to an S3 bucket
93
 *
94
 * @param $s3_key
95
 * @param $file_contents
96
 * @param null $acl
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $acl is correct as it would always require null to be passed?
Loading history...
97
 * @return mixed
98
 */
99
function s3_upload_raw($s3_key, $file_contents, $acl=null) {
100
    if(is_null($acl)) {
0 ignored issues
show
introduced by
The condition is_null($acl) is always true.
Loading history...
101
        Storage::disk('s3')->put($s3_key, $file_contents);
102
    } else {
103
        Storage::disk('s3')->put($s3_key, $file_contents, $acl);
104
    }
105
    return fileURL($s3_key);
106
}
107
108
109
/**
110
 * List all of the files in an S3 directory
111
 *
112
 * @param $s3_key
113
 * @return array
114
 */
115
function s3_list($s3_key)
116
{
117
    $storage = Storage::disk('s3');
118
    $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

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