forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.php
More file actions
171 lines (149 loc) · 4.71 KB
/
FileUtils.php
File metadata and controls
171 lines (149 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary;
use Cloudinary\Api\Exception\GeneralError;
use GuzzleHttp\Psr7;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
/**
* Class Utils
*
* @internal
*/
class FileUtils
{
/**
* @var int Maximum number of characters allowed for the file extension.
*/
protected const MAX_FILE_EXTENSION_LEN = 5;
protected const SUPPORTED_FETCH_PROTOCOLS = ['ftp', 'http', 'https', 's3', 'gs'];
protected const BASE64_DATA_REGEX = '/^data:([\w-]+\/[\w\-\+.]+)?(;[\w-]+=[\w-]+)*;base64,([a-zA-Z0-9\/+\n=]+)$/';
/**
* Helper function that removes current dir(.) if no dirname found.
*
*
*/
public static function dirName($path): string|array|null
{
return ! empty($path) && pathinfo($path, PATHINFO_DIRNAME) !== '.' ? pathinfo($path, PATHINFO_DIRNAME) : null;
}
/**
* Returns filename and extension for the given path.
*
* In case the path does not have an extension, null value for the extension is returned.
*
* @param string $fullPath The path to split.
*
* @return array containing filename and extension.
*/
public static function splitPathFilenameExtension(string $fullPath): array
{
if (empty($fullPath)) {
return ['', '', ''];
}
$path = self::dirName($fullPath);
$extension = pathinfo($fullPath, PATHINFO_EXTENSION);
if (strlen($extension) <= self::MAX_FILE_EXTENSION_LEN) {
$filename = pathinfo($fullPath, PATHINFO_FILENAME);
} else {
$filename = pathinfo($fullPath, PATHINFO_BASENAME);
$extension = null;
}
return [$path, $filename, $extension];
}
/**
* Removes file extension from the file path (can be full path or just filename)
*
*
*/
public static function removeFileExtension($path): string
{
return implode('/', ArrayUtils::safeFilter([self::dirName($path), pathinfo($path, PATHINFO_FILENAME)]));
}
/**
* Determines whether the provided string is a valid base64 data string.
*
* @param mixed $data The candidate to check.
*
* @return false|int
*/
public static function isBase64Data(mixed $data): bool|int
{
return is_string($data) && preg_match(self::BASE64_DATA_REGEX, $data);
}
/**
* Tries to parse the provided URL.
*
* @param mixed $url The URL candidate.
*
* @return bool|UriInterface URL or false if not a valid URL.
*/
public static function tryGetFetchUrl(mixed $url): UriInterface|bool
{
return Utils::tryParseUrl($url, self::SUPPORTED_FETCH_PROTOCOLS);
}
/**
* Everything that is not a supported URL or Base64 data string is considered as a potential local file.
*
* @param mixed $path Path candidate to check.
*
*/
public static function isLocalFilePath(mixed $path): bool
{
return is_string($path) && ! (self::tryGetFetchUrl($path) || self::isBase64Data($path));
}
/**
* Safe version of fopen that throws an exception when file can't be opened (non-existing, permissions, etc).
*
* @param string $filename The file to open.
* @param string $mode Access mode.
* @param bool $useIncludePath Set to true if you want to search for the file in the include_path.
*
* @return resource
*
* @throws GeneralError
*
* @see fopen
*/
public static function safeFileOpen(string $filename, string $mode, bool $useIncludePath = false)
{
if (empty($filename)) {
throw new GeneralError('Path cannot be empty');
}
$fp = @fopen($filename, $mode, $useIncludePath);
if (! $fp) {
$err = error_get_last();
throw new GeneralError($err['message'] ?? 'Failed to open file: ' . $filename);
}
return $fp;
}
/**
* Internal helper method for preparing file for the upload.
*
* @param mixed $file The file.
*
*
* @throws GeneralError
* @internal
*/
public static function handleFile(mixed $file): UriInterface|StreamInterface|bool
{
$url = self::tryGetFetchUrl($file);
if ($url !== false) {
return $url;
}
if (self::isBase64Data($file)) {
$file = Psr7\Utils::streamFor($file);
} elseif (is_string($file)) {
$file = self::safeFileOpen($file, 'rb');
}
return Psr7\Utils::streamFor($file);
}
}