Пример #1
0
        /// <summary>
        /// Handle both file and folder deletion, and ignore not found exception
        /// </summary>
        private void HandlingDeletion(OneDriveModel.OneDriveChange change, string wwwroot)
        {
            if (!change.IsDeleted)
            {
                return;
            }

            string fullPath = GetDestinationPath(wwwroot, change.Path);

            if (fullPath == null)
            {
                TraceMessage("Ignore folder {0}", change.Path);
                return;
            }

            try
            {
                if (FileSystemHelpers.FileExists(fullPath))
                {
                    FileSystemHelpers.DeleteFile(fullPath);
                    TraceMessage("Deleted file {0}", fullPath);
                    LogMessage(Resources.OneDriveDeletedFile, fullPath);
                }
                else if (FileSystemHelpers.DirectoryExists(fullPath))
                {
                    FileSystemHelpers.DeleteDirectorySafe(fullPath, ignoreErrors: false);
                    TraceMessage("Deleted directory {0}", fullPath);
                    LogMessage(Resources.OneDriveDeletedDirectory, fullPath);
                }
                else
                {
                    TraceMessage("Not found: {0}. Unknown item type, skip deletion!", fullPath);
                }
            }
            catch (DirectoryNotFoundException)
            {
                TraceMessage("Directory Not found: {0}. Skip deletion!", fullPath);
            }
            catch (FileNotFoundException)
            {
                TraceMessage("File Not found: {0}. Skip deletion!", fullPath);
            }
        }
Пример #2
0
        private static ChangesResult GetChanges(OneDriveModel.OneDriveItemCollection items, Dictionary <string, OneDriveModel.ItemInfo> ids, string rootUri)
        {
            ChangesResult result = new ChangesResult();

            foreach (var item in items.value)
            {
                ids[item.id] = new OneDriveModel.ItemInfo
                {
                    name     = item.name,
                    parentId = item.parentReference.id
                };

                var path = GetPath(ids, item);
                if (item.deleted != null)
                {
                    result.DeletionChanges.Add(new OneDriveModel.OneDriveChange {
                        Path = path, IsDeleted = true
                    });
                }
                else
                {
                    var change = new OneDriveModel.OneDriveChange
                    {
                        ContentUri      = string.Format(CultureInfo.InvariantCulture, "{0}/items/{1}", rootUri, item.id),
                        Path            = path,
                        IsFile          = item.file != null,
                        LastModifiedUtc = item.lastModifiedDateTime.ToUniversalTime()
                    };

                    if (change.IsFile)
                    {
                        result.FileChanges.Add(change);
                    }
                    else
                    {
                        result.DirectoryChanges.Add(change);
                    }
                }
            }

            return(result);
        }
Пример #3
0
        private async Task <bool> ProcessChangeWithRetry(OneDriveModel.OneDriveChange change, string accessToken, string wwwroot)
        {
            try
            {
                HandlingDeletion(change, wwwroot);
            }
            catch (Exception ex)
            {
                _tracer.TraceError(ex, "HandlingDeletion");
                return(false);
            }

            bool updated = await RetryHandler(() =>
            {
                return(HandlingUpdateOrCreate(change, wwwroot, accessToken));
            });

            // TODO: hande rename and move

            return(updated);
        }
Пример #4
0
        /// <summary>
        /// <para>Handle both file and folder create/update</para>
        /// <para>If local file timestamp the same as the file in server, assume file/folder unchanged, will skip action.</para>
        /// </summary>
        private async Task HandlingUpdateOrCreate(OneDriveModel.OneDriveChange change, string wwwroot, string accessToken)
        {
            if (change.IsDeleted)
            {
                return;
            }

            string fullPath = GetDestinationPath(wwwroot, change.Path);

            if (fullPath == null)
            {
                TraceMessage("Ignore folder {0}", change.Path);
                return;
            }

            if (change.IsFile)
            {
                // return "1/1/1601 12:00:00 AM" when file not existed
                DateTime lastWriteTime = FileSystemHelpers.GetLastWriteTimeUtc(fullPath);
                if (lastWriteTime != change.LastModifiedUtc)
                {
                    string parentDir = Path.GetDirectoryName(fullPath);
                    FileSystemHelpers.EnsureDirectory(parentDir);

                    var now     = DateTime.UtcNow;
                    var success = false;
                    try
                    {
                        using (var client = CreateHttpClient(accessToken))
                            using (HttpResponseMessage response = await client.GetAsync(change.ContentUri))
                            {
                                var fileResponse = await ProcessResponse <Dictionary <string, object> >("HandlingUpdateOrCreate", response);

                                string downloadUrl = (string)fileResponse["@content.downloadUrl"];

                                using (HttpResponseMessage downloadResponse = await client.GetAsync(downloadUrl))
                                    using (Stream stream = await downloadResponse.EnsureSuccessStatusCode().Content.ReadAsStreamAsync())
                                    {
                                        await WriteToFile(stream, fullPath);
                                    }
                            }

                        FileSystemHelpers.SetLastWriteTimeUtc(fullPath, change.LastModifiedUtc);

                        success = true;
                    }
                    finally
                    {
                        var elapse = (DateTime.UtcNow - now).TotalMilliseconds;
                        var result = success ? "successful" : "failed";
                        if (FileSystemHelpers.FileExists(fullPath))
                        {
                            TraceMessage("Update file {0} {1} ({2:0}ms)", fullPath, result, elapse);
                        }
                        else
                        {
                            TraceMessage("Create file {0} {1} ({2:0}ms)", fullPath, result, elapse);
                        }
                    }
                }
                else
                {
                    _tracer.Trace("Timestamp not changed. Skip updating file {0}", fullPath);
                }
            }
            else
            {
                if (FileSystemHelpers.DirectoryExists(fullPath))
                {
                    TraceMessage("Directory {0} exists, no action performed.", fullPath);
                }
                else
                {
                    TraceMessage("Creating directory {0} ...", fullPath);
                    FileSystemHelpers.CreateDirectory(fullPath);
                }
            }
        }