LoadImageURLModule.cs 10 KB

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