/MiniFace/MiniFace/CkFinder/Connector/CommandHandlers/RenameFolderCommandHandler.cs

http://ruandao.codeplex.com · C# · 138 lines · 106 code · 16 blank · 16 comment · 9 complexity · fd709072cb566773fc792993711b39b6 MD5 · raw file

  1. /*
  2. * CKFinder
  3. * ========
  4. * http://ckfinder.com
  5. * Copyright (C) 2007-2009, CKSource - Frederico Knabben. All rights reserved.
  6. *
  7. * The software, this file and its contents are subject to the CKFinder
  8. * License. Please read the license.txt file before using, installing, copying,
  9. * modifying or distribute this file or part of its contents. The contents of
  10. * this file is part of the Source Code of CKFinder.
  11. */
  12. using System;
  13. using System.Web;
  14. using System.Xml;
  15. using System.Globalization;
  16. using System.Text.RegularExpressions;
  17. namespace CKFinder.Connector.CommandHandlers
  18. {
  19. internal class RenameFolderCommandHandler : XmlCommandHandlerBase
  20. {
  21. public RenameFolderCommandHandler()
  22. : base()
  23. {
  24. }
  25. protected override void BuildXml()
  26. {
  27. if ( !this.CurrentFolder.CheckAcl( AccessControlRules.FolderRename ) )
  28. {
  29. ConnectorException.Throw( Errors.Unauthorized );
  30. return;
  31. }
  32. // The root folder cannot be deleted.
  33. if ( this.CurrentFolder.ClientPath == "/" )
  34. {
  35. ConnectorException.Throw( Errors.InvalidRequest );
  36. return;
  37. }
  38. string newFileName = Request[ "NewFolderName" ];
  39. if ( !Connector.CheckFileName( newFileName ) || Config.Current.CheckIsHiddenFolder( newFileName ) )
  40. {
  41. ConnectorException.Throw( Errors.InvalidName );
  42. return;
  43. }
  44. // Get the current folder.
  45. System.IO.DirectoryInfo oDir = new System.IO.DirectoryInfo( this.CurrentFolder.ServerPath );
  46. bool bMoved = false;
  47. try
  48. {
  49. if ( !oDir.Exists )
  50. ConnectorException.Throw( Errors.InvalidRequest );
  51. else
  52. {
  53. // Build the new folder path.
  54. string newFolderPath = System.IO.Path.Combine( oDir.Parent.FullName, newFileName );
  55. if ( System.IO.Directory.Exists( newFolderPath ) )
  56. {
  57. ConnectorException.Throw( Errors.AlreadyExist );
  58. return;
  59. }
  60. oDir.MoveTo( newFolderPath );
  61. bMoved = true;
  62. }
  63. }
  64. catch ( System.UnauthorizedAccessException )
  65. {
  66. ConnectorException.Throw( Errors.AccessDenied );
  67. }
  68. catch ( System.ArgumentException )
  69. {
  70. ConnectorException.Throw( Errors.InvalidName );
  71. }
  72. catch ( System.NotSupportedException )
  73. {
  74. ConnectorException.Throw( Errors.InvalidName );
  75. }
  76. catch ( System.IO.PathTooLongException )
  77. {
  78. ConnectorException.Throw( Errors.InvalidName );
  79. }
  80. catch ( System.IO.IOException )
  81. {
  82. ConnectorException.Throw( Errors.Unknown );
  83. }
  84. catch ( Exception )
  85. {
  86. ConnectorException.Throw( Errors.Unknown );
  87. }
  88. if ( bMoved )
  89. {
  90. try
  91. {
  92. // Get the thumbnails folder.
  93. System.IO.DirectoryInfo oThumbsDir = new System.IO.DirectoryInfo( this.CurrentFolder.ThumbsServerPath );
  94. // Build the new folder path.
  95. string newThumbsFolderPath = System.IO.Path.Combine( oThumbsDir.Parent.FullName, newFileName );
  96. if ( System.IO.Directory.Exists( newThumbsFolderPath ) )
  97. {
  98. System.IO.File.Delete( this.CurrentFolder.ThumbsServerPath );
  99. }
  100. else
  101. {
  102. try
  103. {
  104. oThumbsDir.MoveTo( newThumbsFolderPath );
  105. }
  106. catch
  107. {
  108. System.IO.File.Delete( this.CurrentFolder.ThumbsServerPath );
  109. }
  110. }
  111. }
  112. catch { /* No errors if we are not able to delete the thumb. */ }
  113. string newFolderPath = Regex.Replace( this.CurrentFolder.ClientPath, "[^/]+/?$", newFileName ) + "/";
  114. string newFolderUrl = this.CurrentFolder.ResourceTypeInfo.Url + newFolderPath.TrimStart( '/' );
  115. XmlNode oRenamedNode = XmlUtil.AppendElement( this.ConnectorNode, "RenamedFolder" );
  116. XmlUtil.SetAttribute( oRenamedNode, "newName", newFileName );
  117. XmlUtil.SetAttribute( oRenamedNode, "newPath", newFolderPath );
  118. XmlUtil.SetAttribute( oRenamedNode, "newUrl", newFolderUrl );
  119. }
  120. }
  121. }
  122. }