ArchiveHelpers.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.IO;
  29. using System.Net;
  30. using OpenMetaverse;
  31. using OpenSim.Framework.Serialization;
  32. using OpenSim.Region.Framework.Scenes;
  33. namespace OpenSim.Region.CoreModules.World.Archiver
  34. {
  35. /// <summary>
  36. /// Helper methods for archive manipulation
  37. /// </summary>
  38. /// This is a separate class from ArchiveConstants because we need to bring in very OpenSim specific classes.
  39. public static class ArchiveHelpers
  40. {
  41. /// <summary>
  42. /// Create the filename used for objects in OpenSim Archives.
  43. /// </summary>
  44. /// <param name="objectName"></param>
  45. /// <param name="uuid"></param>
  46. /// <param name="pos"></param>
  47. /// <returns></returns>
  48. public static string CreateObjectFilename(SceneObjectGroup sog)
  49. {
  50. return ArchiveConstants.CreateOarObjectFilename(sog.Name, sog.UUID, sog.AbsolutePosition);
  51. }
  52. /// <summary>
  53. /// Create the path used to store an object in an OpenSim Archive.
  54. /// </summary>
  55. /// <param name="objectName"></param>
  56. /// <param name="uuid"></param>
  57. /// <param name="pos"></param>
  58. /// <returns></returns>
  59. public static string CreateObjectPath(SceneObjectGroup sog)
  60. {
  61. return ArchiveConstants.CreateOarObjectPath(sog.Name, sog.UUID, sog.AbsolutePosition);
  62. }
  63. /// <summary>
  64. /// Resolve path to a working FileStream
  65. /// </summary>
  66. /// <param name="path"></param>
  67. /// <returns></returns>
  68. public static Stream GetStream(string path)
  69. {
  70. if (File.Exists(path))
  71. {
  72. return new FileStream(path, FileMode.Open, FileAccess.Read);
  73. }
  74. else
  75. {
  76. try
  77. {
  78. Uri uri = new Uri(path);
  79. if (uri.Scheme == "file")
  80. {
  81. return new FileStream(uri.AbsolutePath, FileMode.Open, FileAccess.Read);
  82. }
  83. else
  84. {
  85. if (uri.Scheme != "http")
  86. throw new Exception(String.Format("Unsupported URI scheme ({0})", path));
  87. // OK, now we know we have an HTTP URI to work with
  88. return URIFetch(uri);
  89. }
  90. }
  91. catch (UriFormatException)
  92. {
  93. // In many cases the user will put in a plain old filename that cannot be found so assume that
  94. // this is the problem rather than confusing the issue with a UriFormatException
  95. throw new Exception(String.Format("Cannot find file {0}", path));
  96. }
  97. }
  98. }
  99. public static Stream URIFetch(Uri uri)
  100. {
  101. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
  102. // request.Credentials = credentials;
  103. request.ContentLength = 0;
  104. request.KeepAlive = false;
  105. WebResponse response = request.GetResponse();
  106. Stream file = response.GetResponseStream();
  107. // justincc: gonna ignore the content type for now and just try anything
  108. //if (response.ContentType != "application/x-oar")
  109. // throw new Exception(String.Format("{0} does not identify an OAR file", uri.ToString()));
  110. if (response.ContentLength == 0)
  111. throw new Exception(String.Format("{0} returned an empty file", uri.ToString()));
  112. // return new BufferedStream(file, (int) response.ContentLength);
  113. return new BufferedStream(file, 1000000);
  114. }
  115. }
  116. }