AssetService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using Nini.Config;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Data;
  36. using OpenSim.Services.Interfaces;
  37. using OpenMetaverse;
  38. namespace OpenSim.Services.AssetService
  39. {
  40. public class AssetService : AssetServiceBase, IAssetService
  41. {
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(
  44. MethodBase.GetCurrentMethod().DeclaringType);
  45. protected static AssetService m_RootInstance;
  46. public AssetService(IConfigSource config) : base(config)
  47. {
  48. if (m_RootInstance == null)
  49. {
  50. m_RootInstance = this;
  51. MainConsole.Instance.Commands.AddCommand("kfs", false,
  52. "show digest",
  53. "show digest <ID>",
  54. "Show asset digest", HandleShowDigest);
  55. MainConsole.Instance.Commands.AddCommand("kfs", false,
  56. "delete asset",
  57. "delete asset <ID>",
  58. "Delete asset from database", HandleDeleteAsset);
  59. MainConsole.Instance.Commands.AddCommand("kfs", false,
  60. "dump asset",
  61. "dump asset <ID>",
  62. "Dump asset to a file",
  63. "The filename is the same as the ID given.",
  64. HandleDumpAsset);
  65. if (m_AssetLoader != null)
  66. {
  67. IConfig assetConfig = config.Configs["AssetService"];
  68. if (assetConfig == null)
  69. throw new Exception("No AssetService configuration");
  70. string loaderArgs = assetConfig.GetString("AssetLoaderArgs",
  71. String.Empty);
  72. bool assetLoaderEnabled = assetConfig.GetBoolean("AssetLoaderEnabled", true);
  73. if (assetLoaderEnabled)
  74. {
  75. m_log.InfoFormat("[ASSET]: Loading default asset set from {0}", loaderArgs);
  76. m_AssetLoader.ForEachDefaultXmlAsset(loaderArgs,
  77. delegate(AssetBase a)
  78. {
  79. Store(a);
  80. });
  81. }
  82. m_log.Info("[ASSET SERVICE]: Local asset service enabled");
  83. }
  84. }
  85. }
  86. public virtual AssetBase Get(string id)
  87. {
  88. // m_log.DebugFormat("[ASSET SERVICE]: Get asset for {0}", id);
  89. UUID assetID;
  90. if (!UUID.TryParse(id, out assetID))
  91. {
  92. m_log.WarnFormat("[ASSET SERVICE]: Could not parse requested asset id {0}", id);
  93. return null;
  94. }
  95. return m_Database.GetAsset(assetID);
  96. }
  97. public virtual AssetBase GetCached(string id)
  98. {
  99. return Get(id);
  100. }
  101. public virtual AssetMetadata GetMetadata(string id)
  102. {
  103. // m_log.DebugFormat("[ASSET SERVICE]: Get asset metadata for {0}", id);
  104. UUID assetID;
  105. if (!UUID.TryParse(id, out assetID))
  106. return null;
  107. AssetBase asset = m_Database.GetAsset(assetID);
  108. if (asset != null)
  109. return asset.Metadata;
  110. return null;
  111. }
  112. public virtual byte[] GetData(string id)
  113. {
  114. // m_log.DebugFormat("[ASSET SERVICE]: Get asset data for {0}", id);
  115. UUID assetID;
  116. if (!UUID.TryParse(id, out assetID))
  117. return null;
  118. AssetBase asset = m_Database.GetAsset(assetID);
  119. return asset.Data;
  120. }
  121. public virtual bool Get(string id, Object sender, AssetRetrieved handler)
  122. {
  123. //m_log.DebugFormat("[AssetService]: Get asset async {0}", id);
  124. UUID assetID;
  125. if (!UUID.TryParse(id, out assetID))
  126. return false;
  127. AssetBase asset = m_Database.GetAsset(assetID);
  128. //m_log.DebugFormat("[AssetService]: Got asset {0}", asset);
  129. handler(id, sender, asset);
  130. return true;
  131. }
  132. public virtual string Store(AssetBase asset)
  133. {
  134. // m_log.DebugFormat(
  135. // "[ASSET SERVICE]: Storing asset {0} {1}, bytes {2}", asset.Name, asset.ID, asset.Data.Length);
  136. m_Database.StoreAsset(asset);
  137. return asset.ID;
  138. }
  139. public bool UpdateContent(string id, byte[] data)
  140. {
  141. return false;
  142. }
  143. public virtual bool Delete(string id)
  144. {
  145. m_log.DebugFormat("[ASSET SERVICE]: Deleting asset {0}", id);
  146. UUID assetID;
  147. if (!UUID.TryParse(id, out assetID))
  148. return false;
  149. AssetBase asset = m_Database.GetAsset(assetID);
  150. if (asset == null)
  151. return false;
  152. if ((int)(asset.Flags & AssetFlags.Maptile) != 0)
  153. {
  154. return m_Database.Delete(id);
  155. }
  156. else
  157. m_log.DebugFormat("[ASSET SERVICE]: Request to delete asset {0}, but flags are not Maptile", id);
  158. return false;
  159. }
  160. void HandleDumpAsset(string module, string[] args)
  161. {
  162. if (args.Length < 3)
  163. {
  164. MainConsole.Instance.Output("Usage is dump asset <ID>");
  165. return;
  166. }
  167. string rawAssetId = args[2];
  168. UUID assetId;
  169. if (!UUID.TryParse(rawAssetId, out assetId))
  170. {
  171. MainConsole.Instance.OutputFormat("ERROR: {0} is not a valid ID format", rawAssetId);
  172. return;
  173. }
  174. AssetBase asset = m_Database.GetAsset(assetId);
  175. if (asset == null)
  176. {
  177. MainConsole.Instance.OutputFormat("ERROR: No asset found with ID {0}", assetId);
  178. return;
  179. }
  180. string fileName = rawAssetId;
  181. using (FileStream fs = new FileStream(fileName, FileMode.CreateNew))
  182. {
  183. using (BinaryWriter bw = new BinaryWriter(fs))
  184. {
  185. bw.Write(asset.Data);
  186. }
  187. }
  188. MainConsole.Instance.OutputFormat("Asset dumped to file {0}", fileName);
  189. }
  190. void HandleShowDigest(string module, string[] args)
  191. {
  192. if (args.Length < 3)
  193. {
  194. MainConsole.Instance.Output("Syntax: show digest <ID>");
  195. return;
  196. }
  197. AssetBase asset = Get(args[2]);
  198. if (asset == null || asset.Data.Length == 0)
  199. {
  200. MainConsole.Instance.Output("Asset not found");
  201. return;
  202. }
  203. int i;
  204. MainConsole.Instance.OutputFormat("Name: {0}", asset.Name);
  205. MainConsole.Instance.OutputFormat("Description: {0}", asset.Description);
  206. MainConsole.Instance.OutputFormat("Type: {0} (type number = {1})", (AssetType)asset.Type, asset.Type);
  207. MainConsole.Instance.OutputFormat("Content-type: {0}", asset.Metadata.ContentType);
  208. MainConsole.Instance.OutputFormat("Flags: {0}", asset.Metadata.Flags);
  209. for (i = 0 ; i < 5 ; i++)
  210. {
  211. int off = i * 16;
  212. if (asset.Data.Length <= off)
  213. break;
  214. int len = 16;
  215. if (asset.Data.Length < off + len)
  216. len = asset.Data.Length - off;
  217. byte[] line = new byte[len];
  218. Array.Copy(asset.Data, off, line, 0, len);
  219. string text = BitConverter.ToString(line);
  220. MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
  221. }
  222. }
  223. void HandleDeleteAsset(string module, string[] args)
  224. {
  225. if (args.Length < 3)
  226. {
  227. MainConsole.Instance.Output("Syntax: delete asset <ID>");
  228. return;
  229. }
  230. AssetBase asset = Get(args[2]);
  231. if (asset == null || asset.Data.Length == 0)
  232. {
  233. MainConsole.Instance.Output("Asset not found");
  234. return;
  235. }
  236. Delete(args[2]);
  237. //MainConsole.Instance.Output("Asset deleted");
  238. // TODO: Implement this
  239. MainConsole.Instance.Output("Asset deletion not supported by database");
  240. }
  241. }
  242. }