Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions server/php/UploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function __construct($options = null, $initialize = true, $error_messages = null
$this->response = array();
$this->options = array(
'script_url' => $this->get_full_url().'/',
'web_import_temp_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/web_imports/',
'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/',
'upload_url' => $this->get_full_url().'/files/',
'user_dirs' => false,
Expand Down Expand Up @@ -149,7 +150,8 @@ function __construct($options = null, $initialize = true, $error_messages = null
'max_height' => 80
)
),
'print_response' => true
'print_response' => true,
'use_unique_hash_for_names' => false
);
if ($options) {
$this->options = $options + $this->options;
Expand Down Expand Up @@ -209,18 +211,18 @@ protected function get_user_path() {
return '';
}

protected function get_upload_path($file_name = null, $version = null) {
protected function get_upload_path($file_name = null, $version = null, $type = 'upload_dir') {
$file_name = $file_name ? $file_name : '';
if (empty($version)) {
$version_path = '';
} else {
$version_dir = @$this->options['image_versions'][$version]['upload_dir'];
$version_dir = @$this->options['image_versions'][$version][$type];
if ($version_dir) {
return $version_dir.$this->get_user_path().$file_name;
}
$version_path = $version.'/';
}
return $this->options['upload_dir'].$this->get_user_path()
return $this->options[$type].$this->get_user_path()
.$version_path.$file_name;
}

Expand Down Expand Up @@ -1029,7 +1031,7 @@ protected function handle_image_file($file_path, $file) {
}

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
$index = null, $content_range = null) {
$index = null, $content_range = null, $isUrlImport = false) {
$file = new \stdClass();
$file->name = $this->get_file_name($uploaded_file, $name, $size, $type, $error,
$index, $content_range);
Expand All @@ -1055,6 +1057,8 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
} else {
move_uploaded_file($uploaded_file, $file_path);
}
} elseif ($isUrlImport) {
rename($uploaded_file, $file_path);
} else {
// Non-multipart uploads (PUT method support)
file_put_contents(
Expand Down Expand Up @@ -1357,4 +1361,40 @@ public function delete($print_response = true) {
return $this->generate_response($response, $print_response);
}

}
public function importFromUrl ($urls, $print_response = true) {
$files = array();
$content_range = $this->get_server_var('HTTP_CONTENT_RANGE') ?
preg_split('/[^0-9]+/', $this->get_server_var('HTTP_CONTENT_RANGE')) : null;
$web_import_dir = $this->get_upload_path(null, null, 'web_import_temp_dir');
if (!is_dir($web_import_dir)) {
mkdir($web_import_dir, $this->options['mkdir_mode'], true);
}
if (!is_array($urls)) {
$urls = array($urls);
}
foreach ($urls as $fileUrl) {
$pInfo = pathinfo($fileUrl);
$imgExtension = 'jpg';
if (isset($pInfo['extension'])) {
$imgExtension = $pInfo['extension'];
}
$fileName = mt_rand(1, 99999) . '_' . time() . '.' . strtolower($imgExtension);
$tmpFile = $this->get_upload_path($fileName, null, 'web_import_temp_dir');
$content = file_get_contents($fileUrl);
$size = file_put_contents($tmpFile, $content);
$files[] = $this->handle_file_upload(
$tmpFile,
$this->options['use_unique_hash_for_names'] ? $fileName : $pInfo['basename'],
$size,
$this->get_server_var('CONTENT_TYPE'),
null,
null,
$content_range,
true
);
}
$response = array('web' => $files);
return $this->generate_response($response, $print_response);
}

}