115 lines
3.3 KiB
C#
115 lines
3.3 KiB
C#
using log4net;
|
|
using Nini.Config;
|
|
using OpenMetaverse;
|
|
using OpenSim.Region.Framework.Scenes;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace OpenSim.Modules.DataValue.Storage
|
|
{
|
|
class FileSystem : iStorage
|
|
{
|
|
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
private Scene m_scene = null;
|
|
|
|
private bool m_enabledCompress = true;
|
|
private string m_dataValueDirectory = "./ScriptDataValue";
|
|
|
|
public FileSystem(Scene scene, IConfig config)
|
|
{
|
|
m_scene = scene;
|
|
|
|
m_dataValueDirectory = config.Configs["XEngine"].GetString("DataValueStorageDirectory", m_dataValueDirectory);
|
|
m_enabledCompress = config.Configs["XEngine"].GetBoolean("EnabledDataStorageCompressing", m_enabledCompress);
|
|
}
|
|
|
|
public bool check(String storageID, string key)
|
|
{
|
|
string _filePath = getFilePath(hostID, key);
|
|
|
|
FileInfo file = new FileInfo(_filePath);
|
|
|
|
if (file.Exists)
|
|
return 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
public string get(String storageID, string key)
|
|
{
|
|
string _filePath = getFilePath(hostID, key);
|
|
|
|
FileInfo file = new FileInfo(_filePath);
|
|
|
|
if (file.Exists)
|
|
{
|
|
if (m_enabledCompress)
|
|
return Compress.Unzip(File.ReadAllBytes(file.FullName));
|
|
|
|
return File.ReadAllText(file.FullName);
|
|
}
|
|
|
|
|
|
return "";
|
|
}
|
|
|
|
public string remove(string storageID, string key)
|
|
{
|
|
string _filePath = getFilePath(hostID, key);
|
|
|
|
FileInfo file = new FileInfo(_filePath);
|
|
|
|
if (file.Exists)
|
|
file.Delete();
|
|
}
|
|
|
|
public void save(String storageID, string key, string data)
|
|
{
|
|
string _filePath = getFilePath(hostID, key);
|
|
|
|
FileInfo file = new FileInfo(_filePath);
|
|
|
|
if (m_enabledCompress)
|
|
{
|
|
File.WriteAllBytes(file.FullName, Compress.Zip(value));
|
|
return;
|
|
}
|
|
|
|
File.WriteAllText(file.FullName, value);
|
|
}
|
|
}
|
|
|
|
private string getFilePath(UUID host, string index)
|
|
{
|
|
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(host);
|
|
|
|
if (_host != null)
|
|
{
|
|
string _nameSpace = _host.GroupID.ToString().Trim().ToUpper().Replace("-", "");
|
|
|
|
if (!Directory.Exists(m_dataValueDirectory))
|
|
Directory.CreateDirectory(m_dataValueDirectory);
|
|
|
|
if (!Directory.Exists(m_dataValueDirectory + "/" + _nameSpace))
|
|
Directory.CreateDirectory(m_dataValueDirectory + "/" + _nameSpace);
|
|
|
|
string _storageKey = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(ASCIIEncoding.ASCII.GetBytes(index.Trim().ToUpper()))).Replace("-", "");
|
|
|
|
if (m_enabledCompress)
|
|
return m_dataValueDirectory + "/" + _nameSpace + "/" + _storageKey + ".gz";
|
|
|
|
return m_dataValueDirectory + "/" + _nameSpace + "/" + _storageKey + ".txt";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|