1
0

AssetServerConnector.cs 8.6 KB

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