LoadImageURLModule.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.Drawing;
  29. using System.IO;
  30. using System.Net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenMetaverse.Imaging;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. using log4net;
  37. using System.Reflection;
  38. namespace OpenSim.Region.CoreModules.Scripting.LoadImageURL
  39. {
  40. public class LoadImageURLModule : IRegionModule, IDynamicTextureRender
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. private string m_name = "LoadImageURL";
  44. private Scene m_scene;
  45. private IDynamicTextureManager m_textureManager;
  46. private string m_proxyurl = "";
  47. private string m_proxyexcepts = "";
  48. #region IDynamicTextureRender Members
  49. public string GetName()
  50. {
  51. return m_name;
  52. }
  53. public string GetContentType()
  54. {
  55. return ("image");
  56. }
  57. public bool SupportsAsynchronous()
  58. {
  59. return true;
  60. }
  61. public byte[] ConvertUrl(string url, string extraParams)
  62. {
  63. return null;
  64. }
  65. public byte[] ConvertStream(Stream data, string extraParams)
  66. {
  67. return null;
  68. }
  69. public bool AsyncConvertUrl(UUID id, string url, string extraParams)
  70. {
  71. MakeHttpRequest(url, id);
  72. return true;
  73. }
  74. public bool AsyncConvertData(UUID id, string bodyData, string extraParams)
  75. {
  76. return false;
  77. }
  78. public void GetDrawStringSize(string text, string fontName, int fontSize,
  79. out double xSize, out double ySize)
  80. {
  81. xSize = 0;
  82. ySize = 0;
  83. }
  84. #endregion
  85. #region IRegionModule Members
  86. public void Initialise(Scene scene, IConfigSource config)
  87. {
  88. if (m_scene == null)
  89. {
  90. m_scene = scene;
  91. }
  92. m_proxyurl = config.Configs["Startup"].GetString("HttpProxy");
  93. m_proxyexcepts = config.Configs["Startup"].GetString("HttpProxyExceptions");
  94. }
  95. public void PostInitialise()
  96. {
  97. m_textureManager = m_scene.RequestModuleInterface<IDynamicTextureManager>();
  98. if (m_textureManager != null)
  99. {
  100. m_textureManager.RegisterRender(GetContentType(), this);
  101. }
  102. }
  103. public void Close()
  104. {
  105. }
  106. public string Name
  107. {
  108. get { return m_name; }
  109. }
  110. public bool IsSharedModule
  111. {
  112. get { return true; }
  113. }
  114. #endregion
  115. private void MakeHttpRequest(string url, UUID requestID)
  116. {
  117. WebRequest request = HttpWebRequest.Create(url);
  118. if (m_proxyurl != null && m_proxyurl.Length > 0)
  119. {
  120. if (m_proxyexcepts != null && m_proxyexcepts.Length > 0)
  121. {
  122. string[] elist = m_proxyexcepts.Split(';');
  123. request.Proxy = new WebProxy(m_proxyurl, true, elist);
  124. }
  125. else
  126. {
  127. request.Proxy = new WebProxy(m_proxyurl, true);
  128. }
  129. }
  130. RequestState state = new RequestState((HttpWebRequest) request, requestID);
  131. // IAsyncResult result = request.BeginGetResponse(new AsyncCallback(HttpRequestReturn), state);
  132. request.BeginGetResponse(new AsyncCallback(HttpRequestReturn), state);
  133. TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
  134. state.TimeOfRequest = (int) t.TotalSeconds;
  135. }
  136. private void HttpRequestReturn(IAsyncResult result)
  137. {
  138. RequestState state = (RequestState) result.AsyncState;
  139. WebRequest request = (WebRequest) state.Request;
  140. Stream stream = null;
  141. byte[] imageJ2000 = new byte[0];
  142. try
  143. {
  144. HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
  145. if (response != null && response.StatusCode == HttpStatusCode.OK)
  146. {
  147. stream = response.GetResponseStream();
  148. if (stream != null)
  149. {
  150. try
  151. {
  152. Bitmap image = new Bitmap(stream);
  153. Size newsize;
  154. // TODO: make this a bit less hard coded
  155. if ((image.Height < 64) && (image.Width < 64))
  156. {
  157. newsize = new Size(32, 32);
  158. }
  159. else if ((image.Height < 128) && (image.Width < 128))
  160. {
  161. newsize = new Size(64, 64);
  162. }
  163. else if ((image.Height < 256) && (image.Width < 256))
  164. {
  165. newsize = new Size(128, 128);
  166. }
  167. else if ((image.Height < 512 && image.Width < 512))
  168. {
  169. newsize = new Size(256, 256);
  170. }
  171. else if ((image.Height < 1024 && image.Width < 1024))
  172. {
  173. newsize = new Size(512, 512);
  174. }
  175. else
  176. {
  177. newsize = new Size(1024, 1024);
  178. }
  179. Bitmap resize = new Bitmap(image, newsize);
  180. imageJ2000 = OpenJPEG.EncodeFromImage(resize, true);
  181. }
  182. catch (Exception)
  183. {
  184. m_log.Error("[LOADIMAGEURLMODULE]: OpenJpeg Conversion Failed. Empty byte data returned!");
  185. }
  186. }
  187. else
  188. {
  189. m_log.WarnFormat("[LOADIMAGEURLMODULE] No data returned");
  190. }
  191. }
  192. }
  193. catch (WebException)
  194. {
  195. }
  196. finally
  197. {
  198. if (stream != null)
  199. {
  200. stream.Close();
  201. }
  202. }
  203. m_log.DebugFormat("[LOADIMAGEURLMODULE] Returning {0} bytes of image data for request {1}",
  204. imageJ2000.Length, state.RequestID);
  205. m_textureManager.ReturnData(state.RequestID, imageJ2000);
  206. }
  207. #region Nested type: RequestState
  208. public class RequestState
  209. {
  210. public HttpWebRequest Request = null;
  211. public UUID RequestID = UUID.Zero;
  212. public int TimeOfRequest = 0;
  213. public RequestState(HttpWebRequest request, UUID requestID)
  214. {
  215. Request = request;
  216. RequestID = requestID;
  217. }
  218. }
  219. #endregion
  220. }
  221. }