clockwerk-opensim-stable – Blame information for rev 1

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