]> source.dussan.org Git - nextcloud-server.git/commitdiff
Deduplicate code
authorJoas Schilling <nickvergessen@gmx.de>
Mon, 28 Apr 2014 15:45:03 +0000 (17:45 +0200)
committerJoas Schilling <nickvergessen@gmx.de>
Tue, 29 Apr 2014 08:34:28 +0000 (10:34 +0200)
lib/private/files/view.php

index d0c7e14ef63c0cca58920bef0017f36f02b27253..47fc04c937d1eb9e29dabb536cee9a3ddd585e33 100644 (file)
@@ -271,6 +271,39 @@ class View {
                return $this->basicOperation('file_get_contents', $path, array('read'));
        }
 
+       protected function emit_file_hooks_pre($exists, $path, &$run) {
+               if (!$exists) {
+                       \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array(
+                               Filesystem::signal_param_path => $this->getHookPath($path),
+                               Filesystem::signal_param_run => &$run,
+                       ));
+               } else {
+                       \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array(
+                               Filesystem::signal_param_path => $this->getHookPath($path),
+                               Filesystem::signal_param_run => &$run,
+                       ));
+               }
+               \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array(
+                       Filesystem::signal_param_path => $this->getHookPath($path),
+                       Filesystem::signal_param_run => &$run,
+               ));
+       }
+
+       protected function emit_file_hooks_post($exists, $path) {
+               if (!$exists) {
+                       \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array(
+                               Filesystem::signal_param_path => $this->getHookPath($path),
+                       ));
+               } else {
+                       \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array(
+                               Filesystem::signal_param_path => $this->getHookPath($path),
+                       ));
+               }
+               \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array(
+                       Filesystem::signal_param_path => $this->getHookPath($path),
+               ));
+       }
+
        public function file_put_contents($path, $data) {
                if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier
                        $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
@@ -282,33 +315,7 @@ class View {
                                $exists = $this->file_exists($path);
                                $run = true;
                                if ($this->shouldEmitHooks($path)) {
-                                       if (!$exists) {
-                                               \OC_Hook::emit(
-                                                       Filesystem::CLASSNAME,
-                                                       Filesystem::signal_create,
-                                                       array(
-                                                               Filesystem::signal_param_path => $this->getHookPath($path),
-                                                               Filesystem::signal_param_run => &$run
-                                                       )
-                                               );
-                                       } else {
-                                               \OC_Hook::emit(
-                                                       Filesystem::CLASSNAME,
-                                                       Filesystem::signal_update,
-                                                       array(
-                                                               Filesystem::signal_param_path => $this->getHookPath($path),
-                                                               Filesystem::signal_param_run => &$run,
-                                                       )
-                                               );
-                                       }
-                                       \OC_Hook::emit(
-                                               Filesystem::CLASSNAME,
-                                               Filesystem::signal_write,
-                                               array(
-                                                       Filesystem::signal_param_path => $this->getHookPath($path),
-                                                       Filesystem::signal_param_run => &$run
-                                               )
-                                       );
+                                       $this->emit_file_hooks_pre($exists, $path, $run);
                                }
                                if (!$run) {
                                        return false;
@@ -322,24 +329,7 @@ class View {
                                                Updater::writeHook(array(
                                                        'path' => $this->getHookPath($path)
                                                ));
-                                               if (!$exists) {
-                                                       \OC_Hook::emit(
-                                                               Filesystem::CLASSNAME,
-                                                               Filesystem::signal_post_create,
-                                                               array(Filesystem::signal_param_path => $this->getHookPath($path))
-                                                       );
-                                               } else {
-                                                       \OC_Hook::emit(
-                                                               Filesystem::CLASSNAME,
-                                                               Filesystem::signal_post_update,
-                                                               array(Filesystem::signal_param_path => $this->getHookPath($path))
-                                                       );
-                                               }
-                                               \OC_Hook::emit(
-                                                       Filesystem::CLASSNAME,
-                                                       Filesystem::signal_post_write,
-                                                       array(Filesystem::signal_param_path => $this->getHookPath($path))
-                                               );
+                                               $this->emit_file_hooks_post($exists, $path);
                                        }
                                        \OC_FileProxy::runPostProxies('file_put_contents', $absolutePath, $count);
                                        return $result;
@@ -401,30 +391,7 @@ class View {
                        $run = true;
                        if ($this->shouldEmitHooks() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) {
                                // if it was a rename from a part file to a regular file it was a write and not a rename operation
-                               if (!$exists) {
-                                       \OC_Hook::emit(
-                                               Filesystem::CLASSNAME, Filesystem::signal_create,
-                                               array(
-                                                       Filesystem::signal_param_path => $this->getHookPath($path2),
-                                                       Filesystem::signal_param_run => &$run
-                                               )
-                                       );
-                               } else {
-                                       \OC_Hook::emit(
-                                               Filesystem::CLASSNAME, Filesystem::signal_update,
-                                               array(
-                                                       Filesystem::signal_param_path => $this->getHookPath($path2),
-                                                       Filesystem::signal_param_run => &$run,
-                                               )
-                                       );
-                               }
-                               \OC_Hook::emit(
-                                       Filesystem::CLASSNAME, Filesystem::signal_write,
-                                       array(
-                                               Filesystem::signal_param_path => $this->getHookPath($path2),
-                                               Filesystem::signal_param_run => &$run
-                                       )
-                               );
+                               $this->emit_file_hooks_pre($exists, $path2, $run);
                        } elseif ($this->shouldEmitHooks()) {
                                \OC_Hook::emit(
                                        Filesystem::CLASSNAME, Filesystem::signal_rename,
@@ -481,26 +448,7 @@ class View {
                                if ($this->shouldEmitHooks() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
                                        // if it was a rename from a part file to a regular file it was a write and not a rename operation
                                        Updater::writeHook(array('path' => $this->getHookPath($path2)));
-                                       if (!$exists) {
-                                               \OC_Hook::emit(
-                                                       Filesystem::CLASSNAME,
-                                                       Filesystem::signal_post_create,
-                                                       array(Filesystem::signal_param_path => $this->getHookPath($path2))
-                                               );
-                                       } else {
-                                               \OC_Hook::emit(
-                                                       Filesystem::CLASSNAME,
-                                                       Filesystem::signal_post_update,
-                                                       array(Filesystem::signal_param_path => $this->getHookPath($path2))
-                                               );
-                                       }
-                                       \OC_Hook::emit(
-                                               Filesystem::CLASSNAME,
-                                               Filesystem::signal_post_write,
-                                               array(
-                                                       Filesystem::signal_param_path => $this->getHookPath($path2),
-                                               )
-                                       );
+                                       $this->emit_file_hooks_post($exists, $path2);
                                } elseif ($this->shouldEmitHooks() && $result !== false) {
                                        Updater::renameHook(array(
                                                'oldpath' => $this->getHookPath($path1),
@@ -553,35 +501,7 @@ class View {
                                                Filesystem::signal_param_run => &$run
                                        )
                                );
-                               if ($run and !$exists) {
-                                       \OC_Hook::emit(
-                                               Filesystem::CLASSNAME,
-                                               Filesystem::signal_create,
-                                               array(
-                                                       Filesystem::signal_param_path => $this->getHookPath($path2),
-                                                       Filesystem::signal_param_run => &$run
-                                               )
-                                       );
-                               } elseif ($run) {
-                                       \OC_Hook::emit(
-                                               Filesystem::CLASSNAME,
-                                               Filesystem::signal_update,
-                                               array(
-                                                       Filesystem::signal_param_path => $this->getHookPath($path2),
-                                                       Filesystem::signal_param_run => &$run,
-                                               )
-                                       );
-                               }
-                               if ($run) {
-                                       \OC_Hook::emit(
-                                               Filesystem::CLASSNAME,
-                                               Filesystem::signal_write,
-                                               array(
-                                                       Filesystem::signal_param_path => $this->getHookPath($path2),
-                                                       Filesystem::signal_param_run => &$run
-                                               )
-                                       );
-                               }
+                               $this->emit_file_hooks_pre($exists, $path2, $run);
                        }
                        if ($run) {
                                $mp1 = $this->getMountPoint($path1 . $postFix1);
@@ -621,24 +541,7 @@ class View {
                                                        Filesystem::signal_param_newpath => $this->getHookPath($path2)
                                                )
                                        );
-                                       if (!$exists) {
-                                               \OC_Hook::emit(
-                                                       Filesystem::CLASSNAME,
-                                                       Filesystem::signal_post_create,
-                                                       array(Filesystem::signal_param_path => $this->getHookPath($path2))
-                                               );
-                                       } else {
-                                               \OC_Hook::emit(
-                                                       Filesystem::CLASSNAME,
-                                                       Filesystem::signal_post_update,
-                                                       array(Filesystem::signal_param_path => $this->getHookPath($path2))
-                                               );
-                                       }
-                                       \OC_Hook::emit(
-                                               Filesystem::CLASSNAME,
-                                               Filesystem::signal_post_write,
-                                               array(Filesystem::signal_param_path => $this->getHookPath($path2))
-                                       );
+                                       $this->emit_file_hooks_post($exists, $path2);
                                }
                                return $result;
                        } else {