protected StorageSimulatorItem CreateTree(PathInfo pathInfo) { StorageSimulatorItem dir = this.Root; StorageSimulatorItem child = this.Root; var currentUri = new Uri(string.Format("{0}{1}@{2}", this.ChosenProtocolScheme, pathInfo.Container, this.Account.Host)); if (!dir.Items.TryGetValue(pathInfo.Container, out dir)) { string containerUri = currentUri.Scheme + "://" + pathInfo.Container + "@" + currentUri.Host; dir = new StorageSimulatorItem(new Uri(containerUri), pathInfo.Container); this.Root.Items.Add(pathInfo.Container, dir); } foreach (string pathPart in pathInfo.PathParts) { string uri = this.FixUriEnding(currentUri); currentUri = new Uri(uri + "/" + pathPart); if (!dir.Items.TryGetValue(pathPart, out child)) { child = new StorageSimulatorItem(currentUri, pathPart); dir.Items.Add(pathPart, child); dir = child; } else { dir = child; } } return(dir); }
public Task <bool> Exists(Uri path) { var pathInfo = new PathInfo(path); this.AssertIsValidWabsUri(pathInfo); StorageSimulatorItem result = this.GetItem(pathInfo); return(Task.FromResult(result.IsNotNull())); }
public void Delete(Uri path) { var pathInfo = new PathInfo(path); if (pathInfo.Path.IsNullOrEmpty()) { throw new InvalidOperationException("An attempt was made to delete a container. Containers can not be deleted via this API."); } this.AssertIsValidWabsUri(pathInfo); StorageSimulatorItem item = this.GetItem(pathInfo, true); if (item.IsNotNull() && item.Items.ContainsKey(pathInfo.PathParts[pathInfo.PathParts.Length - 1])) { item.Items.Remove(pathInfo.PathParts[pathInfo.PathParts.Length - 1]); } }
public Task <Stream> Read(Uri path) { var pathInfo = new PathInfo(path); this.AssertIsValidWabsUri(pathInfo); StorageSimulatorItem item = this.GetItem(pathInfo); if (item.IsNull()) { throw new InvalidOperationException("Attempt to read an item that was not present in the container."); } Stream stream = Help.SafeCreate <MemoryStream>(); stream.Write(item.Data, 0, item.Data.Length); stream.Position = 0; return(Task.FromResult(stream)); }
public Task Write(Uri path, Stream stream) { var pathInfo = new PathInfo(path); if (pathInfo.Path.IsNullOrEmpty()) { throw new InvalidOperationException("An attempt was made write but no path was provided. Data can not be written without a path."); } this.AssertIsValidWabsUri(pathInfo); StorageSimulatorItem item = this.CreateTree(pathInfo); using (var mem = new MemoryStream()) { stream.CopyTo(mem); item.Data = new byte[mem.Length]; mem.Position = 0; mem.Read(item.Data, 0, item.Data.Length); } return(Task.Delay(0)); }
public Task <IEnumerable <Uri> > List(Uri path, bool recursive) { var items = new List <Uri>(); var queue = new Queue <StorageSimulatorItem>(); var pathInfo = new PathInfo(path); this.AssertIsValidWabsUri(pathInfo); StorageSimulatorItem item = this.GetItem(pathInfo, pathInfo.Path.IsNullOrEmpty()); if (item.IsNotNull()) { queue.Enqueue(item); while (queue.Count > 0) { item = queue.Remove(); queue.AddRange(item.Items.Values); items.Add(item.Path); } } return(Task.FromResult((IEnumerable <Uri>)items)); }
protected StorageSimulatorItem GetItem(PathInfo pathInfo, bool parent = false) { StorageSimulatorItem dir = this.Root; this.Root.Items.TryGetValue(pathInfo.Container, out dir); string[] pathParts = pathInfo.PathParts; if (parent) { if (pathParts.Length > 0) { pathParts = pathParts.Take(pathParts.Length - 1).ToArray(); } } if (pathParts.Length == 0) { return(dir); } int loc = 0; while (dir.IsNotNull() && dir.Items.TryGetValue(pathParts[loc], out dir) && loc < pathParts.Length) { if (loc == pathParts.Length - 1) { return(dir); } if (dir.IsNull()) { return(null); } loc++; } return(null); }
protected StorageSimulatorItem CreateTree(PathInfo pathInfo) { StorageSimulatorItem dir = this.Root; StorageSimulatorItem child = this.Root; var currentUri = new Uri(string.Format("{0}{1}@{2}", this.ChosenProtocolScheme, pathInfo.Container, this.Account.Host)); if (!dir.Items.TryGetValue(pathInfo.Container, out dir)) { string containerUri = currentUri.Scheme + "://" + pathInfo.Container + "@" + currentUri.Host; dir = new StorageSimulatorItem(new Uri(containerUri), pathInfo.Container); this.Root.Items.Add(pathInfo.Container, dir); } foreach (string pathPart in pathInfo.PathParts) { string uri = this.FixUriEnding(currentUri); currentUri = new Uri(uri + "/" + pathPart); if (!dir.Items.TryGetValue(pathPart, out child)) { child = new StorageSimulatorItem(currentUri, pathPart); dir.Items.Add(pathPart, child); dir = child; } else { dir = child; } } return dir; }