예제 #1
0
 /// <summary>
 /// 区分添加子项目
 /// </summary>
 /// <param name="pcCollect"></param>
 /// <param name="mobileCollect"></param>
 /// <param name="newItem"></param>
 private void AddFolderListFileItem(ObservableCollection <DeviceSyncItem> pcCollect,
                                    ObservableCollection <DeviceSyncItem> mobileCollect,
                                    DeviceSyncReadProgressItem newItem)
 {
     if (newItem.FileSource == SyncDeviceType.PC)
     {
         pcCollect.Add(CreateSyncItemViewModel(newItem.FileItem));
     }
     else
     {
         mobileCollect.Add(CreateSyncItemViewModel(newItem.FileItem));
     }
 }
예제 #2
0
        /// <summary>
        /// 读取文件工作任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoReadDeviceFileTask(object sender, DoWorkEventArgs workArgs)
        {
            var args         = (DeviceSyncReadArgs)workArgs.Argument;
            var resultNotify = new MainWindowStatusNotify()
            {
                message = "文件读取完成"
            };

            workArgs.Result = resultNotify;
            //多设备且未手动选择设备
            if (null == args.Device)
            {
                resultNotify.message    = "没有待同步的目标设备";
                resultNotify.alertLevel = AlertLevel.ERROR;
                return;
            }
            //多盘符且未手动选择盘符
            if (null == args.MediaDrive)
            {
                resultNotify.message    = "没有待同步的目标盘符";
                resultNotify.alertLevel = AlertLevel.ERROR;
                return;
            }

            //进度通知
            var progressEvent = new DeviceSyncReadProgressItem
            {
                ProgressType = ReadSyncFileTaskProgressType.TEXT_ONLY,
                Notify       = new MainWindowStatusNotify
                {
                    message     = "正在读取路径配置...",
                    alertLevel  = AlertLevel.RUN,
                    nowProgress = 99
                }
            };

            mReadDeviceFileWorker.ReportProgress(0, progressEvent);
            //从数据库读取
            var dataSet = SQLite.SqlTable("SELECT pc_path, mobile_path FROM media_sync_config where enable = 1", null);

            if (null == dataSet || dataSet.Rows.Count == 0)
            {
                resultNotify.message    = "无法读取路径配置";
                resultNotify.alertLevel = AlertLevel.WARN;
                return;
            }
            //封装进对象
            var syncPathList = new List <MediaSyncConfig>();

            foreach (DataRow dataRow in dataSet.Rows)
            {
                syncPathList.Add(new SyncConfigViewModel()
                {
                    PcPath     = Convert.ToString(dataRow["pc_path"]),
                    MobilePath = Convert.ToString(dataRow["mobile_path"]),
                });
            }
            progressEvent.Notify.message = "正在连接到设备...";
            mReadDeviceFileWorker.ReportProgress(0, progressEvent);
            //筛选WPD设备
            using (args.Device)
            {
                args.Device.Connect();
                foreach (var e in syncPathList)
                {
                    bool isPcPathExists         = Directory.Exists(e.PcPath),
                             isMobilePathExists = args.Device.DirectoryExists(Path.Combine(args.MediaDrive.NameView, e.MobilePath));

                    if (!isPcPathExists && !isMobilePathExists)
                    {
                        //如果两端都不存在目标文件夹, 则跳过
                        continue;
                    }

                    progressEvent.Notify.message = string.Format("正在读取文件列表[{0}]...", e.PcPath);
                    mReadDeviceFileWorker.ReportProgress(0, progressEvent);
                    //读取PC目录下的文件列表
                    var pcFiles = isPcPathExists ? FileUtil.GetFileNameFromFullPath(Directory.GetFiles(e.PcPath)) : new string[0];
                    //读取移动设备下的文件列表
                    //var xxx = args.Device.GetFileSystemEntries(Path.Combine(args.MediaDrive.NameView, e.MobilePath));
                    //isMobilePathExists ? FileUtil.GetFileNameFromFullPath(args.Device.GetFiles(Path.Combine(args.MediaDrive.NameView, e.MobilePath))) :
                    var mobileFileList = new List <string>();
                    if (isMobilePathExists)
                    {
                        var fullMobileFolderPath = Path.Combine(args.MediaDrive.NameView, e.MobilePath);
                        var fullNames            = args.Device.EnumerateFiles(fullMobileFolderPath);
                        foreach (var fullName in fullNames)
                        {
                            //使用Win函数读取文件名
                            var winSupportName = Path.GetFileName(fullName);
                            //通过路径裁剪方式获得文件名
                            var substringName = fullName.Replace(fullMobileFolderPath + Path.DirectorySeparatorChar, string.Empty);
                            //如果两种方式取得的文件名不一致, 说明这个文件名在windows上是不合法的
                            if (!winSupportName.Equals(substringName))
                            {
                                //如果路径包含windows路径符, 记录错误并跳过
                                if (substringName.Contains(Path.DirectorySeparatorChar))
                                {
                                    //发送win10通知, 提醒人工处理此文件
                                    //通知官方文档  https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/send-local-toast-desktop
                                    new ToastContentBuilder()
                                    .AddText("文件收集失败")
                                    .AddText("不支持的文件名")
                                    .AddText(fullName)
                                    .Show();
                                    continue;
                                }
                                //取得后缀名
                                var extendName = Path.GetExtension(substringName);
                                //重命名移动设备上的文件名, 使用随机文件名
                                var newName = string.Format("{0}{1}{2}{3}", DateTime.Now.ToString("yyyyMMddHHmmss"), "_auto_rename_", RandomUtil.MakeSring(false, 6), extendName);
                                args.Device.Rename(fullName, newName);
                                mobileFileList.Add(newName);
                                //发送win10通知, 提醒文件已改名
                                new ToastContentBuilder()
                                .AddText("文件重命名提醒")
                                .AddText(fullMobileFolderPath)
                                .AddText(substringName + " -> " + newName)
                                .Show();
                                continue;
                            }
                            mobileFileList.Add(winSupportName);
                        }
                    }
                    var mobileFilesArr = mobileFileList.ToArray();
                    //找出差异文件
                    var filesOnlyInMobile = DataUtil.FindDiffEls(pcFiles, mobileFilesArr);
                    var filesOnlyInPc     = DataUtil.FindDiffEls(mobileFilesArr, pcFiles);
                    //不存在差异则直接跳过当前文件夹
                    if (DataUtil.IsEmptyCollection(filesOnlyInMobile) && DataUtil.IsEmptyCollection(filesOnlyInPc))
                    {
                        continue;
                    }
                    if (!DataUtil.IsEmptyCollection(filesOnlyInPc))
                    {
                        if (!CollectAndNotify(filesOnlyInPc, workArgs, e, SyncDeviceType.PC))
                        {
                            //不成功通常是任务中断
                            args.Device.Disconnect();
                            resultNotify.message = "已停止";
                            return;
                        }
                    }
                    if (!DataUtil.IsEmptyCollection(filesOnlyInMobile))
                    {
                        if (!CollectAndNotify(filesOnlyInMobile, workArgs, e, SyncDeviceType.PHONE))
                        {
                            args.Device.Disconnect();
                            resultNotify.message = "已停止";
                            return;
                        }
                    }
                }
                args.Device.Disconnect();
            }
        }