AssetService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. m_log.InfoFormat("[ASSET]: Loading default asset set from {0}", loaderArgs);
  61. m_AssetLoader.ForEachDefaultXmlAsset(loaderArgs,
  62. delegate(AssetBase a)
  63. {
  64. Store(a);
  65. });
  66. m_log.Info("[ASSET CONNECTOR]: Local asset service enabled");
  67. }
  68. }
  69. public AssetBase Get(string id)
  70. {
  71. //m_log.DebugFormat("[ASSET SERVICE]: Get asset {0}", id);
  72. UUID assetID;
  73. if (!UUID.TryParse(id, out assetID))
  74. return null;
  75. return m_Database.FetchAsset(assetID);
  76. }
  77. public AssetMetadata GetMetadata(string id)
  78. {
  79. UUID assetID;
  80. if (!UUID.TryParse(id, out assetID))
  81. return null;
  82. AssetBase asset = m_Database.FetchAsset(assetID);
  83. return asset.Metadata;
  84. }
  85. public byte[] GetData(string id)
  86. {
  87. UUID assetID;
  88. if (!UUID.TryParse(id, out assetID))
  89. return null;
  90. AssetBase asset = m_Database.FetchAsset(assetID);
  91. return asset.Data;
  92. }
  93. public bool Get(string id, Object sender, AssetRetrieved handler)
  94. {
  95. //m_log.DebugFormat("[AssetService]: Get asset async {0}", id);
  96. UUID assetID;
  97. if (!UUID.TryParse(id, out assetID))
  98. return false;
  99. AssetBase asset = m_Database.FetchAsset(assetID);
  100. //m_log.DebugFormat("[AssetService]: Got asset {0}", asset);
  101. handler(id, sender, asset);
  102. return true;
  103. }
  104. public string Store(AssetBase asset)
  105. {
  106. //m_log.DebugFormat("[ASSET SERVICE]: Store asset {0} {1}", asset.Name, asset.ID);
  107. m_Database.CreateAsset(asset);
  108. return asset.ID;
  109. }
  110. public bool UpdateContent(string id, byte[] data)
  111. {
  112. return false;
  113. }
  114. public bool Delete(string id)
  115. {
  116. return false;
  117. }
  118. void HandleShowDigest(string module, string[] args)
  119. {
  120. if (args.Length < 3)
  121. {
  122. MainConsole.Instance.Output("Syntax: show digest <ID>");
  123. return;
  124. }
  125. AssetBase asset = Get(args[2]);
  126. if (asset == null || asset.Data.Length == 0)
  127. {
  128. MainConsole.Instance.Output("Asset not found");
  129. return;
  130. }
  131. int i;
  132. MainConsole.Instance.Output(String.Format("Name: {0}", asset.Name));
  133. MainConsole.Instance.Output(String.Format("Description: {0}", asset.Description));
  134. MainConsole.Instance.Output(String.Format("Type: {0}", asset.Type));
  135. MainConsole.Instance.Output(String.Format("Content-type: {0}", asset.Metadata.ContentType));
  136. for (i = 0 ; i < 5 ; i++)
  137. {
  138. int off = i * 16;
  139. if (asset.Data.Length <= off)
  140. break;
  141. int len = 16;
  142. if (asset.Data.Length < off + len)
  143. len = asset.Data.Length - off;
  144. byte[] line = new byte[len];
  145. Array.Copy(asset.Data, off, line, 0, len);
  146. string text = BitConverter.ToString(line);
  147. MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
  148. }
  149. }
  150. void HandleDeleteAsset(string module, string[] args)
  151. {
  152. if (args.Length < 3)
  153. {
  154. MainConsole.Instance.Output("Syntax: delete asset <ID>");
  155. return;
  156. }
  157. AssetBase asset = Get(args[2]);
  158. if (asset == null || asset.Data.Length == 0)
  159. {
  160. MainConsole.Instance.Output("Asset not found");
  161. return;
  162. }
  163. Delete(args[2]);
  164. //MainConsole.Instance.Output("Asset deleted");
  165. // TODO: Implement this
  166. MainConsole.Instance.Output("Asset deletion not supported by database");
  167. }
  168. }
  169. }