From c6208bd2271fa02662c4b106c61ca712b46499e5 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 21 Feb 2015 00:00:08 +0100 Subject: s/files: add add_file_data for future code deduplication Signed-off-by: Florian Pritz --- application/service/files.php | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'application') diff --git a/application/service/files.php b/application/service/files.php index a2aa83c3b..ed0d3ec7e 100644 --- a/application/service/files.php +++ b/application/service/files.php @@ -69,10 +69,39 @@ class files { return $multipaste_info; } + static public function add_file_data($id, $content, $filename) + { + self::add_file_callback($id, $filename, array( + "hash" => function() use ($content) { + return md5($content); + }, + "data_writer" => function($dest) use ($content) { + file_put_contents($dest, $content); + } + )); + } + static public function add_file($id, $file, $filename) { + self::add_file_callback($id, $filename, array( + "hash" => function() use ($file) { + return md5_file($file); + }, + "data_writer" => function($dest) use ($file) { + move_uploaded_file($file, $dest); + } + )); + } + + // TODO: an interface would be nice here, but php doesn't support anonymous + // objects so let's use an array for now + static private function add_file_callback($id, $filename, $callbacks) + { + assert(isset($callbacks["hash"])); + assert(isset($callbacks["data_writer"])); + $CI =& get_instance(); - $hash = md5_file($file); + $hash = $callbacks["hash"](); $dir = $CI->mfile->folder($hash); file_exists($dir) || mkdir ($dir); @@ -80,7 +109,7 @@ class files { $dest = new \service\storage($new_path); $tmpfile = $dest->begin(); - move_uploaded_file($file, $tmpfile); + $callbacks["data_writer"]($tmpfile); $dest->commit(); $CI->mfile->add_file($hash, $id, $filename); -- cgit v1.2.3-24-g4f1b