async Task Lookup(IOwinContext context) { Storage storage = _state.CreateStorage(); StorageContent content = await storage.Load(context.Request.Uri, CancellationToken.None); if (content == null) { await context.Response.WriteAsync("NOT FOUND"); context.Response.StatusCode = (int)HttpStatusCode.NotFound; } else { Stream stream = content.GetContentStream(); if (stream is MemoryStream) { byte[] data = ((MemoryStream)stream).ToArray(); await context.Response.WriteAsync(data); context.Response.ContentType = "application/json"; context.Response.ContentLength = data.Length; context.Response.StatusCode = (int)HttpStatusCode.OK; } else { context.Response.StatusCode = (int)HttpStatusCode.NotImplemented; } } }
public MemoryStorageEntry(StorageContent content, string contentType, string cacheControl) { var stream = new MemoryStream(); content.GetContentStream().CopyToAsync(stream); _data = stream.ToArray(); _contentType = contentType; _cacheControl = cacheControl; }
protected static bool SameData(byte[] data, StorageContent storageContent) { using (var dataStream = storageContent.GetContentStream()) using (var m = new MemoryStream()) { dataStream.CopyTo(m); var submittedArray = m.ToArray(); return(data.SequenceEqual(submittedArray)); } }
private static string GetContentString(StorageContent content) { if (content is StringStorageContent stringStorageContent) { return(stringStorageContent.Content); } using (var reader = new StreamReader(content.GetContentStream())) { return(reader.ReadToEnd()); } }
private static byte[] ToByteArray(StorageContent storageContent) { byte[] rawData; using (var stream = storageContent.GetContentStream()) { using (var buffer = new MemoryStream()) { stream.CopyTo(buffer); rawData = buffer.ToArray(); } } return(rawData); }
private static string GetContentString(StorageContent content) { var stringStorageContent = content as StringStorageContent; if (stringStorageContent != null) { return(stringStorageContent.Content); } using (var reader = new StreamReader(content.GetContentStream())) { return(reader.ReadToEnd()); } }
protected override Task OnSaveAsync( Uri resourceUri, StorageContent content, CancellationToken cancellationToken) { using (var stream = content.GetContentStream()) using (var reader = new StreamReader(stream)) { var json = reader.ReadToEnd(); var cursor = JsonConvert.DeserializeObject <Cursor>(json); CursorValue = cursor.Value; } return(Task.CompletedTask); }
protected override async Task OnSaveAsync(Uri resourceUri, StorageContent content, CancellationToken cancellationToken) { Content[resourceUri] = content; using (var memoryStream = new MemoryStream()) { var contentStream = content.GetContentStream(); await contentStream.CopyToAsync(memoryStream); contentStream.Position = 0; ContentBytes[resourceUri] = memoryStream.ToArray(); } ListMock[resourceUri] = CreateStorageListItem(resourceUri); }
public StorageContent Rewrite( Uri primaryStorageBaseUri, Uri primaryResourceUri, Uri secondaryStorageBaseUri, Uri secondaryResourceUri, StorageContent content) { JTokenReader tokenReader = null; var storageContent = content as JTokenStorageContent; if (storageContent != null) { // Production code should always have JTokenStorageContent tokenReader = storageContent.Content.CreateReader() as JTokenReader; } else { // Test code may end up here - we need to make sure we have a JTokenReader at our disposal using (var streamReader = new StreamReader(content.GetContentStream())) { using (var jsonTextReader = new JsonTextReader(streamReader)) { tokenReader = JToken.Load(jsonTextReader).CreateReader() as JTokenReader; } } } if (tokenReader != null) { // Create a rewriting reader var rewritingReader = new RegistrationBaseUrlRewritingJsonReader(tokenReader, _replacements.Union(new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>(primaryStorageBaseUri.ToString(), secondaryStorageBaseUri.ToString()), new KeyValuePair <string, string>(GetParentUriString(primaryResourceUri), GetParentUriString(secondaryResourceUri)) }).ToList()); // Clone the original token (passing through our intercepting reader) var updatedJson = JToken.Load(rewritingReader); // Create new content return(new JTokenStorageContent(updatedJson, content.ContentType, content.CacheControl)); } return(content); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { StorageContent content = await _storage.Load(request.RequestUri, cancellationToken); if (content == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } else { HttpContent httpContent = new StreamContent(content.GetContentStream()); httpContent.Headers.ContentType = new MediaTypeHeaderValue(content.ContentType); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = httpContent }); } }
protected internal override async Task Execute() { await Extend(TimeSpan.FromMinutes(10)); // Set defaults TargetStorageAccount = TargetStorageAccount ?? Config.Storage.Primary; // Check required payload ArgCheck.Require(TargetBaseAddress, "TargetBaseAddress"); ArgCheck.Require(CatalogIndexUrl, "CatalogIndexUrl"); ArgCheck.Require(CdnBaseAddress, "CdnBaseAddress"); ArgCheck.Require(GalleryBaseAddress, "GalleryBaseAddress"); // Clean input data if (!TargetBaseAddress.EndsWith("/")) { TargetBaseAddress += "/"; } var resolverBaseUri = new Uri(TargetBaseAddress); CdnBaseAddress = CdnBaseAddress.TrimEnd('/'); GalleryBaseAddress = GalleryBaseAddress.TrimEnd('/'); // Load Storage NuGet.Services.Metadata.Catalog.Persistence.Storage storage; string storageDesc; if (String.IsNullOrEmpty(TargetLocalDirectory)) { ArgCheck.Require(TargetStorageAccount, "ResolverStorage"); ArgCheck.Require(TargetPath, "ResolverPath"); var dir = StorageHelpers.GetBlobDirectory(TargetStorageAccount, TargetPath); storage = new AzureStorage(dir, resolverBaseUri); storageDesc = dir.Uri.ToString(); } else { storage = new FileStorage(TargetBaseAddress, TargetLocalDirectory); storageDesc = TargetLocalDirectory; } Uri cursorUri = new Uri(resolverBaseUri, "meta/cursor.json"); Log.LoadingCursor(cursorUri.ToString()); StorageContent content = await storage.Load(cursorUri); CollectorCursor lastCursor; if (content == null) { lastCursor = CollectorCursor.None; } else { JToken cursorDoc = JsonLD.Util.JSONUtils.FromInputStream(content.GetContentStream()); lastCursor = (CollectorCursor)(cursorDoc["http://schema.nuget.org/collectors/resolver#cursor"].Value <DateTime>("@value")); } Log.LoadedCursor(lastCursor.Value); ResolverCollector collector = new ResolverCollector(storage, 200) { ContentBaseAddress = CdnBaseAddress, GalleryBaseAddress = GalleryBaseAddress }; collector.ProcessedCommit += cursor => { ExtendIfNeeded(TimeSpan.FromMinutes(10)).Wait(); if (!Equals(cursor, lastCursor)) { StoreCursor(storage, cursorUri, cursor).Wait(); lastCursor = cursor; } }; Log.EmittingResolverBlobs( CatalogIndexUrl.ToString(), storageDesc, CdnBaseAddress, GalleryBaseAddress); lastCursor = (DateTime)await collector.Run( new Uri(CatalogIndexUrl), lastCursor); Log.EmittedResolverBlobs(); await this.Enqueue(this.Invocation.Job, this.Invocation.Payload, TimeSpan.FromSeconds(3), this.Invocation.JobInstanceName); }