UploadBakedTextureHandler.cs 7.4 KB

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