LoadImageURLModule.cs 9.6 KB

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