示例#1
0
        private async Task <bool> UploadFile(IStorageFile localFile, string path)
        {
            var result = false;
            var cts    = new CancellationTokenSource();

            CachedFileManager.DeferUpdates(localFile);
            try
            {
                using (var stream = await localFile.OpenAsync(FileAccessMode.Read))
                {
                    var targetStream = stream.AsStreamForRead();

                    IProgress <WebDavProgress> progress = new Progress <WebDavProgress>(ProgressHandler);
                    result = await _client.Upload(path, targetStream, localFile.ContentType, progress, cts.Token);
                }

                ToastNotificationService.ShowSyncedFileNotification(localFile.Name);
            }
            catch (ResponseError e2)
            {
                ResponseErrorHandlerService.HandleException(e2);
            }

            // Let Windows know that we're finished changing the file so
            // the other app can update the remote version of the file.
            // Completing updates may require Windows to ask for user input.
            await CachedFileManager.CompleteUpdatesAsync(localFile);

            return(result);
        }
示例#2
0
        public async Task <bool> StartSync()
        {
            if (!SyncDbUtils.LockFolderSyncInfo(folderSyncInfo))
            {
                return(false);
            }

            try
            {
                client = await ClientService.GetClient();

                if (client == null)
                {
                    // ERROR
                    throw new Exception("Error creating webdav client");
                }

                int changedCount = 0;
                List <SyncInfoDetail> oldList = SyncDbUtils.GetAllSyncInfoDetails(folderSyncInfo);
                Debug.WriteLine("Sid List before Sync: ");

                foreach (SyncInfoDetail detail in oldList)
                {
                    Debug.WriteLine("Detail: " + detail.ToString());
                }

                var sid        = SyncDbUtils.GetSyncInfoDetail(resourceInfo, folderSyncInfo);
                var errorCount = 0;

                try
                {
                    if (sid == null)
                    {
                        sid = new SyncInfoDetail(folderSyncInfo)
                        {
                            Path     = resourceInfo.Path,
                            FilePath = baseFolder.Path,
                        };

                        SyncDbUtils.SaveSyncInfoDetail(sid);
                    }
                    else
                    {
                        sidList.Remove(sid);
                        sid.Error = null;
                    }

                    changedCount = await SyncFolder(resourceInfo, baseFolder);

                    foreach (SyncInfoDetail detail in oldList)
                    {
                        if (!sidList.Contains(detail) && detail.FilePath.StartsWith(baseFolder.Path))
                        {
                            // The items left here must have been deleted both remotely and locally so the sid is obsolete.
                            SyncDbUtils.DeleteSyncInfoDetail(detail, false);
                            changedCount++;
                        }
                    }
                    errorCount = SyncDbUtils.GetErrorConflictCount(folderSyncInfo);
                }
                catch (Exception e)
                {
                    sid.Error = e.Message;
                    errorCount++;
                }

                SyncDbUtils.SaveSyncInfoDetail(sid);
                List <SyncInfoDetail> newSidList = SyncDbUtils.GetAllSyncInfoDetails(folderSyncInfo);
                Debug.WriteLine("Sid List after Sync: ");

                foreach (SyncInfoDetail detail in newSidList)
                {
                    Debug.WriteLine("Detail: " + detail.ToString());
                }

                ToastNotificationService.ShowSyncFinishedNotification(folderSyncInfo.Path, changedCount, errorCount);
                return(errorCount == 0);
            }
            finally
            {
                SyncDbUtils.UnlockFolderSyncInfo(folderSyncInfo);
            }
        }
        public async Task <bool> StartSync()
        {
            if (!SyncDbUtils.LockFolderSyncInfo(_folderSyncInfo))
            {
                return(false);
            }

            try
            {
                _client = await ClientService.GetClient();

                if (_client == null)
                {
                    // ERROR
                    throw new NullReferenceException(_resourceLoader.GetString(ResourceConstants.SyncService_Error_CannotCreateClient));
                }

                var changedCount = 0;
                var oldList      = SyncDbUtils.GetAllSyncInfoDetails(_folderSyncInfo);
                Debug.WriteLine("Sid List before Sync: ");

                foreach (var detail in oldList)
                {
                    Debug.WriteLine("Detail: " + detail);
                }

                var sid        = SyncDbUtils.GetSyncInfoDetail(_resourceInfo, _folderSyncInfo);
                var errorCount = 0;

                try
                {
                    if (sid == null)
                    {
                        sid = new SyncInfoDetail(_folderSyncInfo)
                        {
                            Path     = _resourceInfo.Path,
                            FilePath = _baseFolder.Path,
                        };

                        SyncDbUtils.SaveSyncInfoDetail(sid);
                    }
                    else
                    {
                        _sidList.Remove(sid);
                        sid.Error = null;
                    }

                    changedCount = await SyncFolder(_resourceInfo, _baseFolder);

                    foreach (var detail in oldList)
                    {
                        if (_sidList.Contains(detail) || !detail.FilePath.StartsWith(_baseFolder.Path))
                        {
                            continue;
                        }
                        // The items left here must have been deleted both remotely and locally so the sid is obsolete.
                        SyncDbUtils.DeleteSyncInfoDetail(detail, false);
                        changedCount++;
                    }
                    errorCount = SyncDbUtils.GetErrorConflictCount(_folderSyncInfo);
                }
                catch (Exception e)
                {
                    sid.Error = string.Format(_resourceLoader.GetString("UnexpectedException"), e.Message);
                    errorCount++;
                }

                SyncDbUtils.SaveSyncInfoDetail(sid);
                var newSidList = SyncDbUtils.GetAllSyncInfoDetails(_folderSyncInfo);
                Debug.WriteLine("Sid List after Sync: ");

                foreach (var detail in newSidList)
                {
                    Debug.WriteLine("Detail: " + detail);
                }

                ToastNotificationService.ShowSyncFinishedNotification(_folderSyncInfo.Path, changedCount, errorCount);
                return(errorCount == 0);
            }
            finally
            {
                SyncDbUtils.UnlockFolderSyncInfo(_folderSyncInfo);
            }
        }