AssetService.cs 8.7 KB

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