OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
XBakes.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.Diagnostics;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Text;
33 using System.Threading;
34 using System.Reflection;
35 using OpenSim.Framework;
36 using OpenSim.Framework.Console;
37 using OpenSim.Server.Base;
38 using OpenSim.Services.Base;
39 using OpenSim.Services.Interfaces;
40 using Nini.Config;
41 using log4net;
42 using OpenMetaverse;
43 
44 namespace OpenSim.Server.Handlers.BakedTextures
45 {
47  {
48  private static readonly ILog m_log =
49  LogManager.GetLogger(
50  MethodBase.GetCurrentMethod().DeclaringType);
51 
52  protected string m_FSBase;
53 
54  private System.Text.UTF8Encoding utf8encoding =
55  new System.Text.UTF8Encoding();
56 
57  public XBakes(IConfigSource config) : base(config)
58  {
59  MainConsole.Instance.Commands.AddCommand("fs", false,
60  "delete bakes", "delete bakes <ID>",
61  "Delete agent's baked textures from server",
62  HandleDeleteBakes);
63 
64  IConfig assetConfig = config.Configs["BakedTextureService"];
65  if (assetConfig == null)
66  {
67  throw new Exception("No BakedTextureService configuration");
68  }
69 
70  m_FSBase = assetConfig.GetString("BaseDirectory", String.Empty);
71  if (m_FSBase == String.Empty)
72  {
73  m_log.ErrorFormat("[BAKES]: BaseDirectory not specified");
74  throw new Exception("Configuration error");
75  }
76 
77  m_log.Info("[BAKES]: XBakes service enabled");
78  }
79 
80  public string Get(string id)
81  {
82  string file = HashToFile(id);
83  string diskFile = Path.Combine(m_FSBase, file);
84 
85  if (File.Exists(diskFile))
86  {
87  try
88  {
89  byte[] content = File.ReadAllBytes(diskFile);
90 
91  return utf8encoding.GetString(content);
92  }
93  catch
94  {
95  }
96  }
97  return String.Empty;
98  }
99 
100  public void Store(string id, string sdata)
101  {
102  string file = HashToFile(id);
103  string diskFile = Path.Combine(m_FSBase, file);
104 
105  Directory.CreateDirectory(Path.GetDirectoryName(diskFile));
106 
107  File.Delete(diskFile);
108 
109  byte[] data = utf8encoding.GetBytes(sdata);
110 
111  using (FileStream fs = File.Create(diskFile))
112  fs.Write(data, 0, data.Length);
113  }
114 
115  private void HandleDeleteBakes(string module, string[] args)
116  {
117  if (args.Length < 3)
118  {
119  MainConsole.Instance.Output("Syntax: delete bakes <ID>");
120  return;
121  }
122 
123  string file = HashToFile(args[2]);
124  string diskFile = Path.Combine(m_FSBase, file);
125 
126  if (File.Exists(diskFile))
127  {
128  File.Delete(diskFile);
129  MainConsole.Instance.Output("Bakes deleted");
130  return;
131  }
132  MainConsole.Instance.Output("Bakes not found");
133  }
134 
135  public string HashToPath(string hash)
136  {
137  return Path.Combine(hash.Substring(0, 2),
138  Path.Combine(hash.Substring(2, 2),
139  Path.Combine(hash.Substring(4, 2),
140  hash.Substring(6, 4))));
141  }
142 
143  public string HashToFile(string hash)
144  {
145  return Path.Combine(HashToPath(hash), hash);
146  }
147  }
148 }
void Store(string id, string sdata)
Definition: XBakes.cs:100
Interactive OpenSim region server
Definition: OpenSim.cs:55