예제 #1
0
        public static async Task <HttpResponseMessage> Content(Movie_Data movie, RangeHeaderValue header)
        {
            if (movie == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            string path = Get.MoviePath(movie);

            if (path == null)
            {
                throw new HttpResponseException(HttpStatusCode.NoContent);
            }

            MovieFileInfo = new FileInfo(Path.Combine(path, movie.name + "." + movie.ext));

            if (!MovieFileInfo.Exists)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            long totalLength = MovieFileInfo.Length;

            RangeHeaderValue rangeHeader = header;

            response = new HttpResponseMessage();

            response.Headers.AcceptRanges.Add("bytes");

            // The request will be treated as normal request if there is no Range header.
            if (rangeHeader == null || !rangeHeader.Ranges.Any())
            {
                response.StatusCode = HttpStatusCode.OK;
                response.Content    = await Contains.RangeHeader();

                response.Content.Headers.ContentLength = totalLength;
                await History.Create(History.Type.API, new History_API()
                {
                    api_action   = "Movie " + movie.Movie_Info.title + " served successfully.",
                    api_type     = "Task -> Streaming movie ",
                    api_datetime = DateTime.Now
                });

                return(response);
            }

            long start = 0, end = 0;

            // 1. If the unit is not 'bytes'.
            // 2. If there are multiple ranges in header value.
            // 3. If start or end position is greater than file length.
            if (rangeHeader.Unit != "bytes" || rangeHeader.Ranges.Count > 1 ||
                !Read.RangeItem(rangeHeader.Ranges.First(), totalLength, out start, out end))
            {
                response.StatusCode = HttpStatusCode.RequestedRangeNotSatisfiable;
                response.Content    = new StreamContent(Stream.Null); // No content for this status.
                response.Content.Headers.ContentRange = new ContentRangeHeaderValue(totalLength);
                response.Content.Headers.ContentType  = Get.MimeNameFromExt(MovieFileInfo.Extension);
                await History.Create(History.Type.API, new History_API()
                {
                    api_action   = "Movie " + movie.Movie_Info.title + " served successfully.",
                    api_type     = "Task -> Streaming movie ",
                    api_datetime = DateTime.Now
                });

                return(response);
            }

            var contentRange = new ContentRangeHeaderValue(start, end, totalLength);

            // PartialContent creator
            response.StatusCode = HttpStatusCode.PartialContent;

            response.Content = await Create.PartialContent(start, end);

            response.Content.Headers.ContentLength = end - start + 1;
            response.Content.Headers.ContentRange  = contentRange;
            await History.Create(History.Type.API, new History_API()
            {
                api_action   = "Movie " + movie.Movie_Info.title + " served successfully.",
                api_type     = "Task -> Streaming movie ",
                api_datetime = DateTime.Now
            });

            return(response);
        }