AssetService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.DebugFormat("[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.Debug("[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. if (!m_Database.ExistsAsset(asset.FullID))
  142. {
  143. // m_log.DebugFormat(
  144. // "[ASSET SERVICE]: Storing asset {0} {1}, bytes {2}", asset.Name, asset.FullID, asset.Data.Length);
  145. m_Database.StoreAsset(asset);
  146. }
  147. // else
  148. // {
  149. // m_log.DebugFormat(
  150. // "[ASSET SERVICE]: Not storing asset {0} {1}, bytes {2} as it already exists", asset.Name, asset.FullID, asset.Data.Length);
  151. // }
  152. return asset.ID;
  153. }
  154. public bool UpdateContent(string id, byte[] data)
  155. {
  156. return false;
  157. }
  158. public virtual bool Delete(string id)
  159. {
  160. m_log.DebugFormat("[ASSET SERVICE]: Deleting asset {0}", id);
  161. UUID assetID;
  162. if (!UUID.TryParse(id, out assetID))
  163. return false;
  164. AssetBase asset = m_Database.GetAsset(assetID);
  165. if (asset == null)
  166. return false;
  167. if ((int)(asset.Flags & AssetFlags.Maptile) != 0)
  168. {
  169. return m_Database.Delete(id);
  170. }
  171. else
  172. m_log.DebugFormat("[ASSET SERVICE]: Request to delete asset {0}, but flags are not Maptile", id);
  173. return false;
  174. }
  175. void HandleDumpAsset(string module, string[] args)
  176. {
  177. if (args.Length < 3)
  178. {
  179. MainConsole.Instance.Output("Usage is dump asset <ID>");
  180. return;
  181. }
  182. string rawAssetId = args[2];
  183. UUID assetId;
  184. if (!UUID.TryParse(rawAssetId, out assetId))
  185. {
  186. MainConsole.Instance.OutputFormat("ERROR: {0} is not a valid ID format", rawAssetId);
  187. return;
  188. }
  189. AssetBase asset = m_Database.GetAsset(assetId);
  190. if (asset == null)
  191. {
  192. MainConsole.Instance.OutputFormat("ERROR: No asset found with ID {0}", assetId);
  193. return;
  194. }
  195. string fileName = rawAssetId;
  196. using (FileStream fs = new FileStream(fileName, FileMode.CreateNew))
  197. {
  198. using (BinaryWriter bw = new BinaryWriter(fs))
  199. {
  200. bw.Write(asset.Data);
  201. }
  202. }
  203. MainConsole.Instance.OutputFormat("Asset dumped to file {0}", fileName);
  204. }
  205. void HandleShowDigest(string module, string[] args)
  206. {
  207. if (args.Length < 3)
  208. {
  209. MainConsole.Instance.Output("Syntax: show digest <ID>");
  210. return;
  211. }
  212. AssetBase asset = Get(args[2]);
  213. if (asset == null || asset.Data.Length == 0)
  214. {
  215. MainConsole.Instance.Output("Asset not found");
  216. return;
  217. }
  218. int i;
  219. MainConsole.Instance.OutputFormat("Name: {0}", asset.Name);
  220. MainConsole.Instance.OutputFormat("Description: {0}", asset.Description);
  221. MainConsole.Instance.OutputFormat("Type: {0} (type number = {1})", (AssetType)asset.Type, asset.Type);
  222. MainConsole.Instance.OutputFormat("Content-type: {0}", asset.Metadata.ContentType);
  223. MainConsole.Instance.OutputFormat("Flags: {0}", asset.Metadata.Flags);
  224. for (i = 0 ; i < 5 ; i++)
  225. {
  226. int off = i * 16;
  227. if (asset.Data.Length <= off)
  228. break;
  229. int len = 16;
  230. if (asset.Data.Length < off + len)
  231. len = asset.Data.Length - off;
  232. byte[] line = new byte[len];
  233. Array.Copy(asset.Data, off, line, 0, len);
  234. string text = BitConverter.ToString(line);
  235. MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
  236. }
  237. }
  238. void HandleDeleteAsset(string module, string[] args)
  239. {
  240. if (args.Length < 3)
  241. {
  242. MainConsole.Instance.Output("Syntax: delete asset <ID>");
  243. return;
  244. }
  245. AssetBase asset = Get(args[2]);
  246. if (asset == null || asset.Data.Length == 0)
  247. {
  248. MainConsole.Instance.Output("Asset not found");
  249. return;
  250. }
  251. Delete(args[2]);
  252. //MainConsole.Instance.Output("Asset deleted");
  253. // TODO: Implement this
  254. MainConsole.Instance.Output("Asset deletion not supported by database");
  255. }
  256. }
  257. }