private Task <object> GetFileContentAsync(IHttpRequest request)
        {
            var startTime = DateTime.Now;

            var method = $"{request.Method}::{request.Path}";

            Exception exception = null;

            try
            {
                string blobId = request.Parameters.id;

                if (!string.IsNullOrEmpty(blobId))
                {
                    var blobData = _blobStorage.GetBlobData(blobId);

                    if (blobData != null)
                    {
                        var fileResponse = new StreamHttpResponse(blobData.Data, blobData.Info.Type)
                        {
                            FileName         = blobData.Info.Name,
                            LastWriteTimeUtc = blobData.Info.Time
                        };

                        fileResponse.SetContentDispositionAttachment(request.Headers.UserAgent);

                        return(Task.FromResult <object>(fileResponse));
                    }
                }

                return(Task.FromResult <object>(HttpResponse.NotFound));
            }
            catch (Exception e)
            {
                exception = e;

                _log.Error(Resources.RequestProcessedWithException, new Dictionary <string, object> {
                    { "method", method }
                }, e);

                throw;
            }
            finally
            {
                _performanceLog.Log(method, startTime, exception);
            }
        }
        private Task <object> DownloadFile(IHttpRequest request)
        {
            string formString = request.Query.Form;

            dynamic form = null;

            if (!string.IsNullOrWhiteSpace(formString))
            {
                formString = Uri.UnescapeDataString(formString);
                form       = JsonObjectSerializer.Default.Deserialize(formString);
            }

            if (form != null)
            {
                string contentId = form.ContentId;

                if (!string.IsNullOrEmpty(contentId))
                {
                    var blobData = _blobStorage.GetBlobData(contentId);

                    if (blobData != null)
                    {
                        var fileResponse = new StreamHttpResponse(blobData.Data, blobData.Info.Type)
                        {
                            FileName         = blobData.Info.Name,
                            LastWriteTimeUtc = blobData.Info.Time
                        };

                        fileResponse.SetContentDispositionAttachment(request.Headers.UserAgent);

                        return(Task.FromResult <object>(fileResponse));
                    }
                }
            }

            return(Task.FromResult <object>(HttpResponse.NotFound));
        }
Esempio n. 3
0
        private void SetNancyStreamHttpResponse(NancyContext nancyContext, Response nancyResponse, StreamHttpResponse streamHttpResponse)
        {
            var lastWriteTimeUtc = streamHttpResponse.LastWriteTimeUtc;

            if (lastWriteTimeUtc != null)
            {
                var eTag = "\"" + lastWriteTimeUtc.Value.Ticks.ToString("x") + "\"";

                // Если файл не изменился, возвращается статус NotModified (304)
                if (CacheHelpers.ReturnNotModified(eTag, streamHttpResponse.LastWriteTimeUtc, nancyContext))
                {
                    nancyResponse.StatusCode  = HttpStatusCode.NotModified;
                    nancyResponse.Contents    = Response.NoBody;
                    nancyResponse.ContentType = null;
                    return;
                }

                nancyResponse.Headers["ETag"]          = eTag;
                nancyResponse.Headers["Last-Modified"] = lastWriteTimeUtc.Value.ToString("R");
            }

            var fileName = streamHttpResponse.FileName;

            // Если тип содержимого файла не задан, он определяется автоматически
            if (!string.IsNullOrEmpty(fileName) &&
                (string.IsNullOrEmpty(streamHttpResponse.ContentType) ||
                 string.Equals(streamHttpResponse.ContentType, HttpConstants.StreamContentType, StringComparison.OrdinalIgnoreCase)))
            {
                nancyResponse.ContentType = _mimeTypeResolver.GetMimeType(fileName);
            }

            var contentLength = streamHttpResponse.ContentLength;

            // Установка информация о размере файла
            if (contentLength != null)
            {
                nancyResponse.Headers["Content-Length"] = contentLength.ToString();
            }
        }