NewFileAgentInventoryVariablePriceModule.cs 11 KB

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