From 375658a12de3283286a4fc0504959ef5e956106d Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 23 八月 2023 15:15:13 +0800
Subject: [PATCH] 添加获取文件MD5、设置元数据等函数

---
 DataLoader/App.config         |    2 
 DataLoader/MainWindow.xaml.cs |    2 
 DataLoader/CS/Importor.cs     |  194 +++++++++++++++++++++++++++++++++++++++---------
 DataLoader/CS/MD5Helper.cs    |    2 
 DataLoader/Model/SysMeta.cs   |    2 
 5 files changed, 161 insertions(+), 41 deletions(-)

diff --git a/DataLoader/App.config b/DataLoader/App.config
index dff2dde..bef7937 100644
--- a/DataLoader/App.config
+++ b/DataLoader/App.config
@@ -1,6 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <configuration>
   <appSettings>
+	<!-- 鑾峰彇鏂囦欢鐨凪D5鐮侊細0-鍚︼紝1-鏄� -->
+	<add key="GetMD5" value="1"/>
 	<add key="StartPath" value="3"/>
 	<add key="SourcePath" value="D:\Moon\data"/>
 	<add key="TargetPath" value="D:\LF\upload"/>
diff --git a/DataLoader/CS/Importor.cs b/DataLoader/CS/Importor.cs
index c93f991..2424b20 100644
--- a/DataLoader/CS/Importor.cs
+++ b/DataLoader/CS/Importor.cs
@@ -2,29 +2,76 @@
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
+using System.Data;
 using System.IO;
 using System.Linq;
+using System.Security.Cryptography;
+using System.Text;
 using System.Threading.Tasks;
+using static System.Net.Mime.MediaTypeNames;
 
 namespace DataLoader.CS
 {
     public class Importor
     {
+        #region 鍔犺浇
         /// <summary>
         /// 鑾峰彇鏂囦欢
         /// </summary>
-        public static void GetFiles(ObservableCollection<ViewData> viewDatas)
+        public static void GetFiles(ObservableCollection<ViewData> viewDatas, string path)
         {
-            string[] files = Directory.GetFiles(CommonProp.SourcePath, "*", SearchOption.AllDirectories);
+            GetSubFiles(viewDatas, path);
 
-            int i = 1;
-            foreach(string file in files)
+            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);
+        }
+
+        /// <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) || IsExcludFile(file)) continue;
+                if (!StaticData.ALL_EXTENSION.Contains(ext) || IsExcludeFile(file)) continue;
 
                 ViewData vd = new ViewData();
-                vd.ID = i++;
+                vd.ID = viewDatas.Count + 1;
                 vd.FilePath = file;
                 vd.Ext = ext;
                 vd.Status = "鍑嗗";
@@ -40,39 +87,127 @@
         private static void SetSysMeta(ViewData vd)
         {
             FileInfo fi = new FileInfo(vd.FilePath);
+            SysMeta meta = new SysMeta();
+            meta.eventid = Guid.NewGuid().ToString();
+            meta.metaid = 0;
+            meta.verid = 0;
+            meta.name = fi.Name;
+            meta.type = fi.Extension.ToLower().Replace(".", "");
+            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 = new SysMeta();
-            vd.Meta.name = fi.Name;
-            //vd.Meta.dirid = Common.DirId;
-            //vd.Meta.depid = 1;
-            vd.Meta.verid = 0;
-            vd.Meta.type = fi.Extension.ToLower().Replace(".", "");
-            vd.Meta.sizes = Tools.SizeToMb(fi.Length);
-            vd.Meta.create_user = CommonProp.UserId;
+            vd.Meta = meta;
+            vd.Sizes = meta.sizes;
+        }
+
+        private static double GetFileSizes(ViewData vd)
+        {
+            //Tools.SizeToMb(fi.Length); // *
+
+
+            return 0;
+        }
+
+        /// <summary>
+        /// 鏄惁涓篏DB鏂囦欢
+        /// </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>
+        /// 鏄惁涓篛SGB鏂囦欢
+        /// </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 IsExcludFile(string file)
+        private static bool IsExcludeFile(string file)
         {
-            bool isExclud = false;
+            bool isExclude = false;
 
             string name = Path.GetFileName(file).ToLower();
-            foreach (string ss in StaticData.MAPPER_EXCLUDE_EXT)
+            foreach (string ext in StaticData.MAPPER_EXCLUDE_EXT)
             {
-                if (name.EndsWith(ss))
+                if (name.EndsWith(ext))
                 {
-                    isExclud = true;
+                    isExclude = true;
                     break;
                 }
             }
 
-            return isExclud;
+            return isExclude;
         }
 
         /// <summary>
-        /// 鏁版嵁瀵煎叆
+        /// 鑾峰彇鏂囦欢鐨凪D5鐮�
+        /// </summary>
+        private static string GetFileMD5(string file)
+        {
+            if ("1" != Tools.GetSetting("GetMD5") || !File.Exists(file)) return null;
+
+            return MD5Helper.GetMD5Hash(file);
+        }
+
+        /// <summary>
+        /// 鑾峰彇澶氫釜鏂囦欢鐨凪D5鐮�
+        /// </summary>
+        private static string GetFilesMD5(string path)
+        {
+            if ("1" != Tools.GetSetting("GetMD5") || !Directory.Exists(path)) return null;
+
+            string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
+            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);
+        }
+        #endregion
+
+        #region 瀵煎叆
+        /// <summary>
+        /// 瀵煎叆鏂囦欢
         /// </summary>
         public static void ImportFiles(ObservableCollection<ViewData> viewDatas)
         {
@@ -104,22 +239,6 @@
             if (File.Exists(source))
             {
                 File.Copy(source, target, true);
-            }
-        }
-
-        public static void GetFiles(ObservableCollection<ViewData> viewDatas, string source)
-        {
-            string[] files = Directory.GetFiles(source);
-            for (int i = 0, c = files.Length; i < c; i++)
-            {
-                ViewData vd = new ViewData();
-                vd.ID = i + 1;
-                vd.FilePath = files[i];
-                vd.Ext = System.IO.Path.GetExtension(files[i]);
-                vd.Status = "鍑嗗";
-                viewDatas.Add(vd);
-
-                SetSysMeta(vd);
             }
         }
 
@@ -220,5 +339,6 @@
 
             return mf;
         }
+        #endregion
     }
 }
diff --git a/DataLoader/CS/MD5Helper.cs b/DataLoader/CS/MD5Helper.cs
index b51a9e3..1e87fbd 100644
--- a/DataLoader/CS/MD5Helper.cs
+++ b/DataLoader/CS/MD5Helper.cs
@@ -3,8 +3,6 @@
 using System.IO;
 using System.Linq;
 using System.Security.Cryptography;
-using System.Text;
-using System.Threading.Tasks;
 
 namespace DataLoader.CS
 {
diff --git a/DataLoader/MainWindow.xaml.cs b/DataLoader/MainWindow.xaml.cs
index b0c417e..f1bc0fa 100644
--- a/DataLoader/MainWindow.xaml.cs
+++ b/DataLoader/MainWindow.xaml.cs
@@ -155,7 +155,7 @@
 
             this.isBusy = true;
             this.viewDatas.Clear();
-            Importor.GetFiles(viewDatas);
+            Importor.GetFiles(viewDatas, CommonProp.SourcePath);
             this.isBusy = false;
         }
 
diff --git a/DataLoader/Model/SysMeta.cs b/DataLoader/Model/SysMeta.cs
index 234db19..eda7e12 100644
--- a/DataLoader/Model/SysMeta.cs
+++ b/DataLoader/Model/SysMeta.cs
@@ -125,7 +125,7 @@
         /// <summary>
         /// 閲囬泦鏃堕棿
         /// </summary>
-        public DateTime acq_time { set; get; }
+        public DateTime? acq_time { set; get; }
 
         /// <summary>
         /// 鍒嗚鲸鐜�

--
Gitblit v1.9.3