Пример #1
0
        public override async Task <string> Handle(HttpRequest request, HttpResponse response)
        {
            Log.LogDebug("HandleStream get file");

            string file = string.Empty;

            if (request.Query.ContainsKey(string.Empty))
            {
                file = request.Query[string.Empty][0];
                file = HttpUtility.UrlDecode(file);
            }

            if (ProgramSettings.Settings.Dlna && !string.IsNullOrEmpty(file))
            {
                try {
                    var fileRequest = await FileRequest.Create(request, file);

                    if (fileRequest.File.Exists && Tools.Tools.CheckAccessPath(fileRequest.File.FullName))
                    {
                        var data = await File.ReadAllBytesAsync(fileRequest.File.FullName);

                        string source = string.Empty;

                        var items = GetItems(data, ref source);

                        if (!string.IsNullOrEmpty(source))
                        {
                            return(source);
                        }
                        else
                        {
                            return(ResponseManager.CreateResponse(items));
                        }
                    }
                    else
                    {
                        Log.LogDebug("File not found: {0}", file);
                        response.StatusCode = (int)HttpStatusCode.NotFound;
                    }
                } catch (Exception exception) {
                    Log.LogError(exception);
                    response.StatusCode = (int)HttpStatusCode.NotFound;
                }
            }
            else
            {
                Log.LogDebug("Incorrect parameter: {0}", file);

                response.StatusCode = (int)HttpStatusCode.NoContent;
            }

            return(null);
        }
Пример #2
0
        public override async Task <Stream> Handle(HttpRequest request, HttpResponse response)
        {
            Log.LogDebug("HandleStream get file");

            string file = string.Empty;

            if (request.Query.ContainsKey(string.Empty))
            {
                file = request.Query[string.Empty][0];
                file = HttpUtility.UrlDecode(file);
            }

            if (ProgramSettings.Settings.Dlna && !string.IsNullOrEmpty(file))
            {
                try {
                    var fileRequest = await FileRequest.Create(request, file);

                    if (fileRequest.File.Exists && Tools.Tools.CheckAccessPath(fileRequest.File.FullName))
                    {
                        response.Headers.Add("Accept-Ranges", "bytes");
                        if (ValidateRanges(response, fileRequest) &&
                            ValidateModificationDate(request, response, fileRequest) &&
                            ValidateEntityTag(request, response, fileRequest))
                        {
                            response.Headers.Add("Last-Modified", fileRequest.File.LastWriteTime.ToString("r"));
                            response.Headers["Etag"] = fileRequest.EntityTag;

                            if (!fileRequest.RangeRequest)
                            {
                                response.ContentLength = fileRequest.File.Length;
                                response.ContentType   = fileRequest.ContentType;
                                response.StatusCode    = (int)HttpStatusCode.OK;

                                var fileStream = fileRequest.File.OpenRead();
                                fileStream.Position = 0;
                                return(fileStream);
                            }
                            else
                            {
                                response.ContentLength = fileRequest.GetContentLength();

                                if (!fileRequest.MultipartRequest)
                                {
                                    response.Headers.Add("Content-Range",
                                                         $"bytes {fileRequest.RangesStartIndexes[0]}-{fileRequest.RangesEndIndexes[0]}/{fileRequest.File.Length}");
                                    response.ContentType = fileRequest.ContentType;
                                }
                                else
                                {
                                    response.ContentType =
                                        $"multipart/byteranges; boundary={fileRequest.Boundary}";
                                }

                                response.StatusCode = (int)HttpStatusCode.PartialContent;
                                var fileStream = fileRequest.File.OpenRead();
                                fileStream.Position = fileRequest.RangesStartIndexes[0];
                                return(fileStream);
                            }
                        }
                    }
                    else
                    {
                        Log.LogDebug("File not found: {0}", file);
                        response.StatusCode = (int)HttpStatusCode.NotFound;
                    }
                } catch (Exception exception) {
                    Log.LogError(exception);
                    response.StatusCode = (int)HttpStatusCode.NotFound;
                }
            }
            else
            {
                Log.LogDebug("Incorrect parameter: {0}", file);

                response.StatusCode = (int)HttpStatusCode.NoContent;
            }

            return(null);
        }