NewFileAgentInventoryVariablePriceModule.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.Reflection;
  31. using System.IO;
  32. using System.Web;
  33. using Mono.Addins;
  34. using log4net;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Servers;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.Framework.Scenes;
  43. using OpenSim.Services.Interfaces;
  44. using Caps = OpenSim.Framework.Capabilities.Caps;
  45. using OpenSim.Framework.Capabilities;
  46. namespace OpenSim.Region.ClientStack.Linden
  47. {
  48. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  49. public class NewFileAgentInventoryVariablePriceModule : INonSharedRegionModule
  50. {
  51. // private static readonly ILog m_log =
  52. // LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. private Scene m_scene;
  54. // private IAssetService m_assetService;
  55. private bool m_dumpAssetsToFile = false;
  56. private bool m_enabled = true;
  57. #region IRegionModuleBase Members
  58. public Type ReplaceableInterface
  59. {
  60. get { return null; }
  61. }
  62. public void Initialise(IConfigSource source)
  63. {
  64. IConfig meshConfig = source.Configs["Mesh"];
  65. if (meshConfig == null)
  66. return;
  67. m_enabled = meshConfig.GetBoolean("AllowMeshUpload", true);
  68. }
  69. public void AddRegion(Scene pScene)
  70. {
  71. m_scene = pScene;
  72. }
  73. public void RemoveRegion(Scene scene)
  74. {
  75. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  76. m_scene = null;
  77. }
  78. public void RegionLoaded(Scene scene)
  79. {
  80. // m_assetService = m_scene.RequestModuleInterface<IAssetService>();
  81. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  82. }
  83. #endregion
  84. #region IRegionModule Members
  85. public void Close() { }
  86. public string Name { get { return "NewFileAgentInventoryVariablePriceModule"; } }
  87. public void RegisterCaps(UUID agentID, Caps caps)
  88. {
  89. if(!m_enabled)
  90. return;
  91. UUID capID = UUID.Random();
  92. // m_log.Debug("[NEW FILE AGENT INVENTORY VARIABLE PRICE]: /CAPS/" + capID);
  93. caps.RegisterHandler("NewFileAgentInventoryVariablePrice",
  94. new LLSDStreamhandler<LLSDAssetUploadRequest, LLSDNewFileAngentInventoryVariablePriceReplyResponse>("POST",
  95. "/CAPS/" + capID.ToString(),
  96. delegate(LLSDAssetUploadRequest req)
  97. {
  98. return NewAgentInventoryRequest(req,agentID);
  99. }));
  100. }
  101. #endregion
  102. public LLSDNewFileAngentInventoryVariablePriceReplyResponse NewAgentInventoryRequest(LLSDAssetUploadRequest llsdRequest, UUID agentID)
  103. {
  104. //TODO: The Mesh uploader uploads many types of content. If you're going to implement a Money based limit
  105. // You need to be aware of this and
  106. //if (llsdRequest.asset_type == "texture" ||
  107. // llsdRequest.asset_type == "animation" ||
  108. // llsdRequest.asset_type == "sound")
  109. // {
  110. IClientAPI client = null;
  111. IMoneyModule mm = m_scene.RequestModuleInterface<IMoneyModule>();
  112. if (mm != null)
  113. {
  114. if (m_scene.TryGetClient(agentID, out client))
  115. {
  116. if (!mm.UploadCovered(client, mm.UploadCharge))
  117. {
  118. if (client != null)
  119. client.SendAgentAlertMessage("Unable to upload asset. Insufficient funds.", false);
  120. LLSDNewFileAngentInventoryVariablePriceReplyResponse errorResponse = new LLSDNewFileAngentInventoryVariablePriceReplyResponse();
  121. errorResponse.rsvp = "";
  122. errorResponse.state = "error";
  123. return errorResponse;
  124. }
  125. }
  126. }
  127. // }
  128. string assetName = llsdRequest.name;
  129. string assetDes = llsdRequest.description;
  130. string capsBase = "/CAPS/NewFileAgentInventoryVariablePrice/";
  131. UUID newAsset = UUID.Random();
  132. UUID newInvItem = UUID.Random();
  133. UUID parentFolder = llsdRequest.folder_id;
  134. string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000") + "/";
  135. AssetUploader uploader =
  136. new AssetUploader(assetName, assetDes, newAsset, newInvItem, parentFolder, llsdRequest.inventory_type,
  137. llsdRequest.asset_type, capsBase + uploaderPath, MainServer.Instance, m_dumpAssetsToFile);
  138. MainServer.Instance.AddStreamHandler(
  139. new BinaryStreamHandler("POST", capsBase + uploaderPath, uploader.uploaderCaps));
  140. string protocol = "http://";
  141. if (MainServer.Instance.UseSSL)
  142. protocol = "https://";
  143. string uploaderURL = protocol + m_scene.RegionInfo.ExternalHostName + ":" + MainServer.Instance.Port.ToString() + capsBase +
  144. uploaderPath;
  145. LLSDNewFileAngentInventoryVariablePriceReplyResponse uploadResponse = new LLSDNewFileAngentInventoryVariablePriceReplyResponse();
  146. uploadResponse.rsvp = uploaderURL;
  147. uploadResponse.state = "upload";
  148. uploadResponse.resource_cost = 0;
  149. uploadResponse.upload_price = 0;
  150. uploader.OnUpLoad += //UploadCompleteHandler;
  151. delegate(
  152. string passetName, string passetDescription, UUID passetID,
  153. UUID pinventoryItem, UUID pparentFolder, byte[] pdata, string pinventoryType,
  154. string passetType)
  155. {
  156. UploadCompleteHandler(passetName, passetDescription, passetID,
  157. pinventoryItem, pparentFolder, pdata, pinventoryType,
  158. passetType,agentID);
  159. };
  160. return uploadResponse;
  161. }
  162. public void UploadCompleteHandler(string assetName, string assetDescription, UUID assetID,
  163. UUID inventoryItem, UUID parentFolder, byte[] data, string inventoryType,
  164. string assetType,UUID AgentID)
  165. {
  166. sbyte assType = 0;
  167. sbyte inType = 0;
  168. if (inventoryType == "sound")
  169. {
  170. inType = 1;
  171. assType = 1;
  172. }
  173. else if (inventoryType == "animation")
  174. {
  175. inType = 19;
  176. assType = 20;
  177. }
  178. else if (inventoryType == "wearable")
  179. {
  180. inType = 18;
  181. switch (assetType)
  182. {
  183. case "bodypart":
  184. assType = 13;
  185. break;
  186. case "clothing":
  187. assType = 5;
  188. break;
  189. }
  190. }
  191. else if (inventoryType == "mesh")
  192. {
  193. inType = (sbyte)InventoryType.Mesh;
  194. assType = (sbyte)AssetType.Mesh;
  195. }
  196. AssetBase asset;
  197. asset = new AssetBase(assetID, assetName, assType, AgentID.ToString());
  198. asset.Data = data;
  199. if (m_scene.AssetService != null)
  200. m_scene.AssetService.Store(asset);
  201. InventoryItemBase item = new InventoryItemBase();
  202. item.Owner = AgentID;
  203. item.CreatorId = AgentID.ToString();
  204. item.ID = inventoryItem;
  205. item.AssetID = asset.FullID;
  206. item.Description = assetDescription;
  207. item.Name = assetName;
  208. item.AssetType = assType;
  209. item.InvType = inType;
  210. item.Folder = parentFolder;
  211. item.CurrentPermissions = (uint)PermissionMask.All;
  212. item.BasePermissions = (uint)PermissionMask.All;
  213. item.EveryOnePermissions = 0;
  214. item.NextPermissions = (uint)PermissionMask.All;
  215. item.CreationDate = Util.UnixTimeSinceEpoch();
  216. m_scene.AddInventoryItem(item);
  217. }
  218. }
  219. }