Exemplo n.º 1
0
        private async Task AddToRepository(Tuple <ObjectViewResponse, ObjectViewResponse> currentLevelObject)
        {
            var parent  = currentLevelObject.Item2;
            var current = currentLevelObject.Item1;

            var newFile = new FileEntity
            {
                Crc32    = current.Crc32,
                Name     = current.Name,
                IsFolder = current.IsFolder,
                Token    = AccountObject.Token,
                Size     = current.Size,
                UploadId = current.UploadId
            };

            if (parent != null)
            {
                newFile.Parent = await FileRepository.FindAsync(AccountObject.Token, parent.UploadId);
            }

            var file = await FileRepository.FindAsync(newFile);

            if (file != null)
            {
                if (file.UploadId == newFile.UploadId)
                {
                    //the same file scanned twice, should never happened, but who knows
                    file.Status = FileStatus.Conflict;
                }
                else
                {
                    newFile.Name = await GetIncrementName(newFile);

                    newFile.Status = FileStatus.RenameRequired;
                    await FileRepository.AddAsync(newFile);
                }
            }
            else
            {
                newFile.Status = FileStatus.RemotelyCreated;
                await FileRepository.AddAsync(newFile);
            }
        }
Exemplo n.º 2
0
        private async Task ProcessCreated(WebSocketPackage webSocketPackage)
        {
            var file = await FileRepository.FindAsync(AccountObject.Token, webSocketPackage.UploadId);

            if (file != null)
            {
                return;
            }

            var remoteFile = await Connection.GetUploadInfoAsync(AccountObject.Token, webSocketPackage.UploadId);

            if (remoteFile == null || remoteFile.UploadList.Length == 0)
            {
                return;
            }

            file = new FileEntity
            {
                UploadId = webSocketPackage.UploadId,
                Crc32    = remoteFile.UploadList[0].Crc32,
                Size     = remoteFile.UploadList[0].Size,
                IsFolder = remoteFile.UploadList[0].IsFolder,
                Name     = remoteFile.UploadList[0].Name,
                Token    = AccountObject.Token,
                Status   = FileStatus.RemotelyCreated
            };

            if (webSocketPackage.FolderId.HasValue && webSocketPackage.FolderId != 0)
            {
                var remoteParent = await FileRepository.FindAsync(AccountObject.Token, webSocketPackage.FolderId);

                if (remoteParent == null)
                {
                    //there is no parent folder in the DB, let's wait for another iteration
                    PackagesBuffer.Add(webSocketPackage);
                    return;
                }

                file.ParentId = remoteParent.Id;
            }

            var oldFile = await FileRepository.FindAsync(file);

            if (oldFile != null)
            {
                if (file.UploadId == oldFile.UploadId)
                {
                    //the same file scanned twice, should never happened, but who knows
                    oldFile.Status = FileStatus.Conflict;
                    return;
                }

                file.Name = await GetIncrementName(file);

                file.Status = FileStatus.RenameRequired;
                await FileRepository.AddAsync(file);
            }
            else
            {
                //detect moved files
                var deleted = await FileRepository.FindAsync(x =>
                                                             x.Size == file.Size &&
                                                             x.Status == FileStatus.RemotelyDeleted &&
                                                             x.Name == file.Name &&
                                                             x.Crc32 == file.Crc32 &&
                                                             x.UploadId != file.UploadId);

                if (deleted != null)
                {
                    deleted.Status  = FileStatus.RemotelyMovedFrom;
                    deleted.MovedId = file.Id;
                    file.Status     = FileStatus.RemotelyMovedTo;
                    file.MovedId    = file.Id;
                }
                else
                {
                    file.Status = FileStatus.RemotelyCreated;
                }

                await FileRepository.AddAsync(file);
            }
        }
Exemplo n.º 3
0
        private async Task CreateFilesOneLevel(IEnumerable <string> relativePathList, int level)
        {
            foreach (var relativePath in relativePathList.Where(x =>
                                                                x.Split(Path.DirectorySeparatorChar).Length == level))
            {
                var file = await FileRepository.FindAsync(AccountObject.Token, relativePath);

                var fullName = Path.Combine(AccountObject.Path, relativePath);
                var fileInfo = GetFileInfo(fullName);

                //file already exists
                if (file != null)
                {
                    //file exists on the server
                    if (file.UploadId.HasValue)
                    {
                        //file was changed when the sync was not running
                        if (Conflicted(file, fileInfo))
                        {
                            await FileRepository.UpdateBranchStatusAsync(file, FileStatus.Conflict);
                        }
                        else
                        {
                            //no conflicts, already synchronized, update
                            file.LastWrite = fileInfo.LastWriteTime.Ticks;
                            file.Status    = FileStatus.Synchronized;
                        }
                    }
                    else
                    {
                        //it is new record, ensure it has correct status
                        file.Status = FileStatus.LocallyCreated;
                    }

                    continue;
                }

                file = new FileEntity
                {
                    Token     = AccountObject.Token,
                    Name      = Path.GetFileName(relativePath),
                    Status    = FileStatus.LocallyCreated,
                    Hash      = relativePath,
                    LastWrite = fileInfo.LastWriteTime.Ticks,
                    IsFolder  = fileInfo.Attributes.HasFlag(FileAttributes.Directory)
                };

                //not root
                if (level > 1)
                {
                    var parentRelativePath = Path.GetDirectoryName(relativePath);
                    var parent             = await FileRepository.FindAsync(AccountObject.Token, parentRelativePath);

                    if (parent == null)
                    {
                        //no parent in DB, let's try next time
                        AddToBuffer(relativePath);
                        continue;
                    }

                    if (Buffer.Contains(relativePath))
                    {
                        Buffer.Remove(relativePath);
                    }

                    file.Parent = parent;
                }

                await FileRepository.AddAsync(file);
            }
        }