UploadBakedTextureHandler.cs 7.0 KB

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