Changeset View
Changeset View
Standalone View
Standalone View
lib/api/file_create.php
Show All 35 Lines | public function handle() | ||||
} | } | ||||
if (!isset($this->args['content'])) { | if (!isset($this->args['content'])) { | ||||
if (!($this->api instanceof file_api_lib) || empty($this->args['path'])) { | if (!($this->api instanceof file_api_lib) || empty($this->args['path'])) { | ||||
throw new Exception("Missing file content", file_api_core::ERROR_CODE); | throw new Exception("Missing file content", file_api_core::ERROR_CODE); | ||||
} | } | ||||
} | } | ||||
$is_file = false; | |||||
if (is_resource($this->args['content'])) { | if (is_resource($this->args['content'])) { | ||||
$chunk = stream_get_contents($this->args['content'], 1024000, 0); | $chunk = stream_get_contents($this->args['content'], 1024000, 0); | ||||
} | } | ||||
else if ($this->args['path']) { | else if ($this->args['path'] ?? null) { | ||||
$chunk = $this->args['path']; | $chunk = $this->args['path']; | ||||
$is_file = true; | $is_file = true; | ||||
} | } | ||||
else { | else { | ||||
$chunk = $this->args['content']; | $chunk = $this->args['content']; | ||||
} | } | ||||
$ctype = $this->args['content-type']; | $ctype = $this->args['content-type']; | ||||
if ($ctype && !preg_match('/^[a-z_-]+\/[a-z._-]+$/', $ctype)) { | if ($ctype && !preg_match('/^[a-z_-]+\/[a-z._-]+$/', $ctype)) { | ||||
$ctype = ''; | $ctype = ''; | ||||
} | } | ||||
$request = $this instanceof file_api_file_update ? 'file_update' : 'file_create'; | $request = $this instanceof file_api_file_update ? 'file_update' : 'file_create'; | ||||
$file = array( | $file = array( | ||||
'content' => $this->args['content'], | 'content' => $this->args['content'], | ||||
'path' => $this->args['path'], | 'path' => $this->args['path'] ?? null, | ||||
'type' => rcube_mime::file_content_type($chunk, $this->args['file'], $ctype, !$is_file), | 'type' => rcube_mime::file_content_type($chunk, $this->args['file'] ?? null, $ctype, !$is_file), | ||||
); | ); | ||||
if (strpos($file['type'], 'empty') !== false && $ctype) { | if (strpos($file['type'], 'empty') !== false && $ctype) { | ||||
$file['type'] = $ctype; | $file['type'] = $ctype; | ||||
} | } | ||||
else if (empty($file['type'])) { | else if (empty($file['type'])) { | ||||
$file['type'] = 'application/octet-stream'; | $file['type'] = 'application/octet-stream'; | ||||
} | } | ||||
// Get file content from a template | // Get file content from a template | ||||
if ($request == 'file_create' && empty($file['path']) && empty($file['content'])) { | if ($request == 'file_create' && empty($file['path']) && empty($file['content'])) { | ||||
$this->use_file_template($file); | $this->use_file_template($file); | ||||
} | } | ||||
list($driver, $path) = $this->api->get_driver($this->args['file']); | list($driver, $path) = $this->api->get_driver($this->args['file']); | ||||
$driver->$request($path, $file); | $driver->$request($path, $file); | ||||
if (rcube_utils::get_boolean((string) $this->args['info'])) { | if (rcube_utils::get_boolean((string) ($this->args['info'] ?? null))) { | ||||
return $driver->file_info($path); | return $driver->file_info($path); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Use templates when creating empty files | * Use templates when creating empty files | ||||
*/ | */ | ||||
protected function use_file_template(&$file) | protected function use_file_template(&$file) | ||||
Show All 18 Lines |