Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.

Commit 8af6643

Browse files
adding import image from url
1 parent debdd7e commit 8af6643

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

server/php/UploadHandler.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function __construct($options = null, $initialize = true, $error_messages = null
4444
$this->response = array();
4545
$this->options = array(
4646
'script_url' => $this->get_full_url().'/',
47+
'web_import_temp_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/web_imports/',
4748
'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/',
4849
'upload_url' => $this->get_full_url().'/files/',
4950
'user_dirs' => false,
@@ -149,7 +150,8 @@ function __construct($options = null, $initialize = true, $error_messages = null
149150
'max_height' => 80
150151
)
151152
),
152-
'print_response' => true
153+
'print_response' => true,
154+
'use_unique_hash_for_names' => false
153155
);
154156
if ($options) {
155157
$this->options = $options + $this->options;
@@ -209,18 +211,18 @@ protected function get_user_path() {
209211
return '';
210212
}
211213

212-
protected function get_upload_path($file_name = null, $version = null) {
214+
protected function get_upload_path($file_name = null, $version = null, $type = 'upload_dir') {
213215
$file_name = $file_name ? $file_name : '';
214216
if (empty($version)) {
215217
$version_path = '';
216218
} else {
217-
$version_dir = @$this->options['image_versions'][$version]['upload_dir'];
219+
$version_dir = @$this->options['image_versions'][$version][$type];
218220
if ($version_dir) {
219221
return $version_dir.$this->get_user_path().$file_name;
220222
}
221223
$version_path = $version.'/';
222224
}
223-
return $this->options['upload_dir'].$this->get_user_path()
225+
return $this->options[$type].$this->get_user_path()
224226
.$version_path.$file_name;
225227
}
226228

@@ -1029,7 +1031,7 @@ protected function handle_image_file($file_path, $file) {
10291031
}
10301032

10311033
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
1032-
$index = null, $content_range = null) {
1034+
$index = null, $content_range = null, $isUrlImport = false) {
10331035
$file = new \stdClass();
10341036
$file->name = $this->get_file_name($uploaded_file, $name, $size, $type, $error,
10351037
$index, $content_range);
@@ -1055,6 +1057,8 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
10551057
} else {
10561058
move_uploaded_file($uploaded_file, $file_path);
10571059
}
1060+
} elseif ($isUrlImport) {
1061+
rename($uploaded_file, $file_path);
10581062
} else {
10591063
// Non-multipart uploads (PUT method support)
10601064
file_put_contents(
@@ -1357,4 +1361,40 @@ public function delete($print_response = true) {
13571361
return $this->generate_response($response, $print_response);
13581362
}
13591363

1360-
}
1364+
public function importFromUrl ($urls, $print_response = true) {
1365+
$files = array();
1366+
$content_range = $this->get_server_var('HTTP_CONTENT_RANGE') ?
1367+
preg_split('/[^0-9]+/', $this->get_server_var('HTTP_CONTENT_RANGE')) : null;
1368+
$web_import_dir = $this->get_upload_path(null, null, 'web_import_temp_dir');
1369+
if (!is_dir($web_import_dir)) {
1370+
mkdir($web_import_dir, $this->options['mkdir_mode'], true);
1371+
}
1372+
if (!is_array($urls)) {
1373+
$urls = array($urls);
1374+
}
1375+
foreach ($urls as $fileUrl) {
1376+
$pInfo = pathinfo($fileUrl);
1377+
$imgExtension = 'jpg';
1378+
if (isset($pInfo['extension'])) {
1379+
$imgExtension = $pInfo['extension'];
1380+
}
1381+
$fileName = mt_rand(1, 99999) . '_' . time() . '.' . strtolower($imgExtension);
1382+
$tmpFile = $this->get_upload_path($fileName, null, 'web_import_temp_dir');
1383+
$content = file_get_contents($fileUrl);
1384+
$size = file_put_contents($tmpFile, $content);
1385+
$files[] = $this->handle_file_upload(
1386+
$tmpFile,
1387+
$this->options['use_unique_hash_for_names'] ? $fileName : $pInfo['basename'],
1388+
$size,
1389+
$this->get_server_var('CONTENT_TYPE'),
1390+
null,
1391+
null,
1392+
$content_range,
1393+
true
1394+
);
1395+
}
1396+
$response = array('web' => $files);
1397+
return $this->generate_response($response, $print_response);
1398+
}
1399+
1400+
}

0 commit comments

Comments
 (0)