UploadBakedTextureModule.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.Net;
  29. using System.Reflection;
  30. using System.Timers;
  31. using log4net;
  32. using Nini.Config;
  33. using Mono.Addins;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Framework.Capabilities;
  40. using Caps = OpenSim.Framework.Capabilities.Caps;
  41. namespace OpenSim.Region.ClientStack.Linden
  42. {
  43. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "UploadBakedTextureModule")]
  44. public class UploadBakedTextureModule : ISharedRegionModule
  45. {
  46. private static readonly ILog m_log =LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private int m_nscenes;
  48. IAssetCache m_assetCache = null;
  49. private string m_URL;
  50. public void Initialise(IConfigSource source)
  51. {
  52. IConfig config = source.Configs["ClientStack.LindenCaps"];
  53. if (config == null)
  54. return;
  55. m_URL = config.GetString("Cap_UploadBakedTexture", string.Empty);
  56. }
  57. public void AddRegion(Scene s)
  58. {
  59. }
  60. public void RemoveRegion(Scene s)
  61. {
  62. s.EventManager.OnRegisterCaps -= RegisterCaps;
  63. --m_nscenes;
  64. if(m_nscenes <= 0)
  65. m_assetCache = null;
  66. }
  67. public void RegionLoaded(Scene s)
  68. {
  69. if (m_assetCache == null)
  70. m_assetCache = s.RequestModuleInterface <IAssetCache>();
  71. if (m_assetCache != null)
  72. {
  73. ++m_nscenes;
  74. s.EventManager.OnRegisterCaps += RegisterCaps;
  75. }
  76. }
  77. public void PostInitialise()
  78. {
  79. }
  80. public void Close() { }
  81. public string Name { get { return "UploadBakedTextureModule"; } }
  82. public Type ReplaceableInterface
  83. {
  84. get { return null; }
  85. }
  86. public void RegisterCaps(UUID agentID, Caps caps)
  87. {
  88. if (m_URL == "localhost")
  89. {
  90. caps.RegisterSimpleHandler("UploadBakedTexture",
  91. new SimpleStreamHandler("/" + UUID.Random(), delegate (IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  92. {
  93. UploadBakedTexture(httpRequest, httpResponse, agentID, caps, m_assetCache);
  94. }));
  95. }
  96. else if(!string.IsNullOrWhiteSpace(m_URL))
  97. {
  98. caps.RegisterHandler("UploadBakedTexture", m_URL);
  99. }
  100. }
  101. public void UploadBakedTexture(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, UUID agentID, Caps caps, IAssetCache cache)
  102. {
  103. if(httpRequest.HttpMethod != "POST")
  104. {
  105. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  106. return;
  107. }
  108. try
  109. {
  110. string capsBase = "/" + UUID.Random()+"-BK";
  111. string protocol = caps.SSLCaps ? "https://" : "http://";
  112. string uploaderURL = protocol + caps.HostName + ":" + caps.Port.ToString() + capsBase;
  113. LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
  114. uploadResponse.uploader = uploaderURL;
  115. uploadResponse.state = "upload";
  116. BakedTextureUploader uploader =
  117. new BakedTextureUploader(capsBase, caps.HttpListener, agentID, cache, httpRequest.RemoteIPEndPoint.Address);
  118. var uploaderHandler = new SimpleBinaryHandler("POST", capsBase, uploader.process);
  119. uploaderHandler.MaxDataSize = 6000000; // change per asset type?
  120. caps.HttpListener.AddSimpleStreamHandler(uploaderHandler);
  121. httpResponse.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(uploadResponse));
  122. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  123. return;
  124. }
  125. catch (Exception e)
  126. {
  127. m_log.ErrorFormat("[UPLOAD BAKED TEXTURE HANDLER]: {0}{1}", e.Message, e.StackTrace);
  128. }
  129. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  130. }
  131. }
  132. class BakedTextureUploader
  133. {
  134. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  135. private string m_uploaderPath = String.Empty;
  136. private IHttpServer m_httpListener;
  137. private UUID m_agentID = UUID.Zero;
  138. private IPAddress m_remoteAddress;
  139. private IAssetCache m_assetCache;
  140. private Timer m_timeout;
  141. public BakedTextureUploader(string path, IHttpServer httpServer, UUID agentID, IAssetCache cache, IPAddress remoteAddress)
  142. {
  143. m_uploaderPath = path;
  144. m_httpListener = httpServer;
  145. m_agentID = agentID;
  146. m_remoteAddress = remoteAddress;
  147. m_assetCache = cache;
  148. m_timeout = new Timer();
  149. m_timeout.Elapsed += Timeout;
  150. m_timeout.AutoReset = false;
  151. m_timeout.Interval = 30000;
  152. m_timeout.Start();
  153. }
  154. private void Timeout(Object source, ElapsedEventArgs e)
  155. {
  156. m_httpListener.RemoveSimpleStreamHandler(m_uploaderPath);
  157. m_timeout.Dispose();
  158. }
  159. /// <summary>
  160. /// Handle raw uploaded baked texture data.
  161. /// </summary>
  162. /// <param name="data"></param>
  163. /// <param name="path"></param>
  164. /// <param name="param"></param>
  165. /// <returns></returns>
  166. public void process(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, byte[] data)
  167. {
  168. m_timeout.Stop();
  169. m_httpListener.RemoveSimpleStreamHandler(m_uploaderPath);
  170. m_timeout.Dispose();
  171. if (!httpRequest.RemoteIPEndPoint.Address.Equals(m_remoteAddress))
  172. {
  173. httpResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
  174. return;
  175. }
  176. // need to check if data is a baked
  177. try
  178. {
  179. UUID newAssetID = UUID.Random();
  180. AssetBase asset = new AssetBase(newAssetID, "Baked Texture", (sbyte)AssetType.Texture, m_agentID.ToString());
  181. asset.Data = data;
  182. asset.Temporary = true;
  183. asset.Local = true;
  184. //asset.Flags = AssetFlags.AvatarBake;
  185. m_assetCache.Cache(asset);
  186. LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
  187. uploadComplete.new_asset = newAssetID.ToString();
  188. uploadComplete.new_inventory_item = UUID.Zero;
  189. uploadComplete.state = "complete";
  190. httpResponse.RawBuffer = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(uploadComplete));
  191. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  192. return;
  193. }
  194. catch { }
  195. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  196. }
  197. }
  198. }