OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
AssetServerConnector.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.IO;
30 using Nini.Config;
31 using OpenMetaverse;
32 using OpenSim.Framework;
33 using OpenSim.Framework.ServiceAuth;
34 using OpenSim.Framework.Console;
35 using OpenSim.Server.Base;
36 using OpenSim.Services.Interfaces;
37 using OpenSim.Framework.Servers.HttpServer;
38 using OpenSim.Server.Handlers.Base;
39 
40 namespace OpenSim.Server.Handlers.Asset
41 {
43  {
44  private IAssetService m_AssetService;
45  private string m_ConfigName = "AssetService";
46 
47  public AssetServiceConnector(IConfigSource config, IHttpServer server, string configName) :
48  base(config, server, configName)
49  {
50  if (configName != String.Empty)
51  m_ConfigName = configName;
52 
53  IConfig serverConfig = config.Configs[m_ConfigName];
54  if (serverConfig == null)
55  throw new Exception(String.Format("No section '{0}' in config file", m_ConfigName));
56 
57  string assetService = serverConfig.GetString("LocalServiceModule",
58  String.Empty);
59 
60  if (assetService == String.Empty)
61  throw new Exception("No LocalServiceModule in config file");
62 
63  Object[] args = new Object[] { config, m_ConfigName };
64  m_AssetService =
65  ServerUtils.LoadPlugin<IAssetService>(assetService, args);
66 
67  if (m_AssetService == null)
68  throw new Exception(String.Format("Failed to load AssetService from {0}; config is {1}", assetService, m_ConfigName));
69 
70  bool allowDelete = serverConfig.GetBoolean("AllowRemoteDelete", false);
71  bool allowDeleteAllTypes = serverConfig.GetBoolean("AllowRemoteDeleteAllTypes", false);
72 
73  string redirectURL = serverConfig.GetString("RedirectURL", string.Empty);
74 
75  AllowedRemoteDeleteTypes allowedRemoteDeleteTypes;
76 
77  if (!allowDelete)
78  {
79  allowedRemoteDeleteTypes = AllowedRemoteDeleteTypes.None;
80  }
81  else
82  {
83  if (allowDeleteAllTypes)
84  allowedRemoteDeleteTypes = AllowedRemoteDeleteTypes.All;
85  else
86  allowedRemoteDeleteTypes = AllowedRemoteDeleteTypes.MapTile;
87  }
88 
89  IServiceAuth auth = ServiceAuth.Create(config, m_ConfigName);
90 
91  server.AddStreamHandler(new AssetServerGetHandler(m_AssetService, auth, redirectURL));
92  server.AddStreamHandler(new AssetServerPostHandler(m_AssetService, auth));
93  server.AddStreamHandler(new AssetServerDeleteHandler(m_AssetService, allowedRemoteDeleteTypes, auth));
94  server.AddStreamHandler(new AssetsExistHandler(m_AssetService));
95 
96  MainConsole.Instance.Commands.AddCommand("Assets", false,
97  "show asset",
98  "show asset <ID>",
99  "Show asset information",
100  HandleShowAsset);
101 
102  MainConsole.Instance.Commands.AddCommand("Assets", false,
103  "delete asset",
104  "delete asset <ID>",
105  "Delete asset from database",
106  HandleDeleteAsset);
107 
108  MainConsole.Instance.Commands.AddCommand("Assets", false,
109  "dump asset",
110  "dump asset <ID>",
111  "Dump asset to a file",
112  "The filename is the same as the ID given.",
113  HandleDumpAsset);
114  }
115 
116  void HandleDeleteAsset(string module, string[] args)
117  {
118  if (args.Length < 3)
119  {
120  MainConsole.Instance.Output("Syntax: delete asset <ID>");
121  return;
122  }
123 
124  AssetBase asset = m_AssetService.Get(args[2]);
125 
126  if (asset == null || asset.Data.Length == 0)
127  {
128  MainConsole.Instance.OutputFormat("Could not find asset with ID {0}", args[2]);
129  return;
130  }
131 
132  if (!m_AssetService.Delete(asset.ID))
133  MainConsole.Instance.OutputFormat("ERROR: Could not delete asset {0} {1}", asset.ID, asset.Name);
134  else
135  MainConsole.Instance.OutputFormat("Deleted asset {0} {1}", asset.ID, asset.Name);
136  }
137 
138  void HandleDumpAsset(string module, string[] args)
139  {
140  if (args.Length < 3)
141  {
142  MainConsole.Instance.Output("Usage is dump asset <ID>");
143  return;
144  }
145 
146  UUID assetId;
147  string rawAssetId = args[2];
148 
149  if (!UUID.TryParse(rawAssetId, out assetId))
150  {
151  MainConsole.Instance.OutputFormat("ERROR: {0} is not a valid ID format", rawAssetId);
152  return;
153  }
154 
155  AssetBase asset = m_AssetService.Get(assetId.ToString());
156  if (asset == null)
157  {
158  MainConsole.Instance.OutputFormat("ERROR: No asset found with ID {0}", assetId);
159  return;
160  }
161 
162  string fileName = rawAssetId;
163 
165  return;
166 
167  using (FileStream fs = new FileStream(fileName, FileMode.CreateNew))
168  {
169  using (BinaryWriter bw = new BinaryWriter(fs))
170  {
171  bw.Write(asset.Data);
172  }
173  }
174 
175  MainConsole.Instance.OutputFormat("Asset dumped to file {0}", fileName);
176  }
177 
178  void HandleShowAsset(string module, string[] args)
179  {
180  if (args.Length < 3)
181  {
182  MainConsole.Instance.Output("Syntax: show asset <ID>");
183  return;
184  }
185 
186  AssetBase asset = m_AssetService.Get(args[2]);
187 
188  if (asset == null || asset.Data.Length == 0)
189  {
190  MainConsole.Instance.Output("Asset not found");
191  return;
192  }
193 
194  int i;
195 
196  MainConsole.Instance.OutputFormat("Name: {0}", asset.Name);
197  MainConsole.Instance.OutputFormat("Description: {0}", asset.Description);
198  MainConsole.Instance.OutputFormat("Type: {0} (type number = {1})", (AssetType)asset.Type, asset.Type);
199  MainConsole.Instance.OutputFormat("Content-type: {0}", asset.Metadata.ContentType);
200  MainConsole.Instance.OutputFormat("Size: {0} bytes", asset.Data.Length);
201  MainConsole.Instance.OutputFormat("Temporary: {0}", asset.Temporary ? "yes" : "no");
202  MainConsole.Instance.OutputFormat("Flags: {0}", asset.Metadata.Flags);
203 
204  for (i = 0 ; i < 5 ; i++)
205  {
206  int off = i * 16;
207  if (asset.Data.Length <= off)
208  break;
209  int len = 16;
210  if (asset.Data.Length < off + len)
211  len = asset.Data.Length - off;
212 
213  byte[] line = new byte[len];
214  Array.Copy(asset.Data, off, line, 0, len);
215 
216  string text = BitConverter.ToString(line);
217  MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
218  }
219  }
220  }
221 }
void OutputFormat(string format, params object[] components)
AllowedRemoteDeleteTypes
Remote deletes allowed.
sbyte Type
(sbyte) AssetType enum
Definition: AssetBase.cs:198
Interface to OpenSimulator's built in HTTP server. Use this to register handlers (http, llsd, xmlrpc, etc.) for given URLs.
Definition: IHttpServer.cs:36
AssetServiceConnector(IConfigSource config, IHttpServer server, string configName)
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
Non-texture assets
static ICommandConsole Instance
Definition: MainConsole.cs:35
Interactive OpenSim region server
Definition: OpenSim.cs:55
static bool CheckFileDoesNotExist(ICommandConsole console, string path)
Check if the given file path exists.
Definition: ConsoleUtil.cs:74
string ID
Asset MetaData ID (transferring from UUID to string ID)
Definition: AssetBase.cs:177