257 lines
8.7 KiB
C#
257 lines
8.7 KiB
C#
namespace LSServerService
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
|
|
internal static class LSFileManage
|
|
{
|
|
private static Dictionary<int, LSFileData> m_fileList = new Dictionary<int, LSFileData>();
|
|
private static string m_rootPath = "";
|
|
|
|
public static void createClientDirectory(string strPath)
|
|
{
|
|
if (strPath != "")
|
|
{
|
|
try
|
|
{
|
|
string str = "";
|
|
string[] strArray = strPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
|
|
for (int i = 0; i < strArray.Length; i++)
|
|
{
|
|
str = str + strArray[i] + "/";
|
|
if (!Directory.Exists(m_rootPath + str))
|
|
{
|
|
Directory.CreateDirectory(m_rootPath + str);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
PubUtils.WriteLog("LSFileManage.createClientDirectory errorcode: {0} ", new object[] { exception });
|
|
}
|
|
}
|
|
}
|
|
|
|
public static LSFileData createClientFile(string strName)
|
|
{
|
|
if (strName == "")
|
|
{
|
|
return null;
|
|
}
|
|
LSFileData fd = new LSFileData();
|
|
try
|
|
{
|
|
string path = m_rootPath + strName;
|
|
fd.fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
PubUtils.WriteLog("LSFileManage.createClientFile errorcode: {0} ", new object[] { exception });
|
|
eraseFile(fd);
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
public static bool createDir(LSDBManage dbmanager, int parentId, string dirName)
|
|
{
|
|
try
|
|
{
|
|
Directory.CreateDirectory(m_rootPath + dbmanager.getRelativePath(parentId) + dirName);
|
|
return true;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
PubUtils.WriteLog("LSFileManage.createDir errorcode: {0} ", new object[] { exception });
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static LSFileData createFile(LSDBManage dbmanager, int parentId, string fileName, bool saveOld)
|
|
{
|
|
LSFileData data = new LSFileData();
|
|
try
|
|
{
|
|
string str = dbmanager.getRelativePath(parentId);
|
|
string rootPath = m_rootPath;
|
|
string[] strArray = str.Split(new string[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
|
|
for (int i = 0; i < strArray.Length; i++)
|
|
{
|
|
rootPath = rootPath + strArray[i] + "//";
|
|
if (!Directory.Exists(rootPath))
|
|
{
|
|
Directory.CreateDirectory(rootPath);
|
|
}
|
|
}
|
|
string path = m_rootPath + str + fileName;
|
|
if (saveOld && File.Exists(path))
|
|
{
|
|
File.Move(path, path + "#" + DateTime.Now.ToString("yyyy_MM_dd#hh_mm_ss"));
|
|
}
|
|
data.fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
PubUtils.WriteLog("LSFileManage.createFile errorcode: {0} ", new object[] { exception });
|
|
}
|
|
return data;
|
|
}
|
|
|
|
public static bool createFileData(C2S2C_FileData data, out C2S2C_FileTranslateComplete structComplete)
|
|
{
|
|
structComplete = new C2S2C_FileTranslateComplete(true);
|
|
try
|
|
{
|
|
LSFileData data2 = m_fileList[data.dwFileId];
|
|
if (data2 != null)
|
|
{
|
|
data2.fs.Write(data.FileData, 0, data.dwPacketLen);
|
|
data2.dwTotalBytes = data.dwTotalBytes;
|
|
data2.dwTotalCount = data.dwTotalCount;
|
|
data2.dwRevPacketCount++;
|
|
data2.lastWriteTime = Environment.TickCount;
|
|
if (data2.fs.Length >= data2.dwTotalBytes)
|
|
{
|
|
structComplete.dwFileId = data2.dwFileId;
|
|
structComplete.dwTotalBytes = data2.dwTotalBytes;
|
|
structComplete.dwTotalCount = data2.dwTotalCount;
|
|
structComplete.dwRevPacketCount = data2.dwRevPacketCount;
|
|
structComplete.dwRevBytesLen = (int) data2.fs.Length;
|
|
data2.fs.Flush();
|
|
data2.fs.Close();
|
|
data2.fs.Dispose();
|
|
data2.fs = null;
|
|
lock (m_fileList)
|
|
{
|
|
m_fileList.Remove(data.dwFileId);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
PubUtils.WriteLog("LSFileManage.createFileData errorcode: {0} ", new object[] { exception });
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool deleteFile(LSDBManage dbmanager, int fileId)
|
|
{
|
|
try
|
|
{
|
|
string path = m_rootPath + dbmanager.getAbsoluteFile(fileId);
|
|
if (File.Exists(path))
|
|
{
|
|
File.Move(path, path + "#" + DateTime.Now.ToString("yyyy_MM_dd#hh_mm_ss"));
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
PubUtils.WriteLog("LSFileManage.deleteFile errorcode: {0} ", new object[] { exception });
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static void dispose()
|
|
{
|
|
try
|
|
{
|
|
lock (m_fileList)
|
|
{
|
|
foreach (KeyValuePair<int, LSFileData> pair in m_fileList)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
PubUtils.WriteLog("LSFileManage.dispose errorcode: {0} ", new object[] { exception });
|
|
}
|
|
}
|
|
|
|
public static void eraseFile(LSFileData fd)
|
|
{
|
|
if ((fd != null) && (fd.fs != null))
|
|
{
|
|
try
|
|
{
|
|
string name = fd.fs.Name;
|
|
fd.fs.Flush();
|
|
fd.fs.Close();
|
|
fd.fs.Dispose();
|
|
fd.fs = null;
|
|
File.Delete(name);
|
|
if (File.Exists(name))
|
|
{
|
|
PubUtils.WriteLog("LSFileManage.eraseFile 无法删除指定文件: {0} ", new object[] { name });
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
PubUtils.WriteLog("LSFileManage.eraseFile errorcode: {0} ", new object[] { exception });
|
|
}
|
|
lock (m_fileList)
|
|
{
|
|
m_fileList.Remove(fd.dwFileId);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void eraseFile(int dwFileId)
|
|
{
|
|
lock (m_fileList)
|
|
{
|
|
LSFileData fd = m_fileList[dwFileId];
|
|
eraseFile(fd);
|
|
}
|
|
}
|
|
|
|
public static bool existDir(LSDBManage dbmanager, int parentId, string dirName)
|
|
{
|
|
string str = dbmanager.getRelativePath(parentId);
|
|
return Directory.Exists(m_rootPath + str + dirName);
|
|
}
|
|
|
|
public static bool existFile(LSDBManage dbmanager, int parentId, string fileName)
|
|
{
|
|
string str = dbmanager.getRelativePath(parentId);
|
|
return File.Exists(m_rootPath + str + fileName);
|
|
}
|
|
|
|
public static bool init(string rootPath)
|
|
{
|
|
m_rootPath = rootPath;
|
|
return Directory.Exists(rootPath);
|
|
}
|
|
|
|
public static void insertFileData(LSFileData fd)
|
|
{
|
|
lock (m_fileList)
|
|
{
|
|
if (m_fileList.ContainsKey(fd.dwFileId))
|
|
{
|
|
m_fileList.Remove(fd.dwFileId);
|
|
}
|
|
else
|
|
{
|
|
m_fileList.Add(fd.dwFileId, fd);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static string joinFileName(string filename)
|
|
{
|
|
string path = m_rootPath + filename;
|
|
if (File.Exists(path))
|
|
{
|
|
return path;
|
|
}
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
|