UploadBakedTextureHandler.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.Collections;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.Drawing;
  32. using System.Drawing.Imaging;
  33. using System.Reflection;
  34. using System.IO;
  35. using System.Web;
  36. using log4net;
  37. using Nini.Config;
  38. using OpenMetaverse;
  39. using OpenMetaverse.StructuredData;
  40. using OpenMetaverse.Imaging;
  41. using OpenSim.Framework;
  42. using OpenSim.Framework.Capabilities;
  43. using OpenSim.Framework.Servers;
  44. using OpenSim.Framework.Servers.HttpServer;
  45. using OpenSim.Region.Framework.Interfaces;
  46. using OpenSim.Services.Interfaces;
  47. using Caps = OpenSim.Framework.Capabilities.Caps;
  48. namespace OpenSim.Capabilities.Handlers
  49. {
  50. public class UploadBakedTextureHandler
  51. {
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. private Caps m_HostCapsObj;
  54. private IAssetService m_assetService;
  55. private bool m_persistBakedTextures;
  56. public UploadBakedTextureHandler(Caps caps, IAssetService assetService, bool persistBakedTextures)
  57. {
  58. m_HostCapsObj = caps;
  59. m_assetService = assetService;
  60. m_persistBakedTextures = persistBakedTextures;
  61. }
  62. /// <summary>
  63. /// Handle a request from the client for a Uri to upload a baked texture.
  64. /// </summary>
  65. /// <param name="request"></param>
  66. /// <param name="path"></param>
  67. /// <param name="param"></param>
  68. /// <param name="httpRequest"></param>
  69. /// <param name="httpResponse"></param>
  70. /// <returns>The upload response if the request is successful, null otherwise.</returns>
  71. public string UploadBakedTexture(
  72. string request, string path, string param, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  73. {
  74. try
  75. {
  76. string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath;
  77. string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000");
  78. BakedTextureUploader uploader =
  79. new BakedTextureUploader(capsBase + uploaderPath, m_HostCapsObj.HttpListener, m_HostCapsObj.AgentID);
  80. uploader.OnUpLoad += BakedTextureUploaded;
  81. m_HostCapsObj.HttpListener.AddStreamHandler(
  82. new BinaryStreamHandler(
  83. "POST", capsBase + uploaderPath, uploader.uploaderCaps, "UploadBakedTexture", null));
  84. string protocol = "http://";
  85. if (m_HostCapsObj.SSLCaps)
  86. protocol = "https://";
  87. string uploaderURL = protocol + m_HostCapsObj.HostName + ":" +
  88. m_HostCapsObj.Port.ToString() + capsBase + uploaderPath;
  89. LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
  90. uploadResponse.uploader = uploaderURL;
  91. uploadResponse.state = "upload";
  92. return LLSDHelpers.SerialiseLLSDReply(uploadResponse);
  93. }
  94. catch (Exception e)
  95. {
  96. m_log.ErrorFormat("[UPLOAD BAKED TEXTURE HANDLER]: {0}{1}", e.Message, e.StackTrace);
  97. }
  98. return null;
  99. }
  100. /// <summary>
  101. /// Called when a baked texture has been successfully uploaded by a client.
  102. /// </summary>
  103. /// <param name="assetID"></param>
  104. /// <param name="data"></param>
  105. private void BakedTextureUploaded(UUID assetID, byte[] data)
  106. {
  107. m_log.DebugFormat("[UPLOAD BAKED TEXTURE HANDLER]: Received baked texture {0}", assetID.ToString());
  108. AssetBase asset;
  109. asset = new AssetBase(assetID, "Baked Texture", (sbyte)AssetType.Texture, m_HostCapsObj.AgentID.ToString());
  110. asset.Data = data;
  111. asset.Temporary = true;
  112. asset.Local = !m_persistBakedTextures; // Local assets aren't persisted, non-local are
  113. m_assetService.Store(asset);
  114. }
  115. }
  116. class BakedTextureUploader
  117. {
  118. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  119. public event Action<UUID, byte[]> OnUpLoad;
  120. private string uploaderPath = String.Empty;
  121. private UUID newAssetID;
  122. private IHttpServer httpListener;
  123. private UUID AgentId = UUID.Zero;
  124. public BakedTextureUploader(string path, IHttpServer httpServer, UUID uUID)
  125. {
  126. newAssetID = UUID.Random();
  127. uploaderPath = path;
  128. httpListener = httpServer;
  129. AgentId = uUID;
  130. // m_log.InfoFormat("[CAPS] baked texture upload starting for {0}",newAssetID);
  131. }
  132. /// <summary>
  133. /// Handle raw uploaded baked texture data.
  134. /// </summary>
  135. /// <param name="data"></param>
  136. /// <param name="path"></param>
  137. /// <param name="param"></param>
  138. /// <returns></returns>
  139. public string uploaderCaps(byte[] data, string path, string param)
  140. {
  141. Action<UUID, byte[]> handlerUpLoad = OnUpLoad;
  142. // Don't do this asynchronously, otherwise it's possible for the client to send set appearance information
  143. // on another thread which might send out avatar updates before the asset has been put into the asset
  144. // service.
  145. if (handlerUpLoad != null)
  146. handlerUpLoad(newAssetID, data);
  147. string res = String.Empty;
  148. LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
  149. uploadComplete.new_asset = newAssetID.ToString();
  150. uploadComplete.new_inventory_item = UUID.Zero;
  151. uploadComplete.state = "complete";
  152. res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
  153. httpListener.RemoveStreamHandler("POST", uploaderPath);
  154. // m_log.DebugFormat("[BAKED TEXTURE UPLOADER]: baked texture upload completed for {0}", newAssetID);
  155. return res;
  156. }
  157. }
  158. }