AssetServerConnector.cs 8.7 KB

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