예제 #1
0
        private async Task ProcessGetSyncListCmd(CommandHeader cmdHeader)
        {
            var data = await NetworkHelperSequential.Read <GetSyncListCommandData>(_networkStream, cmdHeader.PayloadLength);

            var ret = new ServerResponseWithData <SyncInfo>();

            var session = SessionStorage.Instance.GetSession(data.SessionId);

            if (session == null)
            {
                ret.ErrorMsg = "Session does not exist";
            }
            else if (session.Expired)
            {
                ret.ErrorMsg = "Session has expired";
                //Log?.Invoke("Session has expired");
                //return ret;
            }
            else
            {
                Msg?.Invoke("Scanning local folder...");

                var syncDb = GetSyncDb(session.BaseDir, session.SyncDbDir, out var error);
                if (syncDb == null)
                {
                    ret.ErrorMsg = error;
                    Msg?.Invoke($"Failed to get sync db: {error}");
                }
                else
                {
                    var syncInfo = new SyncInfo();

                    PathHelpers.NormalizeRelative(data.Files);

                    Msg?.Invoke("Preparing sync list...");

                    foreach (var localFileInfo in syncDb.Files)
                    {
                        var remoteFileInfo = data.Files.FirstOrDefault(remoteFile =>
                                                                       remoteFile.RelativePath == localFileInfo.RelativePath);
                        if (remoteFileInfo == null)
                        {
                            if (localFileInfo.State != SyncFileState.Deleted)
                            {
                                syncInfo.ToDownload.Add(localFileInfo);
                            }
                        }
                        else
                        {
                            data.Files.Remove(remoteFileInfo);

                            switch (remoteFileInfo.State)
                            {
                            case SyncFileState.Deleted:
                                if (localFileInfo.State == SyncFileState.NotChanged ||
                                    localFileInfo.State == SyncFileState.Deleted)
                                {
                                    var filePath = Path.Combine(session.BaseDir, localFileInfo.RelativePath);
                                    if (File.Exists(filePath))
                                    {
                                        var movedFilePath = Path.Combine(session.RemovedDir, localFileInfo.RelativePath);
                                        var movedFileDir  = Path.GetDirectoryName(movedFilePath);
                                        if (movedFileDir == null)
                                        {
                                            throw new InvalidOperationException($"Unable to get '{movedFilePath}'s dir");
                                        }
                                        if (!Directory.Exists(movedFileDir))
                                        {
                                            Directory.CreateDirectory(movedFileDir);
                                        }

                                        File.Move(filePath, movedFilePath);
                                    }
                                }
                                else if (localFileInfo.State == SyncFileState.New)
                                {
                                    syncInfo.ToDownload.Add(localFileInfo);
                                }
                                else
                                {
                                    syncInfo.Conflicts.Add(localFileInfo);
                                }

                                break;

                            case SyncFileState.New:
                                syncInfo.Conflicts.Add(localFileInfo);
                                break;

                            case SyncFileState.Modified:
                                if (localFileInfo.State == SyncFileState.NotChanged)
                                {
                                    syncInfo.ToUpload.Add(localFileInfo);
                                }
                                else
                                {
                                    syncInfo.Conflicts.Add(remoteFileInfo);
                                }
                                break;

                            case SyncFileState.NotChanged:
                                if (localFileInfo.State == SyncFileState.Modified)
                                {
                                    syncInfo.ToDownload.Add(localFileInfo);
                                }
                                else if (localFileInfo.State == SyncFileState.Deleted)
                                {
                                    syncInfo.ToRemove.Add(remoteFileInfo);
                                }
                                else if (localFileInfo.State == SyncFileState.New)
                                {
                                    Debugger.Break();     // not possible
                                }

                                break;
                            }
                        }
                    }

                    foreach (var remoteFileInfo in data.Files.Where(x => x.State != SyncFileState.Deleted))
                    {
                        syncInfo.ToUpload.Add(remoteFileInfo);
                    }

                    ret.Data = syncInfo;

                    session.SyncDb = syncDb;
                }
            }

            await CommandHelper.WriteCommandResponse(_networkStream, Commands.GetSyncListCmd, ret);
        }
예제 #2
0
        private async Task ReturnSyncList(CommandHeader cmdHeader)
        {
            var remoteData = await NetworkHelper.Read <GetSyncListRequest>(_networkStream, cmdHeader.PayloadLength);

            var ret = new ServerResponseWithData <SyncInfo>();

            var session = SessionStorage.Instance.GetSession(remoteData.SessionId);

            if (session == null)
            {
                ret.ErrorMsg = "Session does not exist";
            }
            else if (session.Expired)
            {
                ret.ErrorMsg = "Session has expired";
                _log.AppendLine("Session has expired");
                //return ret;
            }
            else
            {
                try
                {
                    Msg?.Invoke("Scanning local folder...");

                    var syncDb = GetSyncDb(session.BaseDir, session.SyncDbDir, out var error);
                    if (syncDb == null)
                    {
                        ret.ErrorMsg = error;
                        Msg?.Invoke($"Failed to get sync db: {error}");
                    }
                    else
                    {
                        var syncInfo = new SyncInfo();

                        PathHelpers.NormalizeRelative(remoteData.Files);

                        Msg?.Invoke("Preparing sync list...");

                        foreach (var localFileInfo in syncDb.Files)
                        {
                            var remoteFileInfo = remoteData.Files.FirstOrDefault(remoteFile =>
                                                                                 remoteFile.RelativePath == localFileInfo.RelativePath);
                            if (remoteFileInfo == null)
                            {
                                if (localFileInfo.State != SyncFileState.Deleted)
                                {
                                    syncInfo.ToDownload.Add(localFileInfo);
                                }
                            }
                            else
                            {
                                remoteData.Files.Remove(remoteFileInfo);

                                switch (remoteFileInfo.State)
                                {
                                case SyncFileState.Deleted:
                                    if (localFileInfo.State == SyncFileState.NotChanged ||
                                        localFileInfo.State == SyncFileState.Deleted)
                                    {
                                        session.FileHelper.PrepareForRemove(localFileInfo.RelativePath);
                                    }
                                    else if (localFileInfo.State == SyncFileState.New)
                                    {
                                        syncInfo.ToDownload.Add(localFileInfo);
                                    }
                                    else
                                    {
                                        var fileExt = Path.GetExtension(localFileInfo.RelativePath);
                                        var newPath = Path.GetFileNameWithoutExtension(localFileInfo.RelativePath) + "_FromServer" + fileExt;
                                        localFileInfo.NewRelativePath = newPath;
                                        session.FileHelper.AddRename(localFileInfo.RelativePath, newPath);
                                        syncInfo.ToDownload.Add(localFileInfo);
                                    }

                                    break;

                                case SyncFileState.New:
                                    if (localFileInfo.HashStr != remoteFileInfo.HashStr)
                                    {
                                        var fileExt = Path.GetExtension(remoteFileInfo.RelativePath);
                                        var newPath = Path.GetFileNameWithoutExtension(remoteFileInfo.RelativePath) + "_FromClient" + fileExt;
                                        remoteFileInfo.NewRelativePath = newPath;
                                        syncInfo.ToUpload.Add(remoteFileInfo);

                                        fileExt = Path.GetExtension(localFileInfo.RelativePath);
                                        newPath = Path.GetFileNameWithoutExtension(localFileInfo.RelativePath) + "_FromServer" + fileExt;
                                        localFileInfo.NewRelativePath = newPath;
                                        session.FileHelper.AddRename(localFileInfo.RelativePath, newPath);
                                        syncInfo.ToDownload.Add(localFileInfo);
                                    }

                                    break;

                                case SyncFileState.Modified:
                                    if (localFileInfo.State == SyncFileState.NotChanged)
                                    {
                                        syncInfo.ToUpload.Add(localFileInfo);
                                    }
                                    else if (localFileInfo.State == SyncFileState.Modified ||
                                             localFileInfo.State == SyncFileState.New)
                                    {
                                        var fileExt = Path.GetExtension(remoteFileInfo.RelativePath);
                                        var newPath = Path.GetFileNameWithoutExtension(remoteFileInfo.RelativePath) + "_FromClient" + fileExt;
                                        remoteFileInfo.NewRelativePath = newPath;
                                        syncInfo.ToUpload.Add(remoteFileInfo);

                                        fileExt = Path.GetExtension(localFileInfo.RelativePath);
                                        newPath = Path.GetFileNameWithoutExtension(localFileInfo.RelativePath) + "_FromServer" + fileExt;
                                        localFileInfo.NewRelativePath = newPath;
                                        session.FileHelper.AddRename(localFileInfo.RelativePath, newPath);
                                        syncInfo.ToDownload.Add(localFileInfo);
                                    }
                                    else if (localFileInfo.State == SyncFileState.Deleted)
                                    {
                                        var fileExt = Path.GetExtension(remoteFileInfo.RelativePath);
                                        var newPath = Path.GetFileNameWithoutExtension(remoteFileInfo.RelativePath) + "_FromClient" + fileExt;
                                        remoteFileInfo.NewRelativePath = newPath;
                                        syncInfo.ToUpload.Add(remoteFileInfo);
                                    }

                                    break;

                                case SyncFileState.NotChanged:
                                    if (localFileInfo.State == SyncFileState.Modified)
                                    {
                                        syncInfo.ToDownload.Add(localFileInfo);
                                    }
                                    else if (localFileInfo.State == SyncFileState.Deleted)
                                    {
                                        syncInfo.ToRemove.Add(remoteFileInfo);
                                    }
                                    else if (localFileInfo.State == SyncFileState.New)
                                    {
                                        if (localFileInfo.HashStr == remoteFileInfo.HashStr)
                                        {
                                            Msg?.Invoke("Skip identical file, client already has one");
                                            localFileInfo.State = SyncFileState.NotChanged;
                                        }
                                        else
                                        {
                                            Debugger.Break(); // not possible
                                            throw new InvalidOperationException("Invalid server state");
                                        }
                                    }

                                    break;
                                }
                            }
                        }

                        foreach (var remoteFileInfo in remoteData.Files.Where(x => x.State != SyncFileState.Deleted))
                        {
                            syncInfo.ToUpload.Add(remoteFileInfo);
                        }

                        ret.Data = syncInfo;

                        session.SyncDb         = syncDb;
                        session.LastAccessTime = DateTime.Now;
                    }
                }
                catch (Exception e)
                {
                    ret.ErrorMsg = e.Message;
                }
            }

            await CommandHelper.WriteCommandResponse(_networkStream, Commands.GetSyncListCmd, ret);
        }