-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploaded_File.php
More file actions
195 lines (169 loc) · 5.44 KB
/
Copy pathUploaded_File.php
File metadata and controls
195 lines (169 loc) · 5.44 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* Uploaded_File class file.
*
* @package Mantle
*
* @phpcs:disable Squiz.Commenting.FunctionComment.MissingParamComment
*/
namespace Mantle\Http;
use Mantle\Container\Container;
use Mantle\Contracts\Filesystem\Filesystem_Manager;
use Mantle\Database\Model\Attachment;
use Mantle\Filesystem\File_Helpers;
use Mantle\Support\Arr;
use Mantle\Support\Str;
use Mantle\Support\Traits\Macroable;
use RuntimeException;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
/**
* Handles Uploaded Files
*/
class Uploaded_File extends SymfonyUploadedFile {
use File_Helpers;
use Macroable;
/**
* Store the uploaded file on a filesystem disk.
*
* @param string $path
* @param array<mixed>|string $options
* @return string|false
*/
public function store( $path, array|string $options = [] ) {
return $this->store_as( $path, $this->hash_name(), $this->parse_options( $options ) );
}
/**
* Store the uploaded file on a filesystem disk with public visibility.
*
* @param string $path
* @param array<mixed>|string $options
* @return string|false
*/
public function store_publicly( $path, $options = [] ) {
$options = $this->parse_options( $options );
$options['visibility'] = 'public';
return $this->store_as( $path, $this->getFilename(), $options );
}
/**
* Store the uploaded file on a filesystem disk with public visibility.
*
* @param string $path
* @param string $name
* @param array<mixed>|string $options
* @return string|false
*/
public function store_publicly_as( $path, $name, $options = [] ) {
$options = $this->parse_options( $options );
$options['visibility'] = 'public';
return $this->store_as( $path, $name, $options );
}
/**
* Store the uploaded file on a filesystem disk.
*
* @param string $path
* @param string $name
* @param array<mixed>|string $options
* @return string|false
*/
public function store_as( $path, $name, $options = [] ) {
$path = untrailingslashit( $path );
$options = $this->parse_options( $options );
$disk = Arr::pull( $options, 'disk' );
return Container::get_instance()->make( Filesystem_Manager::class )->drive( $disk )->put_file_as(
$path,
$this,
$name,
$options
);
}
/**
* Store the file as a WordPress attachment.
*
* @param string $path Path to store uploaded file to.
* @param string $name File name.
* @param array<mixed> $options Options for storage, disk name as string.
*
* @throws RuntimeException Thrown on error storing file.
* @todo Enable proper attachment meta data indexing.
*/
public function store_as_attachment( string $path = '/', ?string $name = null, $options = [] ): Attachment {
$options = $this->parse_options( $options );
// Set the default visibility for attachments to public.
if ( ! isset( $options['visibility'] ) ) {
$options['visibility'] = 'public';
}
$uploaded_file = $name ? $this->store_as( $path, $name, $options ) : $this->store( $path, $options );
if ( empty( $uploaded_file ) ) {
throw new RuntimeException( "Error uploading file to [{$path}]: [{$this->getFilename()}]" );
}
$disk_name = $options['disk'] ?? null;
$disk = Container::get_instance()->make( Filesystem_Manager::class )->drive( $disk_name );
// Create the attachment for the file.
$attachment = Attachment::create(
[
'post_mime_type' => $this->getClientMimeType(),
'guid' => $disk->url( $uploaded_file ),
'post_parent' => 0,
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $uploaded_file ) ),
'post_content' => '',
'meta' => [
'_wp_attached_file' => Str::unpreceding_slash( trailingslashit( $path ) . $uploaded_file ),
Attachment::META_KEY_CLOUD_STORAGE => [
'disk' => $disk_name,
'name' => $uploaded_file,
'path' => $path,
'visibility' => $options['visibility'],
],
],
]
);
return $attachment;
}
/**
* Get the contents of the uploaded file.
*
* @throws FileNotFoundException When file not found.
*/
public function get(): string|false {
if ( ! $this->isValid() ) {
throw new FileNotFoundException( "File does not exist at path {$this->getPathname()}." );
}
return file_get_contents( $this->getPathname() ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown
}
/**
* Get the file's extension supplied by the client.
*
* @return string
*/
public function clientExtension(): ?string {
return $this->guessClientExtension();
}
/**
* Parse and format the given options.
*
* @param array<mixed>|string $options
* @return array<mixed>
*/
protected function parse_options( array|string $options ): array {
if ( is_string( $options ) ) {
return [ 'disk' => $options ];
}
return $options;
}
/**
* Create a new file instance from a base instance.
*
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
* @param bool $test
*/
public static function createFromBase( \Symfony\Component\HttpFoundation\File\UploadedFile $file, $test = false ): static {
return $file instanceof static ? $file : new static(
$file->getPathname(),
$file->getClientOriginalName(),
$file->getClientMimeType(),
$file->getError(),
$test
);
}
}