backgroundupload.php
4.38 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class NF_FU_External_Abstracts_Backgroundupload
*/
abstract class NF_FU_External_Abstracts_Backgroundupload extends \NF_FU_VENDOR\WP_Background_Process {
/**
* @var string
*/
protected $service;
/**
* @var
*/
protected $upload_file;
/**
* @var
*/
protected $external_path;
/**
* @var
*/
protected $external_filename;
/**
* @var
*/
protected $max_chunk_size;
/**
* NF_FU_External_Abstracts_Backgroundupload constructor.
*
* @param string $service
*/
public function __construct( $service ) {
$this->service = $service;
$this->action = 'nf_' . $this->service . '_upload';
parent::__construct();
}
/**
* @return bool|NF_FU_External_Abstracts_Service
*/
protected function get_service() {
return NF_File_Uploads()->externals->get( $this->service );
}
/**
* @param array $item
*/
protected function set_props( $item ) {
foreach ( $item as $key => $value ) {
if ( $key === 'data' ) {
continue;
}
$this->{$key} = $value;
}
}
/**
* @param array $item
*
* @return bool|array
*/
protected function task( $item ) {
$this->set_props( $item );
if ( ! file_exists( $this->upload_file ) ) {
$upload_id = isset( $item['data']['upload_id'] ) ? $item['data']['upload_id'] : null;
return $this->task_error( $upload_id, sprintf( __( 'Ninja Forms File Upload: %s Upload. File does not exist %s', 'ninja-forms-uploads' ), $this->service, $this->upload_file ) );
}
$data = $this->upload_file( $item['data'] );
if ( is_array( $data ) ) {
$item['data'] = $data;
return $item;
}
// Upload has been completed
return false;
}
/**
* @param int|null $upload_id
* @param string $message
*
* @return bool
*/
protected function task_error( $upload_id = null, $message = '' ) {
error_log( $message );
// This file cannot be uploaded to the service
if ( $upload_id ) {
$upload_data = $this->get_upload_data( $upload_id );
unset( $upload_data['external_locations'][ $this->service ] );
if ( empty( $upload_data['external_locations'] ) ) {
unset( $upload_data['external_locations'] );
}
NF_File_Uploads()->model->update( $upload_id, $upload_data );
}
// Remove the job from the queue
return false;
}
/**
* @param $upload_id
*
* @return mixed
*/
protected function get_upload_data( $upload_id ) {
$upload = NF_File_Uploads()->model->get( $upload_id );
return unserialize( $upload->data );
}
/**
* @param array $data
*
* @return bool
*/
protected function complete_upload( $data ) {
$old_data = $this->get_upload_data( $data['upload_id'] );
$data = array_merge( $data, $old_data );
$data['external_path'] = $this->external_path;
$data['external_filename'] = $this->external_filename;
$data['upload_location'] = $this->service;
$data['external_locations'][ $this->service ] = 1;
if ( $this->maybe_delete_server_file( $data ) ) {
unset( $data['defer_remove_from_server'] );
$data['removed_from_server'] = true;
}
unset( $data['chunked'] );
NF_File_Uploads()->model->update( $data['upload_id'], $data );
return true;
}
/**
* Remove the local file from the server after all background uploads have taken place.
*
* @param array $data
*
* @return bool
*/
protected function maybe_delete_server_file( $data ) {
if ( ! isset( $data['defer_remove_from_server'] ) || ! $data['defer_remove_from_server'] ) {
return false;
}
foreach ( $data['external_locations'] as $location => $uploaded ) {
if ( 0 == $uploaded ) {
return false;
}
}
if ( ! file_exists( $this->upload_file ) ) {
return false;
}
$result = unlink( $this->upload_file );
return $result;
}
abstract protected function chunked_upload_file( $service, $data );
/**
* Upload the file either in a single request or multipart chunked upload.
*
* @param array $data
*
* @return array|bool
*/
protected function upload_file( $data ) {
$service = $this->get_service();
if ( isset( $data['chunked'] ) && $data['chunked'] ) {
return $this->chunked_upload_file( $service, $data );
}
$service->set_upload_file( $data['file_path'] );
$service->set_external_path( $data['external_path'] );
$service->set_external_filename( $data['external_filename'] );
$data = $service->upload_file( $data );
if ( false === $data ) {
return $data;
}
return $this->complete_upload( $data );
}
}