123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using System;
- using System.Diagnostics;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Threading;
- using System.Reflection;
- using OpenSim.Framework;
- using OpenSim.Framework.Console;
- using OpenSim.Server.Base;
- using OpenSim.Services.Base;
- using OpenSim.Services.Interfaces;
- using Nini.Config;
- using log4net;
- using OpenMetaverse;
- namespace OpenSim.Server.Handlers.BakedTextures
- {
- public class XBakes : ServiceBase, IBakedTextureService
- {
- private static readonly ILog m_log =
- LogManager.GetLogger(
- MethodBase.GetCurrentMethod().DeclaringType);
- protected string m_FSBase;
- private System.Text.UTF8Encoding utf8encoding =
- new System.Text.UTF8Encoding();
- public XBakes(IConfigSource config) : base(config)
- {
- MainConsole.Instance.Commands.AddCommand("fs", false,
- "delete bakes", "delete bakes <ID>",
- "Delete agent's baked textures from server",
- HandleDeleteBakes);
- IConfig assetConfig = config.Configs["BakedTextureService"];
- if (assetConfig == null)
- {
- throw new Exception("No BakedTextureService configuration");
- }
- m_FSBase = assetConfig.GetString("BaseDirectory", String.Empty);
- if (m_FSBase == String.Empty)
- {
- m_log.ErrorFormat("[BAKES]: BaseDirectory not specified");
- throw new Exception("Configuration error");
- }
- m_log.Info("[BAKES]: XBakes service enabled");
- }
- public string Get(string id)
- {
- string file = HashToFile(id);
- string diskFile = Path.Combine(m_FSBase, file);
- if (File.Exists(diskFile))
- {
- try
- {
- byte[] content = File.ReadAllBytes(diskFile);
- return utf8encoding.GetString(content);
- }
- catch
- {
- }
- }
- return String.Empty;
- }
- public void Store(string id, string sdata)
- {
- string file = HashToFile(id);
- string diskFile = Path.Combine(m_FSBase, file);
- Directory.CreateDirectory(Path.GetDirectoryName(diskFile));
- File.Delete(diskFile);
- byte[] data = utf8encoding.GetBytes(sdata);
- using (FileStream fs = File.Create(diskFile))
- fs.Write(data, 0, data.Length);
- }
- private void HandleDeleteBakes(string module, string[] args)
- {
- if (args.Length < 3)
- {
- MainConsole.Instance.Output("Syntax: delete bakes <ID>");
- return;
- }
- string file = HashToFile(args[2]);
- string diskFile = Path.Combine(m_FSBase, file);
- if (File.Exists(diskFile))
- {
- File.Delete(diskFile);
- MainConsole.Instance.Output("Bakes deleted");
- return;
- }
- MainConsole.Instance.Output("Bakes not found");
- }
- public string HashToPath(string hash)
- {
- return Path.Combine(hash.Substring(0, 2),
- Path.Combine(hash.Substring(2, 2),
- Path.Combine(hash.Substring(4, 2),
- hash.Substring(6, 4))));
- }
- public string HashToFile(string hash)
- {
- return Path.Combine(HashToPath(hash), hash);
- }
- }
- }
|