NewFileAgentInventoryVariablePriceModule.cs 11 KB

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