PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/webdav/WebDavConnection.cs

https://github.com/jonpryor/WebDavClient
C# | 193 lines | 172 code | 21 blank | 0 comment | 29 complexity | 7d5de4d80c14dbd40cf78d1621708f6e MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using Mono.Options;
  7. using Cadenza.Net;
  8. namespace Cadenza.Tools.WebDav {
  9. [AttributeUsage (AttributeTargets.Method, AllowMultiple=true)]
  10. class HelpAttribute : Attribute {
  11. public HelpAttribute (string helpText)
  12. {
  13. if (helpText == null)
  14. throw new ArgumentNullException ("helpText");
  15. HelpText = helpText;
  16. }
  17. public string HelpText {get; private set;}
  18. }
  19. class WebDavConnection
  20. {
  21. public WebDavMethodBuilder Builder {get; private set;}
  22. public WebDavConnection ()
  23. {
  24. Builder = new WebDavMethodBuilder ();
  25. }
  26. public static Action<WebDavConnection, string> GetCommand (string command)
  27. {
  28. Action<WebDavConnection, string> action;
  29. if (Commands.TryGetValue (command, out action))
  30. return action;
  31. return null;
  32. }
  33. static readonly Dictionary<string, Action<WebDavConnection, string>> Commands = new Dictionary<string, Action<WebDavConnection, string>>() {
  34. { "help", ShowHelp },
  35. { "get", Download },
  36. { "exit", Exit },
  37. { "server", Server },
  38. { "ls", ListPath },
  39. { "verbose", SetLogging },
  40. };
  41. [Help ("`help`; List all commands")]
  42. static void ShowHelp (WebDavConnection state, string args)
  43. {
  44. Console.WriteLine ("webdav commands--");
  45. foreach (var name in Commands.Keys.OrderBy (k => k)) {
  46. var d = Commands [name];
  47. var h = (HelpAttribute[]) d.Method.GetCustomAttributes (typeof (HelpAttribute), false);
  48. var prefix = " " + name + ":";
  49. if (h == null || h.Length == 0) {
  50. Console.WriteLine (prefix);
  51. continue;
  52. }
  53. if (prefix.Length > 10) {
  54. Console.WriteLine (prefix);
  55. prefix = new string (' ', 10);
  56. } else {
  57. prefix = prefix.PadRight (10);
  58. }
  59. foreach (var a in h)
  60. foreach (var l in Mono.Options.StringCoda.WrappedLines (a.HelpText, 50, 48)) {
  61. Console.WriteLine ("{0}{1}", prefix, l);
  62. prefix = new string (' ', 12);
  63. }
  64. }
  65. }
  66. [Help ("`exit`; Quit the program.")]
  67. static void Exit (WebDavConnection state, string ignored)
  68. {
  69. Environment.Exit (0);
  70. }
  71. [Help ("`server URI`; Connect to WebDAV server URI.")]
  72. static void Server (WebDavConnection state, string server)
  73. {
  74. if (!string.IsNullOrEmpty (server))
  75. state.Builder.Server = new Uri (server);
  76. else
  77. Console.WriteLine ("Server: {0}", state.Builder.Server);
  78. }
  79. [Help ("`ls [PATH [DEPTH [XML-REQUEST]]`;\n" +
  80. "List collection contents.\n" +
  81. "PATH: collection name to list; default=''\n" +
  82. "DEPTH: -1: infinity; 0: item; 1: children\n" +
  83. "XML-REQUEST: XML request to send to server.")]
  84. static void ListPath (WebDavConnection state, string args)
  85. {
  86. string[] v = Parse (args, 3);
  87. string p = v [0];
  88. int? d = v [1] == null ? null : (int?) int.Parse (v [1]);
  89. string x = v [2];
  90. XElement r = null;
  91. if (x != null) {
  92. try {
  93. r = XElement.Parse (x);
  94. } catch (Exception e) {
  95. Console.Error.WriteLine ("Invalid XML in '{0}': {1}", x, e.Message);
  96. return;
  97. }
  98. }
  99. using (var t = state.Builder.CreateFileStatusMethodAsync (p, d, r)) {
  100. try {
  101. t.Wait ();
  102. } catch (Exception e) {
  103. Console.Error.WriteLine ("webdav: {0}", e);
  104. return;
  105. }
  106. if (t.IsFaulted) {
  107. Console.Error.WriteLine ("webdav: {0}", t.Exception.Flatten ());
  108. return;
  109. }
  110. foreach (var e in t.Result.GetResponses ()) {
  111. Console.WriteLine ("{0} {1,10} {2,-12} {3}",
  112. e.ResourceType == null ? " " : e.ResourceType == WebDavResourceType.Collection ? "d" : "-",
  113. e.ContentLength,
  114. e.CreationDate == null ? "" : e.CreationDate.Value.ToString ("MMM d HH:MM"),
  115. e.Href);
  116. }
  117. }
  118. }
  119. static string[] Parse (string source, int max)
  120. {
  121. string[] values = new string [max];
  122. int i = 0;
  123. foreach (string s in ArgumentSource.GetArguments (new StringReader (source))) {
  124. if (i < (values.Length-1))
  125. values [i++] = s;
  126. else
  127. values [i] = values [i] == null ? s : values [i] + " " + s;
  128. }
  129. return values;
  130. }
  131. [Help ("`get SOURCE [TARGET]`: Download SOURCE.\n" +
  132. "If TARGET not specified, use stdout.")]
  133. static void Download (WebDavConnection state, string args)
  134. {
  135. string[] v = Parse (args, 2);
  136. string s = v [0];
  137. string d = v [1];
  138. Stream dest = d != null ? File.OpenWrite (d) : Console.OpenStandardOutput ();
  139. try {
  140. using (var t = state.Builder.CreateDownloadMethodAsync (s, dest)) {
  141. try {
  142. t.Wait ();
  143. } catch (Exception e) {
  144. Console.Error.WriteLine ("webdav: {0}", e);
  145. return;
  146. }
  147. if (t.IsFaulted) {
  148. Console.Error.WriteLine ("webdav: {0}", t.Exception.Flatten ());
  149. return;
  150. }
  151. Console.WriteLine ("StatusCode: {0}", t.Result.ResponseStatusCode);
  152. }
  153. } finally {
  154. if (d != null)
  155. dest.Close ();
  156. }
  157. }
  158. [Help ("`verbose [ENABLE]`: Show logging status.\n" +
  159. "ENABLE: 1/true=enables; 0/false=disables logging.")]
  160. static void SetLogging (WebDavConnection state, string args)
  161. {
  162. if (string.IsNullOrEmpty (args)) {
  163. Console.WriteLine (state.Builder.Log != null);
  164. return;
  165. }
  166. if (args == "1" || args == "true")
  167. state.Builder.Log = Console.Out;
  168. else
  169. state.Builder.Log = null;
  170. }
  171. }
  172. }