LoadImageURLModule.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 OpenSim 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. try
  141. {
  142. HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
  143. if (response.StatusCode == HttpStatusCode.OK)
  144. {
  145. Bitmap image = new Bitmap(response.GetResponseStream());
  146. Size newsize;
  147. // TODO: make this a bit less hard coded
  148. if ((image.Height < 64) && (image.Width < 64))
  149. {
  150. newsize = new Size(32, 32);
  151. }
  152. else if ((image.Height < 128) && (image.Width < 128))
  153. {
  154. newsize = new Size(64, 64);
  155. }
  156. else if ((image.Height < 256) && (image.Width < 256))
  157. {
  158. newsize = new Size(128, 128);
  159. }
  160. else if ((image.Height < 512 && image.Width < 512))
  161. {
  162. newsize = new Size(256, 256);
  163. }
  164. else if ((image.Height < 1024 && image.Width < 1024))
  165. {
  166. newsize = new Size(512, 512);
  167. }
  168. else
  169. {
  170. newsize = new Size(1024, 1024);
  171. }
  172. Bitmap resize = new Bitmap(image, newsize);
  173. byte[] imageJ2000 = new byte[0];
  174. try
  175. {
  176. imageJ2000 = OpenJPEG.EncodeFromImage(resize, true);
  177. }
  178. catch (Exception)
  179. {
  180. m_log.Error("[LOADIMAGEURLMODULE]: OpenJpeg Encode Failed. Empty byte data returned!");
  181. }
  182. m_textureManager.ReturnData(state.RequestID, imageJ2000);
  183. }
  184. }
  185. catch (WebException)
  186. {
  187. }
  188. }
  189. #region Nested type: RequestState
  190. public class RequestState
  191. {
  192. public HttpWebRequest Request = null;
  193. public UUID RequestID = UUID.Zero;
  194. public int TimeOfRequest = 0;
  195. public RequestState(HttpWebRequest request, UUID requestID)
  196. {
  197. Request = request;
  198. RequestID = requestID;
  199. }
  200. }
  201. #endregion
  202. }
  203. }