AssetService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 Nini.Config;
  30. using log4net;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Console;
  33. using OpenSim.Data;
  34. using OpenSim.Services.Interfaces;
  35. using OpenMetaverse;
  36. namespace OpenSim.Services.AssetService
  37. {
  38. public class AssetService : AssetServiceBase, IAssetService
  39. {
  40. private static readonly ILog m_log =
  41. LogManager.GetLogger(
  42. MethodBase.GetCurrentMethod().DeclaringType);
  43. public AssetService(IConfigSource config) : base(config)
  44. {
  45. MainConsole.Instance.Commands.AddCommand("kfs", false,
  46. "show digest",
  47. "show digest <ID>",
  48. "Show asset digest", HandleShowDigest);
  49. MainConsole.Instance.Commands.AddCommand("kfs", false,
  50. "delete asset",
  51. "delete asset <ID>",
  52. "Delete asset from database", HandleDeleteAsset);
  53. if (m_AssetLoader != null)
  54. {
  55. IConfig assetConfig = config.Configs["AssetService"];
  56. if (assetConfig == null)
  57. throw new Exception("No AssetService configuration");
  58. string loaderArgs = assetConfig.GetString("AssetLoaderArgs",
  59. String.Empty);
  60. bool assetLoaderEnabled = assetConfig.GetBoolean("AssetLoaderEnabled", true);
  61. if (assetLoaderEnabled)
  62. {
  63. m_log.InfoFormat("[ASSET]: Loading default asset set from {0}", loaderArgs);
  64. m_AssetLoader.ForEachDefaultXmlAsset(loaderArgs,
  65. delegate(AssetBase a)
  66. {
  67. Store(a);
  68. });
  69. }
  70. m_log.Info("[ASSET CONNECTOR]: Local asset service enabled");
  71. }
  72. }
  73. public AssetBase Get(string id)
  74. {
  75. //m_log.DebugFormat("[ASSET SERVICE]: Get asset {0}", id);
  76. UUID assetID;
  77. if (!UUID.TryParse(id, out assetID))
  78. return null;
  79. return m_Database.GetAsset(assetID);
  80. }
  81. public AssetMetadata GetMetadata(string id)
  82. {
  83. UUID assetID;
  84. if (!UUID.TryParse(id, out assetID))
  85. return null;
  86. AssetBase asset = m_Database.GetAsset(assetID);
  87. return asset.Metadata;
  88. }
  89. public byte[] GetData(string id)
  90. {
  91. UUID assetID;
  92. if (!UUID.TryParse(id, out assetID))
  93. return null;
  94. AssetBase asset = m_Database.GetAsset(assetID);
  95. return asset.Data;
  96. }
  97. public bool Get(string id, Object sender, AssetRetrieved handler)
  98. {
  99. //m_log.DebugFormat("[AssetService]: Get asset async {0}", id);
  100. UUID assetID;
  101. if (!UUID.TryParse(id, out assetID))
  102. return false;
  103. AssetBase asset = m_Database.GetAsset(assetID);
  104. //m_log.DebugFormat("[AssetService]: Got asset {0}", asset);
  105. handler(id, sender, asset);
  106. return true;
  107. }
  108. public string Store(AssetBase asset)
  109. {
  110. //m_log.DebugFormat("[ASSET SERVICE]: Store asset {0} {1}", asset.Name, asset.ID);
  111. m_Database.StoreAsset(asset);
  112. return asset.ID;
  113. }
  114. public bool UpdateContent(string id, byte[] data)
  115. {
  116. return false;
  117. }
  118. public bool Delete(string id)
  119. {
  120. return false;
  121. }
  122. void HandleShowDigest(string module, string[] args)
  123. {
  124. if (args.Length < 3)
  125. {
  126. MainConsole.Instance.Output("Syntax: show digest <ID>");
  127. return;
  128. }
  129. AssetBase asset = Get(args[2]);
  130. if (asset == null || asset.Data.Length == 0)
  131. {
  132. MainConsole.Instance.Output("Asset not found");
  133. return;
  134. }
  135. int i;
  136. MainConsole.Instance.Output(String.Format("Name: {0}", asset.Name));
  137. MainConsole.Instance.Output(String.Format("Description: {0}", asset.Description));
  138. MainConsole.Instance.Output(String.Format("Type: {0}", asset.Type));
  139. MainConsole.Instance.Output(String.Format("Content-type: {0}", asset.Metadata.ContentType));
  140. for (i = 0 ; i < 5 ; i++)
  141. {
  142. int off = i * 16;
  143. if (asset.Data.Length <= off)
  144. break;
  145. int len = 16;
  146. if (asset.Data.Length < off + len)
  147. len = asset.Data.Length - off;
  148. byte[] line = new byte[len];
  149. Array.Copy(asset.Data, off, line, 0, len);
  150. string text = BitConverter.ToString(line);
  151. MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
  152. }
  153. }
  154. void HandleDeleteAsset(string module, string[] args)
  155. {
  156. if (args.Length < 3)
  157. {
  158. MainConsole.Instance.Output("Syntax: delete asset <ID>");
  159. return;
  160. }
  161. AssetBase asset = Get(args[2]);
  162. if (asset == null || asset.Data.Length == 0)
  163. {
  164. MainConsole.Instance.Output("Asset not found");
  165. return;
  166. }
  167. Delete(args[2]);
  168. //MainConsole.Instance.Output("Asset deleted");
  169. // TODO: Implement this
  170. MainConsole.Instance.Output("Asset deletion not supported by database");
  171. }
  172. }
  173. }