AzureWebFarm.Services.SyncService.SyncLocalToBlob C# (CSharp) Method

SyncLocalToBlob() public method

public SyncLocalToBlob ( ) : void
return void
        public void SyncLocalToBlob()
        {
            var seen = new HashSet<string>();

            foreach (var thing in EnumerateLocalEntries())
            {
                var path = thing.Item1;
                var entry = thing.Item2;

                seen.Add(path);

                if (!_entries.ContainsKey(path) || _entries[path].LocalLastModified < entry.LocalLastModified)
                {
                    var newBlob = _container.GetBlobReference(path);
                    if (entry.IsDirectory)
                    {
                        newBlob.Metadata["IsDirectory"] = bool.TrueString;
                        newBlob.UploadByteArray(new byte[0]);
                    }
                    else
                    {
                        _logger.InfoFormat("[Local Storage => Blob] - Uploading file: '{0}'", path);
                        using (var stream = File.Open(Path.Combine(_localTempPath, path), FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
                        {
                            newBlob.Metadata["IsDirectory"] = bool.FalseString;
                            newBlob.UploadFromStream(stream);
                        }
                    }

                    entry.CloudLastModified = newBlob.Properties.LastModifiedUtc;
                    _entries[path] = entry;
                }
            }

            foreach (var path in _entries.Keys.Where(k => !seen.Contains(k)).ToArray())
            {
                // Try deleting all the unused files and directories
                try
                {
                    if (_entries[path].IsDirectory)
                    {
                        Directory.Delete(path);
                    }
                    else
                    {
                        File.Delete(path);
                    }
                }
                catch (Exception e)
                {
                    _logger.Warn("[Local Storage => Blob] - Error deleting unused file: {0}", e);
                }

                _entries.Remove(path);
            }
        }