| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using Autodesk.Revit.DB;
- using FWindSoft.Tools.SystemUtil;
- using SAGA.DotNetUtils;
- using SAGA.DotNetUtils.Extend;
- using SAGA.MBI.Common;
- using SAGA.MBI.RequestData;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace SAGA.MBI
- {
- public class ProjectLogManager
- {
- #region 键值相关
- /// <summary>
- /// 创建文件键值
- /// </summary>
- /// <param name="fileName">无扩展名名称</param>
- /// <returns></returns>
- public static string CreateLogFileKey(string fileName)
- {
- return fileName;
- }
- /// <summary>
- /// 创建指定项目的服务器文件键值
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static string CreateLogFileKey(Document doc)
- {
- string pathName = doc.PathName;
- string fileName = pathName.GetFileName();
- return CreateLogFileKey(fileName??string.Empty);
- }
- #endregion
- #region 操作相关
- /// <summary>
- /// 删除本机,本层的日志文件。用于重新下载本层的模型文件时
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static bool ClearFile(string fileName)
- {
- try
- {
- string noExtendFileName = fileName.GetFileName();
- string key = CreateLogFileKey(noExtendFileName);
- var delFiles = DeleteFiles(new List<string>() { key }) ?? new List<string>();
- return 1 == delFiles.Count;
- }
- catch (Exception)
- {
- throw;
- }
- }
- /// <summary>
- /// 清除指定前缀的文件
- /// </summary>
- /// <param name="prefix"></param>
- /// <returns></returns>
- public static bool ClearFiles(string prefix)
- {
- try
- {
- var files = GetFiles(prefix);
- if (files==null||files.IsNullOrEmptyExt())
- return true;
- var delFiles = DeleteFiles(files ?? new List<string>());
- return files.Count== delFiles.Count;
- }
- catch (Exception)
- {
- throw;
- }
- }
- #endregion
- #region 获取日志内容
- /// <summary>
- /// 获取日志内容,
- /// </summary>
- /// <param name="fileKey">约定为文件名称</param>
- /// <returns></returns>
- public static string GetLogInfo(string fileKey)
- {
- var log = string.Empty;
- #region 获取服务器日志文件
- if (!string.IsNullOrEmpty(fileKey))
- {
- try
- {
- var bytes = DownLoadFile(fileKey);
- if (bytes != null)
- {
- log = Encoding.UTF8.GetString(bytes);
- }
- }
- catch (Exception)
- {
- }
- }
- #endregion
- return log ?? string.Empty;
- }
- #endregion
- #region 本机文件系统维护
- public static string LogDirectory
- {
- get
- {
- return MBIControl.ProjectCur.LocalPath;
- }
- }
- /// <summary>
- /// 获取获取文件内容
- /// </summary>
- /// <param name="fileKey"></param>
- /// <returns></returns>
- public static byte[] DownLoadFile(string fileKey)
- {
- string path = Path.Combine(LogDirectory, fileKey);
- if (File.Exists(path))
- {
- return File.ReadAllBytes(path);
- }
- return null;
- }
- public static bool UpLoadFile(string fileKey, byte[] bytes)
- {
- try
- {
- string path = Path.Combine(LogDirectory, fileKey);
- File.WriteAllBytes(path, bytes);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- public static List<string> DeleteFiles(List<string> inputFiles)
- {
- List<string> results = new List<string>();
- for (int i = 0; i < inputFiles.Count; i++)
- {
- var currentFile = inputFiles[i];
- if (File.Exists(currentFile))
- {
- try
- {
- File.Delete(currentFile);
- results.Add(currentFile);
- }
- catch (Exception)
- {
- }
- }
- }
- return results;
- }
- public static List<string> GetFiles(string prefix)
- {
- List<string> results=new List<string>();
- if (!Directory.Exists(LogDirectory))
- return results;
- var paths = Directory.GetFiles(LogDirectory);
- foreach (var path in paths)
- {
- FileInfo file=new FileInfo(path);
- if (file.Name.StartsWith(prefix))
- {
- results.Add(path);
- }
- }
- return results;
- }
-
- #endregion
- }
- }
|