diff options
author | Florian Pritz <bluewind@xinu.at> | 2015-02-21 00:00:08 +0100 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2015-02-21 00:00:08 +0100 |
commit | c6208bd2271fa02662c4b106c61ca712b46499e5 (patch) | |
tree | 780e32a31587fcea8cd08822634e71ced70a5932 /application | |
parent | 0443d0829ab7e2d30825fded2f172b74d26b5b3c (diff) |
s/files: add add_file_data for future code deduplication
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'application')
-rw-r--r-- | application/service/files.php | 33 |
1 files changed, 31 insertions, 2 deletions
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); |