示例#1
0
        private async Task operationDownloadFileAsync(string filename, string CRC, bool ignoreCRCCheck = false)
        {
            Debug.WriteLine("Operation Download");
            //vars to prevent processing when FileWatcher triggers this copy/modify event
            _operationDownloadedFile     = filename;
            _operationDownloadedFileTime = DateTime.Now;

            bool newFile = true;

            if (_fileManager.Exists(filename))
            {
                //If File exists and has the same Hash no download is needed
                if (!ignoreCRCCheck && !String.IsNullOrEmpty(CRC) && CRC.Equals(_fileManager.GetHash(filename)))
                {
                    return;
                }

                //if(!ignoreSyncStatus && _folderSyncState.LocalFile(filename).CurrentStatus()!=FileStatusType.Synced)
                //{
                //    //Server has file updated but local file is not Synced
                //    userConfirmationCall(new SyncConflict() {Filename=filename, CRC=CRC, ConflictType=SyncConflictType.NewerVersionRemoteAndLocalVersionChanged });
                //    return;
                //}

                newFile = false;
            }

            byte[] fileContent = new byte[0];

            try
            {
                fileContent = await _serverManager.DownloadFileAsync(filename);
            }
            catch (HttpRequestException e)
            {
                userNotification(filename, newFile ? SyncNotification.NewDownloadFail : SyncNotification.UpdateDownloadFail);
                return;
            }
            try
            {
                Debug.WriteLine("Operation Download - before saveFile");
                _fileManager.TryToSaveFile(filename, fileContent);
                Debug.WriteLine("Operation Download - after saveFile");
            }
            catch
            {
                userNotification(filename, newFile ? SyncNotification.ErrorWritingFile : SyncNotification.ErrorOverwritingFile);
                return;
            }
            Debug.WriteLine("Operation Download - before _folderSyncState.NewFileLocalAndServer");
            _folderSyncState.NewFileLocalAndServer(filename, CRC);
            Debug.WriteLine("Operation Download - after _folderSyncState.NewFileLocalAndServer");
            userNotification(filename, newFile ? SyncNotification.SuccessfulNewDownload : SyncNotification.SuccessfulUpdateDownload);
        }