@@ -22,47 +22,65 @@ public function getRename()
2222 $ new_name = parent ::translateFromUtf8 (trim (request ('new_name ' )));
2323
2424 $ old_file = parent ::getCurrentPath ($ old_name );
25-
26- if (empty ($ new_name )) {
27- if (File::isDirectory ($ old_file )) {
28- return parent ::error ('folder-name ' );
29- } else {
30- return parent ::error ('file-name ' );
31- }
25+ if (File::isDirectory ($ old_file )) {
26+ return $ this ->renameDirectory ($ old_name , $ new_name );
27+ } else {
28+ return $ this ->renameFile ($ old_name , $ new_name );
3229 }
30+ }
3331
34- if (! File::isDirectory ($ old_file )) {
35- $ extension = File::extension ($ old_file );
36- $ new_name = str_replace ('. ' . $ extension , '' , $ new_name ) . '. ' . $ extension ;
32+ protected function renameDirectory ($ old_name , $ new_name )
33+ {
34+ if (empty ($ new_name )) {
35+ return parent ::error ('folder-name ' );
3736 }
3837
38+ $ old_file = parent ::getCurrentPath ($ old_name );
3939 $ new_file = parent ::getCurrentPath ($ new_name );
4040
41- if (File::isDirectory ($ old_file )) {
42- event (new FolderIsRenaming ($ old_file , $ new_file ));
43- } else {
44- event (new ImageIsRenaming ($ old_file , $ new_file ));
45- }
41+ event (new FolderIsRenaming ($ old_file , $ new_file ));
4642
4743 if (config ('lfm.alphanumeric_directory ' ) && preg_match ('/[^\w-]/i ' , $ new_name )) {
4844 return parent ::error ('folder-alnum ' );
49- } elseif (File::exists ($ new_file )) {
45+ }
46+
47+ if (File::exists ($ new_file )) {
5048 return parent ::error ('rename ' );
5149 }
5250
53- if (File::isDirectory ($ old_file )) {
54- File::move ($ old_file , $ new_file );
55- event (new FolderWasRenamed ($ old_file , $ new_file ));
51+ File::move ($ old_file , $ new_file );
52+ event (new FolderWasRenamed ($ old_file , $ new_file ));
5653
57- return parent ::$ success_response ;
54+ return parent ::$ success_response ;
55+ }
56+
57+ protected function renameFile ($ old_name , $ new_name )
58+ {
59+ if (empty ($ new_name )) {
60+ return parent ::error ('file-name ' );
61+ }
62+
63+ $ old_file = parent ::getCurrentPath ($ old_name );
64+ $ extension = File::extension ($ old_file );
65+ $ new_file = parent ::getCurrentPath (basename ($ new_name , ". $ extension " ) . ". $ extension " );
66+
67+ if (config ('lfm.alphanumeric_filename ' ) && preg_match ('/[^\w-.]/i ' , $ new_name )) {
68+ return parent ::error ('file-alnum ' );
5869 }
5970
60- if (parent ::fileIsImage ($ old_file )) {
71+ // TODO Should be "FileIsRenaming"
72+ event (new ImageIsRenaming ($ old_file , $ new_file ));
73+
74+ if (File::exists ($ new_file )) {
75+ return parent ::error ('rename ' );
76+ }
77+
78+ if (parent ::fileIsImage ($ old_file ) && File::exists (parent ::getThumbPath ($ old_name ))) {
6179 File::move (parent ::getThumbPath ($ old_name ), parent ::getThumbPath ($ new_name ));
6280 }
6381
6482 File::move ($ old_file , $ new_file );
65-
83+ // TODO Should be "FileWasRenamed"
6684 event (new ImageWasRenamed ($ old_file , $ new_file ));
6785
6886 return parent ::$ success_response ;
0 commit comments