| | |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Reflection; |
| | | using System.Windows.Forms; |
| | | |
| | | namespace DataLoader.CS |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | 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 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)"; |
| | | 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 string GetFilePathByGuid(string guid) |
| | | { |
| | | string sql = "select path from lf.sys_meta_file where guid = @guid limit"; |
| | | string sql = "select path from lf.sys_meta_file where guid = @guid limit 1"; |
| | | |
| | | DbParameter dp = new NpgsqlParameter("@guid", guid); |
| | | object obj = Helper.GetScalar(sql, dp); |
| | | |
| | | return obj == null ? null : obj.ToString(); |
| | | } |
| | | |
| | | public static bool IsFileExists(string guid) |
| | | { |
| | | string sql = "select count(*) from lf.sys_meta_file where guid = @guid limit 1"; |
| | | |
| | | DbParameter dp = new NpgsqlParameter("@guid", guid); |
| | | object obj = Helper.GetScalar(sql, dp); |
| | | |
| | | return obj != null && Convert.ToInt32(obj) > 0; |
| | | } |
| | | |
| | | public static int InsertMeta(SysMeta meta) |
| | |
| | | public static void Import(ObservableCollection<ViewData> viewDatas, string source, string target) |
| | | { |
| | | string[] files = Directory.GetFiles(source); |
| | | foreach (string file in files) |
| | | { |
| | | ViewData vd = new ViewData(); |
| | | viewDatas.Add(vd); |
| | | |
| | | |
| | | } |
| | | |
| | | for (int i = 0, c = files.Length; i < c; i++) |
| | | { |
| | | ViewData vd = new ViewData(); |
| | |
| | | |
| | | SetSysMeta(vd); |
| | | } |
| | | |
| | | ImportFiles(viewDatas, target); |
| | | } |
| | | |
| | | private static void SetSysMeta(ViewData vd) |
| | |
| | | vd.Meta.dirid = 0; |
| | | vd.Meta.depid = 0; |
| | | vd.Meta.verid = 0; |
| | | vd.Meta.type = "File"; |
| | | vd.Meta.type = "file"; |
| | | vd.Meta.sizes = Tool.SizeToMb(fi.Length); |
| | | vd.Meta.cs = null; |
| | | vd.Meta.scale = null; |
| | |
| | | vd.Meta.descr = null; |
| | | vd.Meta.create_user = Tool.UserId; |
| | | } |
| | | |
| | | public static void ImportFiles(ObservableCollection<ViewData> viewDatas, string target) |
| | | { |
| | | int start = 1; |
| | | foreach (ViewData vd in viewDatas) |
| | | { |
| | | try |
| | | { |
| | | vd.Status = "获取MD5码"; |
| | | string guid = MD5Helper.GetMD5Hash(vd.FilePath); |
| | | if (IsFileExists(guid)) |
| | | { |
| | | vd.Status = "已存在"; |
| | | continue; |
| | | } |
| | | |
| | | vd.Status = "插入数据库"; |
| | | int metaId = InsertMeta(vd.Meta); |
| | | if (metaId == 0) |
| | | { |
| | | vd.Status = "元数据出错"; |
| | | continue; |
| | | } |
| | | |
| | | start = GetSubPath(target, start); |
| | | SysMetaFile mf = GetMetaFile(vd, metaId, start, guid); |
| | | InsertMetaFile(mf); |
| | | |
| | | vd.Status = "复制文件"; |
| | | CopyFile(vd.FilePath, Path.Combine(target, mf.path)); |
| | | |
| | | vd.Status = "完成"; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | vd.Status = "失败!"; |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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++; |
| | | } |
| | | } |
| | | |
| | | private static SysMetaFile GetMetaFile(ViewData vd, int metaId, int subPath, string guid) |
| | | { |
| | | SysMetaFile mf = new SysMetaFile(); |
| | | mf.name = vd.Meta.name; |
| | | mf.metaid = metaId; |
| | | mf.fileid = 0; |
| | | mf.guid = guid; |
| | | mf.path = subPath + "\\" + mf.name; |
| | | mf.sizes = vd.Meta.sizes; |
| | | mf.create_user = vd.Meta.create_user; |
| | | |
| | | return mf; |
| | | } |
| | | |
| | | private static void CopyFile(string source, string target) |
| | | { |
| | | if (File.Exists(source)) |
| | | { |
| | | File.Copy(source, target, true); |
| | | } |
| | | } |
| | | } |
| | | } |