AssetService.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. protected static AssetService m_RootInstance;
  44. public AssetService(IConfigSource config) : base(config)
  45. {
  46. if (m_RootInstance == null)
  47. {
  48. m_RootInstance = this;
  49. MainConsole.Instance.Commands.AddCommand("kfs", false,
  50. "show digest",
  51. "show digest <ID>",
  52. "Show asset digest", HandleShowDigest);
  53. MainConsole.Instance.Commands.AddCommand("kfs", false,
  54. "delete asset",
  55. "delete asset <ID>",
  56. "Delete asset from database", HandleDeleteAsset);
  57. if (m_AssetLoader != null)
  58. {
  59. IConfig assetConfig = config.Configs["AssetService"];
  60. if (assetConfig == null)
  61. throw new Exception("No AssetService configuration");
  62. string loaderArgs = assetConfig.GetString("AssetLoaderArgs",
  63. String.Empty);
  64. bool assetLoaderEnabled = assetConfig.GetBoolean("AssetLoaderEnabled", true);
  65. if (assetLoaderEnabled)
  66. {
  67. m_log.InfoFormat("[ASSET]: Loading default asset set from {0}", loaderArgs);
  68. m_AssetLoader.ForEachDefaultXmlAsset(loaderArgs,
  69. delegate(AssetBase a)
  70. {
  71. Store(a);
  72. });
  73. }
  74. m_log.Info("[ASSET SERVICE]: Local asset service enabled");
  75. }
  76. }
  77. }
  78. public virtual AssetBase Get(string id)
  79. {
  80. UUID assetID;
  81. if (!UUID.TryParse(id, out assetID))
  82. {
  83. m_log.WarnFormat("[ASSET SERVICE]: Could not parse requested sset id {0}", id);
  84. return null;
  85. }
  86. return m_Database.GetAsset(assetID);
  87. }
  88. public virtual AssetBase GetCached(string id)
  89. {
  90. return Get(id);
  91. }
  92. public virtual AssetMetadata GetMetadata(string id)
  93. {
  94. UUID assetID;
  95. if (!UUID.TryParse(id, out assetID))
  96. return null;
  97. AssetBase asset = m_Database.GetAsset(assetID);
  98. if (asset != null)
  99. return asset.Metadata;
  100. return null;
  101. }
  102. public virtual byte[] GetData(string id)
  103. {
  104. UUID assetID;
  105. if (!UUID.TryParse(id, out assetID))
  106. return null;
  107. AssetBase asset = m_Database.GetAsset(assetID);
  108. return asset.Data;
  109. }
  110. public virtual bool Get(string id, Object sender, AssetRetrieved handler)
  111. {
  112. //m_log.DebugFormat("[AssetService]: Get asset async {0}", id);
  113. UUID assetID;
  114. if (!UUID.TryParse(id, out assetID))
  115. return false;
  116. AssetBase asset = m_Database.GetAsset(assetID);
  117. //m_log.DebugFormat("[AssetService]: Got asset {0}", asset);
  118. handler(id, sender, asset);
  119. return true;
  120. }
  121. public virtual string Store(AssetBase asset)
  122. {
  123. //m_log.DebugFormat("[ASSET SERVICE]: Store asset {0} {1}", asset.Name, asset.ID);
  124. m_Database.StoreAsset(asset);
  125. return asset.ID;
  126. }
  127. public bool UpdateContent(string id, byte[] data)
  128. {
  129. return false;
  130. }
  131. public virtual bool Delete(string id)
  132. {
  133. m_log.DebugFormat("[ASSET SERVICE]: Deleting asset {0}", id);
  134. UUID assetID;
  135. if (!UUID.TryParse(id, out assetID))
  136. return false;
  137. AssetBase asset = m_Database.GetAsset(assetID);
  138. if (asset == null)
  139. return false;
  140. if ((int)(asset.Flags & AssetFlags.Maptile) != 0)
  141. {
  142. return m_Database.Delete(id);
  143. }
  144. else
  145. m_log.DebugFormat("[ASSET SERVICE]: Request to delete asset {0}, but flags are not Maptile", id);
  146. return false;
  147. }
  148. void HandleShowDigest(string module, string[] args)
  149. {
  150. if (args.Length < 3)
  151. {
  152. MainConsole.Instance.Output("Syntax: show digest <ID>");
  153. return;
  154. }
  155. AssetBase asset = Get(args[2]);
  156. if (asset == null || asset.Data.Length == 0)
  157. {
  158. MainConsole.Instance.Output("Asset not found");
  159. return;
  160. }
  161. int i;
  162. MainConsole.Instance.Output(String.Format("Name: {0}", asset.Name));
  163. MainConsole.Instance.Output(String.Format("Description: {0}", asset.Description));
  164. MainConsole.Instance.Output(String.Format("Type: {0}", asset.Type));
  165. MainConsole.Instance.Output(String.Format("Content-type: {0}", asset.Metadata.ContentType));
  166. MainConsole.Instance.Output(String.Format("Flags: {0}", asset.Metadata.Flags.ToString()));
  167. for (i = 0 ; i < 5 ; i++)
  168. {
  169. int off = i * 16;
  170. if (asset.Data.Length <= off)
  171. break;
  172. int len = 16;
  173. if (asset.Data.Length < off + len)
  174. len = asset.Data.Length - off;
  175. byte[] line = new byte[len];
  176. Array.Copy(asset.Data, off, line, 0, len);
  177. string text = BitConverter.ToString(line);
  178. MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
  179. }
  180. }
  181. void HandleDeleteAsset(string module, string[] args)
  182. {
  183. if (args.Length < 3)
  184. {
  185. MainConsole.Instance.Output("Syntax: delete asset <ID>");
  186. return;
  187. }
  188. AssetBase asset = Get(args[2]);
  189. if (asset == null || asset.Data.Length == 0)
  190. {
  191. MainConsole.Instance.Output("Asset not found");
  192. return;
  193. }
  194. Delete(args[2]);
  195. //MainConsole.Instance.Output("Asset deleted");
  196. // TODO: Implement this
  197. MainConsole.Instance.Output("Asset deletion not supported by database");
  198. }
  199. }
  200. }