1
0

AssetServerConnector.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.IO;
  29. using Nini.Config;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Console;
  33. using OpenSim.Server.Base;
  34. using OpenSim.Services.Interfaces;
  35. using OpenSim.Framework.Servers.HttpServer;
  36. using OpenSim.Server.Handlers.Base;
  37. namespace OpenSim.Server.Handlers.Asset
  38. {
  39. public class AssetServiceConnector : ServiceConnector
  40. {
  41. private IAssetService m_AssetService;
  42. private string m_ConfigName = "AssetService";
  43. public AssetServiceConnector(IConfigSource config, IHttpServer server, string configName) :
  44. base(config, server, configName)
  45. {
  46. if (configName != String.Empty)
  47. m_ConfigName = configName;
  48. IConfig serverConfig = config.Configs[m_ConfigName];
  49. if (serverConfig == null)
  50. throw new Exception(String.Format("No section '{0}' in config file", m_ConfigName));
  51. string assetService = serverConfig.GetString("LocalServiceModule",
  52. String.Empty);
  53. if (assetService == String.Empty)
  54. throw new Exception("No LocalServiceModule in config file");
  55. Object[] args = new Object[] { config, m_ConfigName };
  56. m_AssetService =
  57. ServerUtils.LoadPlugin<IAssetService>(assetService, args);
  58. if (m_AssetService == null)
  59. throw new Exception(String.Format("Failed to load AssetService from {0}; config is {1}", assetService, m_ConfigName));
  60. bool allowDelete = serverConfig.GetBoolean("AllowRemoteDelete", false);
  61. bool allowDeleteAllTypes = serverConfig.GetBoolean("AllowRemoteDeleteAllTypes", false);
  62. AllowedRemoteDeleteTypes allowedRemoteDeleteTypes;
  63. if (!allowDelete)
  64. {
  65. allowedRemoteDeleteTypes = AllowedRemoteDeleteTypes.None;
  66. }
  67. else
  68. {
  69. if (allowDeleteAllTypes)
  70. allowedRemoteDeleteTypes = AllowedRemoteDeleteTypes.All;
  71. else
  72. allowedRemoteDeleteTypes = AllowedRemoteDeleteTypes.MapTile;
  73. }
  74. server.AddStreamHandler(new AssetServerGetHandler(m_AssetService));
  75. server.AddStreamHandler(new AssetServerPostHandler(m_AssetService));
  76. server.AddStreamHandler(new AssetServerDeleteHandler(m_AssetService, allowedRemoteDeleteTypes));
  77. MainConsole.Instance.Commands.AddCommand("Assets", false,
  78. "show asset",
  79. "show asset <ID>",
  80. "Show asset information",
  81. HandleShowAsset);
  82. MainConsole.Instance.Commands.AddCommand("Assets", false,
  83. "delete asset",
  84. "delete asset <ID>",
  85. "Delete asset from database",
  86. HandleDeleteAsset);
  87. MainConsole.Instance.Commands.AddCommand("Assets", false,
  88. "dump asset",
  89. "dump asset <ID>",
  90. "Dump asset to a file",
  91. "The filename is the same as the ID given.",
  92. HandleDumpAsset);
  93. }
  94. void HandleDeleteAsset(string module, string[] args)
  95. {
  96. if (args.Length < 3)
  97. {
  98. MainConsole.Instance.Output("Syntax: delete asset <ID>");
  99. return;
  100. }
  101. AssetBase asset = m_AssetService.Get(args[2]);
  102. if (asset == null || asset.Data.Length == 0)
  103. {
  104. MainConsole.Instance.OutputFormat("Could not find asset with ID {0}", args[2]);
  105. return;
  106. }
  107. if (!m_AssetService.Delete(asset.ID))
  108. MainConsole.Instance.OutputFormat("ERROR: Could not delete asset {0} {1}", asset.ID, asset.Name);
  109. else
  110. MainConsole.Instance.OutputFormat("Deleted asset {0} {1}", asset.ID, asset.Name);
  111. }
  112. void HandleDumpAsset(string module, string[] args)
  113. {
  114. if (args.Length < 3)
  115. {
  116. MainConsole.Instance.Output("Usage is dump asset <ID>");
  117. return;
  118. }
  119. UUID assetId;
  120. string rawAssetId = args[2];
  121. if (!UUID.TryParse(rawAssetId, out assetId))
  122. {
  123. MainConsole.Instance.OutputFormat("ERROR: {0} is not a valid ID format", rawAssetId);
  124. return;
  125. }
  126. AssetBase asset = m_AssetService.Get(assetId.ToString());
  127. if (asset == null)
  128. {
  129. MainConsole.Instance.OutputFormat("ERROR: No asset found with ID {0}", assetId);
  130. return;
  131. }
  132. string fileName = rawAssetId;
  133. if (!ConsoleUtil.CheckFileDoesNotExist(MainConsole.Instance, fileName))
  134. return;
  135. using (FileStream fs = new FileStream(fileName, FileMode.CreateNew))
  136. {
  137. using (BinaryWriter bw = new BinaryWriter(fs))
  138. {
  139. bw.Write(asset.Data);
  140. }
  141. }
  142. MainConsole.Instance.OutputFormat("Asset dumped to file {0}", fileName);
  143. }
  144. void HandleShowAsset(string module, string[] args)
  145. {
  146. if (args.Length < 3)
  147. {
  148. MainConsole.Instance.Output("Syntax: show asset <ID>");
  149. return;
  150. }
  151. AssetBase asset = m_AssetService.Get(args[2]);
  152. if (asset == null || asset.Data.Length == 0)
  153. {
  154. MainConsole.Instance.Output("Asset not found");
  155. return;
  156. }
  157. int i;
  158. MainConsole.Instance.OutputFormat("Name: {0}", asset.Name);
  159. MainConsole.Instance.OutputFormat("Description: {0}", asset.Description);
  160. MainConsole.Instance.OutputFormat("Type: {0} (type number = {1})", (AssetType)asset.Type, asset.Type);
  161. MainConsole.Instance.OutputFormat("Content-type: {0}", asset.Metadata.ContentType);
  162. MainConsole.Instance.OutputFormat("Size: {0} bytes", asset.Data.Length);
  163. MainConsole.Instance.OutputFormat("Temporary: {0}", asset.Temporary ? "yes" : "no");
  164. MainConsole.Instance.OutputFormat("Flags: {0}", asset.Metadata.Flags);
  165. for (i = 0 ; i < 5 ; i++)
  166. {
  167. int off = i * 16;
  168. if (asset.Data.Length <= off)
  169. break;
  170. int len = 16;
  171. if (asset.Data.Length < off + len)
  172. len = asset.Data.Length - off;
  173. byte[] line = new byte[len];
  174. Array.Copy(asset.Data, off, line, 0, len);
  175. string text = BitConverter.ToString(line);
  176. MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
  177. }
  178. }
  179. }
  180. }