private bool GetLocalFile(FileInfoDto m, string saveFullDirectory, FilePositionCallback call = null)
        {
            bool   result     = false;
            string serverfile = this.GetLocalPath(Utils.Combine(m.Directory, m.Name));

            if (File.Exists(serverfile))
            {
                var serverinfo = new FileInfo(serverfile);
                this.OnFilePositionCallback(call, serverinfo.Length, 0);
                string localFullFileName = Utils.Combine(saveFullDirectory, m.Name);
                if (File.Exists(localFullFileName))
                {
                    var localInfo = new FileInfo(localFullFileName);
                    if (serverinfo.Length != localInfo.Length || serverinfo.CreationTime != localInfo.CreationTime || serverinfo.LastAccessTime != localInfo.LastAccessTime)
                    {
                        serverinfo.CopyTo(localFullFileName, true);
                    }
                }
                else
                {
                    serverinfo.CopyTo(localFullFileName, true);
                }
                this.OnFilePositionCallback(call, serverinfo.Length, serverinfo.Length);
                result = true;
            }

            return(result);
        }
        private bool OnFilePositionCallback(FilePositionCallback call, long length, long position)
        {
            bool result = true;

            if (call != null)
            {
                try { result = call(length, position); }
                catch {}
            }

            return(result);
        }
        public virtual bool SendFile(string localFullFileName, string remoteDirectory, string remoteName, FilePositionCallback call = null)
        {
            bool result = false;

            try
            {
                if (!string.IsNullOrEmpty(localFullFileName) && !string.IsNullOrEmpty(remoteName) &&
                    File.Exists(localFullFileName) && this.Client.IsLogin)
                {
                    var localinfo = new FileInfo(localFullFileName);
                    this.OnFilePositionCallback(call, localinfo.Length, 0);
                    if (this.Client.Host == "127.0.0.1" || this.Client.Host == "localhost")
                    {
                        result = this.AddFileInfo(localinfo, remoteDirectory, remoteName);
                        this.OnFilePositionCallback(call, localinfo.Length, localinfo.Length);
                    }
                    else
                    {
                        using (var fs = File.OpenRead(localFullFileName))
                        {
                            long fileLength = fs.Length;
                            CreateFileParamDto createDto = new CreateFileParamDto()
                            {
                                Directory     = remoteDirectory,
                                Name          = remoteName,
                                Length        = localinfo.Length,
                                CreationTime  = localinfo.CreationTime,
                                LastWriteTime = localinfo.LastAccessTime
                            };
                            var         getDto  = this.Client.Request <FileDataParamDto, CreateFileParamDto>(MsgCmd.CreateFile, createDto);
                            FileDataDto dataDto = new FileDataDto();
                            if (getDto != null)
                            {
                                while (getDto.Index < fileLength)
                                {
                                    dataDto.Index = getDto.Index;
                                    int  size = getDto.Size;
                                    long temp = fileLength - dataDto.Index;
                                    if (temp < size)
                                    {
                                        size = Convert.ToInt32(temp);
                                    }
                                    byte[] buffer = new byte[size];
                                    fs.Seek(dataDto.Index, SeekOrigin.Begin);
                                    dataDto.Length = fs.Read(buffer, 0, buffer.Length);
                                    dataDto.Data   = buffer;
                                    getDto         = this.Client.Request <FileDataParamDto, FileDataDto>(MsgCmd.UploadFileData, dataDto);
                                    bool isContinue = this.OnFilePositionCallback(call, fileLength, dataDto.Index);
                                    if (getDto == null || !isContinue)
                                    {
                                        break;
                                    }
                                }

                                this.Client.Request(MsgCmd.CreateFileCompleted);

                                if (getDto != null && getDto.Index >= fileLength)
                                {
                                    this.OnFilePositionCallback(call, fileLength, dataDto.Index);
                                    result = true;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtils.Error("【FileInfoClient.SendFile】", ex);
            }

            return(result);
        }
        public virtual bool GetFile(FileInfoDto m, string saveFullDirectory, string name, FilePositionCallback call = null)
        {
            bool result = false;
            bool isopen = false;

            try
            {
                if (m != null && m.Type == FileInfoType.File && !string.IsNullOrEmpty(saveFullDirectory) && this.Client.IsLogin)
                {
                    if (!Directory.Exists(saveFullDirectory))
                    {
                        Directory.CreateDirectory(saveFullDirectory);
                    }
                    if (this.Client.Host == "127.0.0.1" || this.Client.Host == "localhost")
                    {
                        result = this.GetLocalFile(m, saveFullDirectory, call);
                    }
                    else
                    {
                        long position = 0;
                        this.OnFilePositionCallback(call, m.Length, position);
                        var fs = this.CreateFile(m, saveFullDirectory, name, out position);
                        if (m.Length == position && fs == null)
                        {
                            this.OnFilePositionCallback(call, m.Length, position);
                            result = true;
                        }
                        else if (fs != null)
                        {
                            using (fs)
                            {
                                isopen = this.Client.Request <bool, int>(MsgCmd.OpenFile, m.Id);
                                if (isopen)
                                {
                                    FileDataParamDto getDto = new FileDataParamDto();
                                    while (position < m.Length)
                                    {
                                        bool isContinue = this.OnFilePositionCallback(call, m.Length, position);
                                        getDto.Index = position;
                                        getDto.Size  = MAX_FILE_DATA_SIZE;
                                        FileDataDto dataDto = this.Client.Request <FileDataDto, FileDataParamDto>(MsgCmd.GetFileData, getDto);
                                        if (dataDto != null && isContinue)
                                        {
                                            fs.Seek(position, SeekOrigin.Begin);
                                            if (dataDto.Length > 0 && dataDto.Data != null && dataDto.Length >= dataDto.Data.Length)
                                            {
                                                fs.Write(dataDto.Data, 0, dataDto.Length);
                                                fs.Flush();
                                            }
                                            position = position + dataDto.Length;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }

                                    this.Client.Request(MsgCmd.CloseFile);

                                    string saveFile  = Utils.Combine(saveFullDirectory, m.Name);
                                    string tempFile  = saveFile + FILE_TEMP_SUFFIX;
                                    string indexFile = saveFile + FILE_INDEX_SUFFIX;
                                    File.WriteAllText(indexFile, position.ToString() + " " + JsonUtils.Serialize(m), Encoding.UTF8);
                                    fs.Close();
                                    if (position >= m.Length)
                                    {
                                        this.OnFilePositionCallback(call, m.Length, position);
                                        if (File.Exists(saveFile))
                                        {
                                            File.Delete(saveFile);
                                        }
                                        File.Move(tempFile, Utils.Combine(saveFullDirectory, name));
                                        File.Delete(indexFile);
                                        result = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (isopen)
                {
                    this.Client.Request(MsgCmd.CloseFile);
                }
                LogUtils.Error("【FileInfoClient.GetFile】", ex);
            }

            return(result);
        }
        private void DownloadLocal(FileInfoDto m, string loaclDir)
        {
            if (m == null || m.Type == (int)FileInfoType.None)
            {
                return;
            }

            using (var client = IocUtils.Get <IFileInfoService>(new object[] { MainForm.Current.FileClient }))
            {
                Queue <FileInfoDto> queue = new Queue <FileInfoDto>();
                queue.Enqueue(m);
                string relDir = m.Directory;
                while (queue.Count > 0 && !this.isStop)
                {
                    FileInfoDto item     = queue.Dequeue();
                    string      dir      = Utils.Combine(loaclDir, Utils.GetRelative(item.Directory, relDir));
                    string      fullname = Utils.Combine(dir, item.Name);
                    string      remofull = Utils.Combine(item.Directory, item.Name);
                    if (item.Type == FileInfoType.Directory)
                    {
                        MainForm.Current.SetStatusText("下载目录:" + remofull);
                        if (!Directory.Exists(fullname))
                        {
                            Directory.CreateDirectory(fullname);
                            Directory.SetCreationTime(fullname, item.CreationTime);
                            Directory.SetLastAccessTime(fullname, item.LastWriteTime);
                        }
                        FileInfoPageParamDto param = new FileInfoPageParamDto()
                        {
                            Index    = 1,
                            Size     = 50,
                            ParentId = item.Id
                        };
                        var page = client.GetPageList(param);
                        int pos  = 0;
                        while (page != null && pos < page.TotalCount && !this.isStop)
                        {
                            if (page.List != null && page.List.Count > 0)
                            {
                                page.List.ForEach(q => { queue.Enqueue(q); pos++; });
                            }
                            param.Index = param.Index + 1;
                            page        = client.GetPageList(param);
                        }
                    }
                    else if (item.Type == FileInfoType.File)
                    {
                        string status             = "";
                        FilePositionCallback call = (len, pos) =>
                        {
                            string s = string.Format("下载文件: {0} ({1}/{2})", remofull, Utils.ToAutoUnit(pos), Utils.ToAutoUnit(len));
                            if (status != s)
                            {
                                status = s;
                                MainForm.Current.SetStatusText(status);
                            }

                            return(!this.isStop);
                        };
                        client.GetFile(item, dir, item.Name, call);
                    }
                }
            }

            MainForm.Current.SetStatusText("");
        }
        private void UploadServer(FileInfoDto m, string serverDir)
        {
            if (m == null || m.Type == (int)FileInfoType.None)
            {
                return;
            }

            using (var client = IocUtils.Get <IFileInfoService>(new object[] { MainForm.Current.FileClient }))
            {
                Queue <FileInfoDto> queue = new Queue <FileInfoDto>();
                queue.Enqueue(m);

                string name               = "";
                string status             = "";
                FilePositionCallback call = (len, pos) =>
                {
                    string s = string.Format("上传文件:{0} ({1}/{2})", name, Utils.ToAutoUnit(pos), Utils.ToAutoUnit(len));
                    if (status != s)
                    {
                        status = s;
                        MainForm.Current.SetStatusText(status);
                    }

                    return(!this.isStop);
                };

                string localBaseDir = m.Directory;

                while (queue.Count > 0 && !this.isStop)
                {
                    var    item      = queue.Dequeue();
                    string loaclFull = Utils.Combine(item.Directory, item.Name);
                    string dir       = Utils.Combine(serverDir, Utils.GetRelative(item.Directory, localBaseDir));
                    name = Utils.Combine(dir, item.Name);
                    if (item.Type == FileInfoType.File)
                    {
                        client.SendFile(loaclFull, dir, item.Name, call);
                    }
                    else if (item.Type == FileInfoType.Directory)
                    {
                        MainForm.Current.SetStatusText("上传目录:" + name);
                        client.SendDirectory(loaclFull, dir, item.Name);
                        var arr = Directory.GetFiles(loaclFull);
                        if (arr != null && arr.Length > 0)
                        {
                            foreach (var s in arr)
                            {
                                if (this.isStop)
                                {
                                    break;
                                }
                                FileInfoDto vm = new FileInfoDto()
                                {
                                    Type      = FileInfoType.File,
                                    Directory = Utils.GetParent(s),
                                    Name      = Utils.GetName(s)
                                };
                                queue.Enqueue(vm);
                            }
                        }

                        arr = Directory.GetDirectories(loaclFull);
                        if (arr != null && arr.Length > 0)
                        {
                            foreach (var s in arr)
                            {
                                if (this.isStop)
                                {
                                    break;
                                }
                                FileInfoDto vm = new FileInfoDto()
                                {
                                    Type      = FileInfoType.Directory,
                                    Directory = Utils.GetParent(s),
                                    Name      = Utils.GetName(s)
                                };
                                queue.Enqueue(vm);
                            }
                        }
                    }
                }
            }

            MainForm.Current.SetStatusText("");
        }