@@ -584,6 +584,7 @@ def copy(self, from_path, to_path=None):
584
584
from_path must be a full path to a file.
585
585
"""
586
586
path = from_path .strip ("/" )
587
+
587
588
if to_path is not None :
588
589
to_path = to_path .strip ("/" )
589
590
@@ -599,12 +600,20 @@ def copy(self, from_path, to_path=None):
599
600
if model ["type" ] == "directory" :
600
601
raise HTTPError (400 , "Can't copy directories" )
601
602
602
- if to_path is None :
603
+ is_destination_specified = to_path is not None
604
+ if not is_destination_specified :
603
605
to_path = from_dir
604
606
if self .dir_exists (to_path ):
605
607
name = copy_pat .sub ("." , from_name )
606
608
to_name = self .increment_filename (name , to_path , insert = "-Copy" )
607
609
to_path = "{0}/{1}" .format (to_path , to_name )
610
+ elif is_destination_specified :
611
+ if "/" in to_path :
612
+ to_dir , to_name = to_path .rsplit ("/" , 1 )
613
+ if not self .dir_exists (to_dir ):
614
+ raise HTTPError (404 , "No such parent directory: %s to copy file in" % to_dir )
615
+ else :
616
+ raise HTTPError (404 , "No such directory: %s" % to_path )
608
617
609
618
model = self .save (model , to_path )
610
619
return model
@@ -944,6 +953,7 @@ async def copy(self, from_path, to_path=None):
944
953
from_path must be a full path to a file.
945
954
"""
946
955
path = from_path .strip ("/" )
956
+
947
957
if to_path is not None :
948
958
to_path = to_path .strip ("/" )
949
959
@@ -958,12 +968,21 @@ async def copy(self, from_path, to_path=None):
958
968
model .pop ("name" , None )
959
969
if model ["type" ] == "directory" :
960
970
raise HTTPError (400 , "Can't copy directories" )
961
- if to_path is None :
971
+
972
+ is_destination_specified = to_path is not None
973
+ if not is_destination_specified :
962
974
to_path = from_dir
963
975
if await ensure_async (self .dir_exists (to_path )):
964
976
name = copy_pat .sub ("." , from_name )
965
977
to_name = await self .increment_filename (name , to_path , insert = "-Copy" )
966
978
to_path = "{0}/{1}" .format (to_path , to_name )
979
+ elif is_destination_specified :
980
+ if "/" in to_path :
981
+ to_dir , to_name = to_path .rsplit ("/" , 1 )
982
+ if not await ensure_async (self .dir_exists (to_dir )):
983
+ raise HTTPError (404 , "No such parent directory: %s to copy file in" % to_dir )
984
+ else :
985
+ raise HTTPError (404 , "No such directory: %s" % to_path )
967
986
968
987
model = await self .save (model , to_path )
969
988
return model
0 commit comments