1
0

AssetService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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(
  77. loaderArgs,
  78. delegate(AssetBase a)
  79. {
  80. AssetBase existingAsset = Get(a.ID);
  81. // AssetMetadata existingMetadata = GetMetadata(a.ID);
  82. if (existingAsset == null || Util.SHA1Hash(existingAsset.Data) != Util.SHA1Hash(a.Data))
  83. {
  84. // m_log.DebugFormat("[ASSET]: Storing {0} {1}", a.Name, a.ID);
  85. Store(a);
  86. }
  87. });
  88. }
  89. m_log.Info("[ASSET SERVICE]: Local asset service enabled");
  90. }
  91. }
  92. }
  93. public virtual AssetBase Get(string id)
  94. {
  95. // m_log.DebugFormat("[ASSET SERVICE]: Get asset for {0}", id);
  96. UUID assetID;
  97. if (!UUID.TryParse(id, out assetID))
  98. {
  99. m_log.WarnFormat("[ASSET SERVICE]: Could not parse requested asset id {0}", id);
  100. return null;
  101. }
  102. return m_Database.GetAsset(assetID);
  103. }
  104. public virtual AssetBase GetCached(string id)
  105. {
  106. return Get(id);
  107. }
  108. public virtual AssetMetadata GetMetadata(string id)
  109. {
  110. // m_log.DebugFormat("[ASSET SERVICE]: Get asset metadata for {0}", id);
  111. UUID assetID;
  112. if (!UUID.TryParse(id, out assetID))
  113. return null;
  114. AssetBase asset = m_Database.GetAsset(assetID);
  115. if (asset != null)
  116. return asset.Metadata;
  117. return null;
  118. }
  119. public virtual byte[] GetData(string id)
  120. {
  121. // m_log.DebugFormat("[ASSET SERVICE]: Get asset data for {0}", id);
  122. UUID assetID;
  123. if (!UUID.TryParse(id, out assetID))
  124. return null;
  125. AssetBase asset = m_Database.GetAsset(assetID);
  126. return asset.Data;
  127. }
  128. public virtual bool Get(string id, Object sender, AssetRetrieved handler)
  129. {
  130. //m_log.DebugFormat("[AssetService]: Get asset async {0}", id);
  131. UUID assetID;
  132. if (!UUID.TryParse(id, out assetID))
  133. return false;
  134. AssetBase asset = m_Database.GetAsset(assetID);
  135. //m_log.DebugFormat("[AssetService]: Got asset {0}", asset);
  136. handler(id, sender, asset);
  137. return true;
  138. }
  139. public virtual string Store(AssetBase asset)
  140. {
  141. // m_log.DebugFormat(
  142. // "[ASSET SERVICE]: Storing asset {0} {1}, bytes {2}", asset.Name, asset.ID, asset.Data.Length);
  143. m_Database.StoreAsset(asset);
  144. return asset.ID;
  145. }
  146. public bool UpdateContent(string id, byte[] data)
  147. {
  148. return false;
  149. }
  150. public virtual bool Delete(string id)
  151. {
  152. m_log.DebugFormat("[ASSET SERVICE]: Deleting asset {0}", id);
  153. UUID assetID;
  154. if (!UUID.TryParse(id, out assetID))
  155. return false;
  156. AssetBase asset = m_Database.GetAsset(assetID);
  157. if (asset == null)
  158. return false;
  159. if ((int)(asset.Flags & AssetFlags.Maptile) != 0)
  160. {
  161. return m_Database.Delete(id);
  162. }
  163. else
  164. m_log.DebugFormat("[ASSET SERVICE]: Request to delete asset {0}, but flags are not Maptile", id);
  165. return false;
  166. }
  167. void HandleDumpAsset(string module, string[] args)
  168. {
  169. if (args.Length < 3)
  170. {
  171. MainConsole.Instance.Output("Usage is dump asset <ID>");
  172. return;
  173. }
  174. string rawAssetId = args[2];
  175. UUID assetId;
  176. if (!UUID.TryParse(rawAssetId, out assetId))
  177. {
  178. MainConsole.Instance.OutputFormat("ERROR: {0} is not a valid ID format", rawAssetId);
  179. return;
  180. }
  181. AssetBase asset = m_Database.GetAsset(assetId);
  182. if (asset == null)
  183. {
  184. MainConsole.Instance.OutputFormat("ERROR: No asset found with ID {0}", assetId);
  185. return;
  186. }
  187. string fileName = rawAssetId;
  188. using (FileStream fs = new FileStream(fileName, FileMode.CreateNew))
  189. {
  190. using (BinaryWriter bw = new BinaryWriter(fs))
  191. {
  192. bw.Write(asset.Data);
  193. }
  194. }
  195. MainConsole.Instance.OutputFormat("Asset dumped to file {0}", fileName);
  196. }
  197. void HandleShowDigest(string module, string[] args)
  198. {
  199. if (args.Length < 3)
  200. {
  201. MainConsole.Instance.Output("Syntax: show digest <ID>");
  202. return;
  203. }
  204. AssetBase asset = Get(args[2]);
  205. if (asset == null || asset.Data.Length == 0)
  206. {
  207. MainConsole.Instance.Output("Asset not found");
  208. return;
  209. }
  210. int i;
  211. MainConsole.Instance.OutputFormat("Name: {0}", asset.Name);
  212. MainConsole.Instance.OutputFormat("Description: {0}", asset.Description);
  213. MainConsole.Instance.OutputFormat("Type: {0} (type number = {1})", (AssetType)asset.Type, asset.Type);
  214. MainConsole.Instance.OutputFormat("Content-type: {0}", asset.Metadata.ContentType);
  215. MainConsole.Instance.OutputFormat("Flags: {0}", asset.Metadata.Flags);
  216. for (i = 0 ; i < 5 ; i++)
  217. {
  218. int off = i * 16;
  219. if (asset.Data.Length <= off)
  220. break;
  221. int len = 16;
  222. if (asset.Data.Length < off + len)
  223. len = asset.Data.Length - off;
  224. byte[] line = new byte[len];
  225. Array.Copy(asset.Data, off, line, 0, len);
  226. string text = BitConverter.ToString(line);
  227. MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
  228. }
  229. }
  230. void HandleDeleteAsset(string module, string[] args)
  231. {
  232. if (args.Length < 3)
  233. {
  234. MainConsole.Instance.Output("Syntax: delete asset <ID>");
  235. return;
  236. }
  237. AssetBase asset = Get(args[2]);
  238. if (asset == null || asset.Data.Length == 0)
  239. {
  240. MainConsole.Instance.Output("Asset not found");
  241. return;
  242. }
  243. Delete(args[2]);
  244. //MainConsole.Instance.Output("Asset deleted");
  245. // TODO: Implement this
  246. MainConsole.Instance.Output("Asset deletion not supported by database");
  247. }
  248. }
  249. }