Ejemplo n.º 1
0
        private async Task <MemoryStream> GetPhoto(Attachment attachment, SignWith signWith, DateTime now)
        {
            // 1. Found in cache?
            if (this.imageCacheService.TryGet(attachment.Url, out MemoryStream stream))
            {
                return(stream);
            }

            // 2. Needs download
            KeyValuePair <string, TemporaryFile> pair = await this.neuronService.Contracts.GetAttachment(attachment.Url, signWith, Constants.Timeouts.DownloadFile);

            // If download has been cancelled any time _during_ download, stop here.
            if (this.loadPhotosTimestamp > now)
            {
                pair.Value.Dispose();
                return(null);
            }

            using (TemporaryFile file = pair.Value)
            {
                file.Reset();
                MemoryStream ms = new MemoryStream();
                await file.CopyToAsync(ms);

                // 3. Save to cache
                ms.Reset();
                await this.imageCacheService.Add(attachment.Url, ms);

                ms.Reset();
                return(ms);
            }
        }