| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Collections.ObjectModel; |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Reflection; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | |
| | |
| | | { |
| | | public class Importor |
| | | { |
| | | private static readonly string insertMeta = "insert into lf.sys_meta(name, dirid, depid, verid, type, sizes, cs, scale, resolution, gather, batch, descr, create_user) values @name, @dirid, @depid, @verid, @type, @sizes, @cs, @scale, @resolution, @gather, @batch, @descr, @create_user) returning id"; |
| | | |
| | | private static readonly string insertMetaFile = "insert into lf.sys_meta_file(name, metaid, fileid, guid, path, sizes, create_user) values (@name, @metaid, @fileid, @guid, @path, @sizes, @create_user)"; |
| | | |
| | | public static void Import(ObservableCollection<ViewData> viewDatas, string source, string target) |
| | | #region 加载 |
| | | /// <summary> |
| | | /// 获取文件 |
| | | /// </summary> |
| | | public static void GetFiles(ObservableCollection<ViewData> viewDatas, string path) |
| | | { |
| | | GetSubFiles(viewDatas, path); |
| | | |
| | | string[] directories = Directory.GetDirectories(path); |
| | | if (null == directories || directories.Length == 0) return; |
| | | |
| | | foreach (string sub in directories) |
| | | { |
| | | if (IsGdbFile(sub)) |
| | | { |
| | | SetSysMeta(viewDatas, sub, "gdb"); |
| | | continue; |
| | | } |
| | | if (IsOsgbFile(sub)) |
| | | { |
| | | SetSysMeta(viewDatas, sub, "osgb"); |
| | | continue; |
| | | } |
| | | |
| | | GetFiles(viewDatas, sub); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 设置元数据 |
| | | /// </summary> |
| | | private static void SetSysMeta(ObservableCollection<ViewData> viewDatas, string path, string ext) |
| | | { |
| | | ViewData vd = new ViewData(); |
| | | vd.ID = viewDatas.Count + 1; |
| | | vd.FilePath = path; |
| | | vd.Ext = "." + ext; |
| | | vd.Status = "准备"; |
| | | viewDatas.Add(vd); |
| | | |
| | | SetSysMeta(vd); |
| | | |
| | | DirectoryInfo di = new DirectoryInfo(vd.FilePath); |
| | | vd.Meta.name = di.Name; |
| | | vd.Meta.type = ext; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取子文件 |
| | | /// </summary> |
| | | private static void GetSubFiles(ObservableCollection<ViewData> viewDatas, string path) |
| | | { |
| | | string[] files = Directory.GetFiles(path); |
| | | if (null == files || files.Length == 0) return; |
| | | |
| | | foreach (string file in files) |
| | | { |
| | | string ext = Path.GetExtension(file).ToLower(); |
| | | if (!StaticData.ALL_EXTENSION.Contains(ext) || IsExcludeFile(file)) continue; |
| | | |
| | | ViewData vd = new ViewData(); |
| | | vd.ID = viewDatas.Count + 1; |
| | | vd.FilePath = file; |
| | | vd.Ext = ext; |
| | | vd.Status = "准备"; |
| | | viewDatas.Add(vd); |
| | | |
| | | SetSysMeta(vd); |
| | | |
| | | FileInfo fi = new FileInfo(vd.FilePath); |
| | | vd.Meta.name = fi.Name; |
| | | vd.Meta.type = fi.Extension.ToLower().Replace(".", ""); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 设置元数据 |
| | | /// </summary> |
| | | private static void SetSysMeta(ViewData vd) |
| | | { |
| | | SysMeta meta = new SysMeta(); |
| | | meta.eventid = Guid.NewGuid().ToString(); |
| | | meta.metaid = 0; |
| | | meta.verid = 0; |
| | | meta.guid = null; |
| | | meta.path = vd.FilePath; // * |
| | | meta.sizes = GetFileSizes(vd); |
| | | meta.tab = null; |
| | | meta.rows = 0; |
| | | meta.create_user = CommonProp.UserId; |
| | | meta.create_time = DateTime.Now; |
| | | meta.bak = null; |
| | | meta.geom = null; |
| | | meta.layer = null; |
| | | meta.depcode = CommonProp.Depcode; |
| | | meta.dircode = CommonProp.Dircode; |
| | | meta.ismeta = 0; |
| | | meta.sensortype = CommonProp.SensorType; |
| | | meta.acq_time = CommonProp.AcqTime; |
| | | |
| | | vd.Meta = meta; |
| | | vd.Sizes = meta.sizes; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取文件大小 |
| | | /// </summary> |
| | | private static double GetFileSizes(ViewData vd) |
| | | { |
| | | if (File.Exists(vd.FilePath)) |
| | | { |
| | | if (vd.Ext == StaticData.SHP) |
| | | { |
| | | List<string> files = GetShpFiles(vd.FilePath); |
| | | return GetFileSizes(files.ToArray()); |
| | | } |
| | | else |
| | | { |
| | | FileInfo fi = new FileInfo(vd.FilePath); |
| | | return Tools.SizeToMb(fi.Length); |
| | | } |
| | | } |
| | | |
| | | if (Directory.Exists(vd.FilePath)) |
| | | { |
| | | string[] files = Directory.GetFiles(vd.FilePath, "*", SearchOption.AllDirectories); |
| | | return GetFileSizes(files); |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取文件大小 |
| | | /// </summary> |
| | | private static double GetFileSizes(string[] files) |
| | | { |
| | | if (null == files || files.Length == 0) return 0; |
| | | |
| | | double sizes = 0; |
| | | foreach (string file in files) |
| | | { |
| | | FileInfo fi = new FileInfo(file); |
| | | sizes += Tools.SizeToMb(fi.Length); |
| | | } |
| | | |
| | | return sizes; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取SHP文件集合 |
| | | /// </summary> |
| | | private static List<string> GetShpFiles(string shpPath) |
| | | { |
| | | List<string> files = new List<string>(); |
| | | files.Add(shpPath); |
| | | |
| | | foreach (string ext in StaticData.SHP_EXT) |
| | | { |
| | | string file = shpPath.Replace(".shp", ext); |
| | | if (File.Exists(file)) |
| | | { |
| | | files.Add(file); |
| | | } |
| | | } |
| | | |
| | | return files; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 是否为GDB文件 |
| | | /// </summary> |
| | | private static bool IsGdbFile(string path) |
| | | { |
| | | if (!Directory.Exists(path)) return false; |
| | | |
| | | DirectoryInfo info = new DirectoryInfo(path); |
| | | if (!info.Name.ToLower().EndsWith(StaticData.GDB)) return false; |
| | | |
| | | string gdb = Path.Combine(path, StaticData.NGDB); |
| | | |
| | | return File.Exists(gdb); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 是否为OSGB文件 |
| | | /// </summary> |
| | | private static bool IsOsgbFile(string path) |
| | | { |
| | | if (!Directory.Exists(path)) return false; |
| | | |
| | | string meta = Path.Combine(path, "metadata.xml"); |
| | | string data = Path.Combine(path, "Data"); |
| | | |
| | | return File.Exists(meta) && Directory.Exists(data); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 是/否为排除文件 |
| | | /// </summary> |
| | | private static bool IsExcludeFile(string file) |
| | | { |
| | | bool isExclude = false; |
| | | |
| | | string name = Path.GetFileName(file).ToLower(); |
| | | foreach (string ext in StaticData.MAPPER_EXCLUDE_EXT) |
| | | { |
| | | if (name.EndsWith(ext)) |
| | | { |
| | | isExclude = true; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | return isExclude; |
| | | } |
| | | #endregion |
| | | |
| | | #region 导入 |
| | | /// <summary> |
| | | /// 导入文件 |
| | | /// </summary> |
| | | public static void ImportFiles(ObservableCollection<ViewData> viewDatas) |
| | | { |
| | | Parallel.ForEach(viewDatas, (vd, ParallelLoopState) => |
| | | { |
| | | try |
| | | { |
| | | vd.Status = "生成MD5..."; |
| | | vd.Meta.guid = GetFilesMD5(vd); |
| | | |
| | | if (!string.IsNullOrEmpty(vd.Meta.guid) && DBHelper.IsFileExists(vd.Meta.guid)) |
| | | { |
| | | vd.Status = "已存在!"; |
| | | return; |
| | | } |
| | | |
| | | vd.Status = "复制文件..."; |
| | | CopyFiles(vd); |
| | | |
| | | vd.Status = "数据入库..."; |
| | | int id = DBHelper.InsertMeta(vd.Meta); |
| | | vd.Status = id > 0 ? "成功。" : "失败!"; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.StackTrace); |
| | | vd.Status = string.Format("失败:{0}!", ex.Message); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取文件的MD5 |
| | | /// </summary> |
| | | private static string GetFilesMD5(ViewData vd) |
| | | { |
| | | if ("1" != Tools.GetSetting("GetMD5")) return null; |
| | | |
| | | if (File.Exists(vd.FilePath)) |
| | | { |
| | | if (vd.Ext == StaticData.SHP) |
| | | { |
| | | List<string> files = GetShpFiles(vd.FilePath); |
| | | return GetFilesMD5(files.ToArray()); |
| | | } |
| | | else |
| | | { |
| | | return MD5Helper.GetMD5Hash(vd.FilePath); |
| | | } |
| | | } |
| | | |
| | | if (Directory.Exists(vd.FilePath)) |
| | | { |
| | | string[] files = Directory.GetFiles(vd.FilePath, "*", SearchOption.AllDirectories); |
| | | return GetFilesMD5(files); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取多个文件的MD5码 |
| | | /// </summary> |
| | | private static string GetFilesMD5(string[] files) |
| | | { |
| | | if (null == files || files.Length == 0) return null; |
| | | |
| | | List<string> list = new List<string>(); |
| | | foreach (string file in files) |
| | | { |
| | | string md5 = MD5Helper.GetMD5Hash(file); |
| | | if (string.IsNullOrEmpty(md5)) list.Add(md5); |
| | | } |
| | | |
| | | if (list.Count == 0) return null; |
| | | |
| | | string str = string.Join(",", list.ToArray()); |
| | | byte[] bytes = Encoding.Unicode.GetBytes(str); |
| | | |
| | | return MD5Helper.GetMD5Hash(bytes); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 复制文件 |
| | | /// </summary> |
| | | private static void CopyFiles(ViewData vd) |
| | | { |
| | | //mf.path = subPath + "\\" + mf.name; |
| | | |
| | | //if (File.Exists(source)) |
| | | //{ |
| | | // File.Copy(source, target, true); |
| | | //} |
| | | } |
| | | |
| | | private static int GetSubPath(string target, int start = 1) |
| | | { |
| | | while (true) |
| | | { |
| | | string path = Path.Combine(target, start.ToString()); |
| | | if (!Directory.Exists(path)) |
| | | { |
| | | Directory.CreateDirectory(path); |
| | | return start; |
| | | } |
| | | |
| | | string[] files = Directory.GetFiles(path); |
| | | if (files.Length < 2001) |
| | | { |
| | | return start; |
| | | } |
| | | |
| | | start++; |
| | | } |
| | | } |
| | | #endregion |
| | | } |
| | | } |