AssetService.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 SERVICE]: Local asset service enabled");
  71. }
  72. }
  73. public AssetBase Get(string id)
  74. {
  75. UUID assetID;
  76. if (!UUID.TryParse(id, out assetID))
  77. {
  78. m_log.WarnFormat("[ASSET SERVICE]: Could not parse requested sset id {0}", id);
  79. return null;
  80. }
  81. return m_Database.GetAsset(assetID);
  82. }
  83. public AssetBase GetCached(string id)
  84. {
  85. return Get(id);
  86. }
  87. public AssetMetadata GetMetadata(string id)
  88. {
  89. UUID assetID;
  90. if (!UUID.TryParse(id, out assetID))
  91. return null;
  92. AssetBase asset = m_Database.GetAsset(assetID);
  93. if (asset != null)
  94. return asset.Metadata;
  95. return null;
  96. }
  97. public byte[] GetData(string id)
  98. {
  99. UUID assetID;
  100. if (!UUID.TryParse(id, out assetID))
  101. return null;
  102. AssetBase asset = m_Database.GetAsset(assetID);
  103. return asset.Data;
  104. }
  105. public bool Get(string id, Object sender, AssetRetrieved handler)
  106. {
  107. //m_log.DebugFormat("[AssetService]: Get asset async {0}", id);
  108. UUID assetID;
  109. if (!UUID.TryParse(id, out assetID))
  110. return false;
  111. AssetBase asset = m_Database.GetAsset(assetID);
  112. //m_log.DebugFormat("[AssetService]: Got asset {0}", asset);
  113. handler(id, sender, asset);
  114. return true;
  115. }
  116. public string Store(AssetBase asset)
  117. {
  118. //m_log.DebugFormat("[ASSET SERVICE]: Store asset {0} {1}", asset.Name, asset.ID);
  119. m_Database.StoreAsset(asset);
  120. return asset.ID;
  121. }
  122. public bool UpdateContent(string id, byte[] data)
  123. {
  124. return false;
  125. }
  126. public bool Delete(string id)
  127. {
  128. m_log.DebugFormat("[ASSET SERVICE]: Deleting asset {0}", id);
  129. UUID assetID;
  130. if (!UUID.TryParse(id, out assetID))
  131. return false;
  132. AssetBase asset = m_Database.GetAsset(assetID);
  133. if (asset == null)
  134. return false;
  135. if ((int)(asset.Flags & AssetFlags.Maptile) != 0)
  136. {
  137. return m_Database.Delete(id);
  138. }
  139. else
  140. m_log.DebugFormat("[ASSET SERVICE]: Request to delete asset {0}, but flags are not Maptile", id);
  141. return false;
  142. }
  143. void HandleShowDigest(string module, string[] args)
  144. {
  145. if (args.Length < 3)
  146. {
  147. MainConsole.Instance.Output("Syntax: show digest <ID>");
  148. return;
  149. }
  150. AssetBase asset = Get(args[2]);
  151. if (asset == null || asset.Data.Length == 0)
  152. {
  153. MainConsole.Instance.Output("Asset not found");
  154. return;
  155. }
  156. int i;
  157. MainConsole.Instance.Output(String.Format("Name: {0}", asset.Name));
  158. MainConsole.Instance.Output(String.Format("Description: {0}", asset.Description));
  159. MainConsole.Instance.Output(String.Format("Type: {0}", asset.Type));
  160. MainConsole.Instance.Output(String.Format("Content-type: {0}", asset.Metadata.ContentType));
  161. MainConsole.Instance.Output(String.Format("Flags: {0}", asset.Metadata.Flags.ToString()));
  162. for (i = 0 ; i < 5 ; i++)
  163. {
  164. int off = i * 16;
  165. if (asset.Data.Length <= off)
  166. break;
  167. int len = 16;
  168. if (asset.Data.Length < off + len)
  169. len = asset.Data.Length - off;
  170. byte[] line = new byte[len];
  171. Array.Copy(asset.Data, off, line, 0, len);
  172. string text = BitConverter.ToString(line);
  173. MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
  174. }
  175. }
  176. void HandleDeleteAsset(string module, string[] args)
  177. {
  178. if (args.Length < 3)
  179. {
  180. MainConsole.Instance.Output("Syntax: delete asset <ID>");
  181. return;
  182. }
  183. AssetBase asset = Get(args[2]);
  184. if (asset == null || asset.Data.Length == 0)
  185. {
  186. MainConsole.Instance.Output("Asset not found");
  187. return;
  188. }
  189. Delete(args[2]);
  190. //MainConsole.Instance.Output("Asset deleted");
  191. // TODO: Implement this
  192. MainConsole.Instance.Output("Asset deletion not supported by database");
  193. }
  194. }
  195. }