예제 #1
0
        public ActionResult File(string location, int id)
        {
            var metadata = this.db.FileMetadatas.SingleOrDefault(f => f.Id == id);

            if (metadata != null)
            {
                var containerName = location.Split("\\".ToCharArray(), 2).FirstOrDefault();

                if (containerName == metadata.ContainerName)
                {
                    var fileName = metadata.SourceFileName;
                    var stream   = this.storage.DownloadFileFromStorage(containerName, metadata.BlobName);

                    if (stream == null)
                    {
                        return(new StatusCodeResult(HttpStatusCode.NotFound));
                    }

                    using (stream)
                        using (var memoryStream = new MemoryStream())
                        {
                            stream.CopyTo(memoryStream);
                            return(this.File(memoryStream.ToArray(), MimeTypesHelper.GetContentType(fileName), fileName));
                        }
                }
            }

            return(new StatusCodeResult(HttpStatusCode.NotFound));
        }
예제 #2
0
        public ActionResult File(int id)
        {
            var dbPatientFile = this.db.PatientFiles.Include("FileMetadata").First(m => m.Id == id);

            DateTime modifiedSince;
            bool     hasModifiedSince = DateTime.TryParse(this.Request.Headers.Get("If-Modified-Since"), out modifiedSince);

            // File in the database never change after they are uploaded.
            // They can be deleted though.
            if (dbPatientFile == null)
            {
                return(new StatusCodeResult(HttpStatusCode.NotFound));
            }

            // If the request suggests that the client has any previous version of the file,
            // then just indicate it hasn't changed, since these images never change.
            if (hasModifiedSince)
            {
                return(new StatusCodeResult(HttpStatusCode.NotModified));
            }

            // Caching file forever.
            this.HttpContext.Response.Cache.SetCacheability(HttpCacheability.Public);
            this.HttpContext.Response.Cache.SetMaxAge(DebugConfig.IsDebug ? TimeSpan.FromMinutes(1) : TimeSpan.MaxValue);
            this.HttpContext.Response.Cache.SetLastModified(this.datetimeService.UtcNow);

            var metadata = dbPatientFile.FileMetadata;

            if (metadata != null)
            {
                var fileName = metadata.SourceFileName;
                var stream   = this.storage.DownloadFileFromStorage(metadata.ContainerName, metadata.BlobName);

                if (stream == null)
                {
                    return(new StatusCodeResult(HttpStatusCode.NotFound));
                }

                using (stream)
                    using (var memoryStream = new MemoryStream())
                    {
                        stream.CopyTo(memoryStream);
                        return(this.File(memoryStream.ToArray(), MimeTypesHelper.GetContentType(fileName), fileName));
                    }
            }

            return(new StatusCodeResult(HttpStatusCode.NotFound));
        }