diff --git a/lib/api/file_create.php b/lib/api/file_create.php index 1c50387..6323037 100644 --- a/lib/api/file_create.php +++ b/lib/api/file_create.php @@ -1,82 +1,109 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class file_api_file_create extends file_api_common { /** * Request handler */ public function handle() { parent::handle(); if (!isset($this->args['file']) || $this->args['file'] === '') { throw new Exception("Missing file name", file_api_core::ERROR_CODE); } if (!isset($this->args['content'])) { if (!($this->api instanceof file_api_lib) || empty($this->args['path'])) { throw new Exception("Missing file content", file_api_core::ERROR_CODE); } } if (is_resource($this->args['content'])) { $chunk = stream_get_contents($this->args['content'], 1024000, 0); } else if ($this->args['path']) { $chunk = $this->args['path']; $is_file = true; } else { $chunk = $this->args['content']; } $ctype = $this->args['content-type']; if ($ctype && !preg_match('/^[a-z_-]+\/[a-z._-]+$/', $ctype)) { $ctype = ''; } $request = $this instanceof file_api_file_update ? 'file_update' : 'file_create'; $file = array( 'content' => $this->args['content'], 'path' => $this->args['path'], 'type' => rcube_mime::file_content_type($chunk, $this->args['file'], $ctype, !$is_file), ); if (strpos($file['type'], 'empty') !== false && $ctype) { $file['type'] = $ctype; } else if (empty($file['type'])) { $file['type'] = 'application/octet-stream'; } + // Get file content from a template + if ($request == 'file_create' && empty($file['path']) && !strlen($file['content'])) { + $this->use_file_template($file); + } + list($driver, $path) = $this->api->get_driver($this->args['file']); $driver->$request($path, $file); if (rcube_utils::get_boolean((string) $this->args['info'])) { return $driver->file_info($path); } } + + /** + * Use templates when creating empty files + */ + protected function use_file_template(&$file) + { + if ($ext = array_search($file['type'], file_utils::$ext_map)) { + // find the template + $ext = ".$ext"; + if ($handle = opendir(__DIR__ . '/../templates')) { + while (false !== ($entry = readdir($handle))) { + if (substr($entry, -strlen($ext)) == $ext) { + // set path to the template file + $file['path'] = __DIR__ . '/../templates/' . $entry; + break; + } + } + + closedir($handle); + } + } + } } diff --git a/lib/templates/empty.odt b/lib/templates/empty.odt new file mode 100644 index 0000000..4dfdd4e Binary files /dev/null and b/lib/templates/empty.odt differ