Пример #1
0
 public static string MoviePath(Movie_Data movie)
 {
     foreach (var mDir in Global.Global.GlobalMovieDisksList)
     {
         if (Directory.Exists(mDir.value + @"\" + movie.folder))
         {
             return(mDir.value + @"\" + movie.folder);
         }
     }
     return(null);
 }
Пример #2
0
                public static async Task <bool> Directory(Movie_Data sqlData, Movie_Data readDataFromDisk)
                {
                    if (sqlData != null && readDataFromDisk != null &&
                        sqlData != readDataFromDisk)
                    {
                        sqlData.dir    = readDataFromDisk.dir;
                        sqlData.folder = readDataFromDisk.folder;
                        await db.SaveChangesAsync();

                        return(true);
                    }
                    return(false);
                }
Пример #3
0
            /// <summary>
            /// Return the media's (movies) length as ticks
            /// </summary>
            /// <param name="data">Movie_Data</param>
            /// <returns>long</returns>
            public static long Length(Movie_Data data)
            {
                var movie = (Directory.EnumerateFiles(data.dir))
                            .Where(y => y.Contains(data.ext) || y.Contains(data.name))
                            .FirstOrDefault();

                var inputFile = new MediaFile {
                    Filename = movie
                };

                using (var engine = new Engine())
                {
                    engine.GetMetadata(inputFile);
                }

                return(inputFile.Metadata.Duration.Ticks);
            }
Пример #4
0
                /// <summary>
                /// Remove movie from db
                /// </summary>
                /// <param name="item">MovieData</param>
                /// <returns>int</returns>
                public static async Task <int> ByModel(Movie_Data item)
                {
                    try
                    {
                        db.Movie_Data.Remove(item);
                        return(await db.SaveChangesAsync());
                    }
                    catch (Exception ex)
                    {
                        await History.Create(History.Type.API, new History_API()
                        {
                            api_action   = "Exception caught",
                            api_type     = "Exception -> Movie.Remove.ByModel() --> " + ex.Message,
                            api_datetime = DateTime.Now
                        });

                        return(-1);
                    }
                }
Пример #5
0
                public static async Task <bool> Movie(Movie_Data movie)
                {
                    if (movie.guid.Length > 0)
                    {
                        var m = await Get.ByGuidAndChangeCounter(movie.guid, false);

                        if (m != null && m != movie)
                        {
                            db.Movie_Data.Remove(m);
                            if (!Functions.Functions.PropertyCheck.IsAnyNullOrEmpty(movie))
                            {
                                db.Movie_Data.Add(movie);
                                await db.SaveChangesAsync();

                                return(true);
                            }
                            return(false);
                        }
                    }
                    return(false);
                }
Пример #6
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);
        }
Пример #7
0
            /// <summary>
            /// Insert new movies to database
            /// </summary>
            /// <returns></returns>
            public static async Task MoviesToDatabase()
            {
                await History.Create(History.Type.API, new History_API()
                {
                    api_action   = "Starting task -> Add movies to local Database",
                    api_datetime = DateTime.Now,
                    api_type     = "Task -> add movies to Db",
                });

                var list = new List <int>();

                //item1 = MovieData, item2 = Match
                foreach (var item in NewMovieEntry)
                {
                    try
                    {
                        //check if movie is already in database with this title and update values only for data not info!!
                        var Db_Movie = await Db.Movie_Data.Where(x => x.name == item.Item1.name).FirstOrDefaultAsync();

                        if (Db_Movie == null)
                        {
                            //get movieinfo from api
                            if (MoviesAPI.countAPICalls > 30)
                            {
                                await Task.Delay(5000); MoviesAPI.countAPICalls = 0;
                            }
                            //editMovieInfo, movie[0] is array from method GetMovieName
                            Movie_Info mInfo = await MoviesAPI.Get.Info(item.Item2, DatabaseMovieCount);

                            if (mInfo.id != null)
                            {
                                //tagline error in database has max length of 128 char in SQL
                                if (mInfo.tagline.Length > 128)
                                {
                                    mInfo.tagline = mInfo.tagline.Substring(0, 127);
                                }
                                item.Item1.Movie_Info = mInfo;

                                //set tick for movie length
                                item.Item1.Movie_Info.length = Media.Length(item.Item1);

                                try
                                {
                                    Db.Movie_Data.Add(item.Item1);
                                    await Db.SaveChangesAsync();

                                    //databaseMovieCount++;
                                    //temp.Add(mData);
                                    Movie_Data movie = Db.Movie_Data.Where(x => x.name == item.Item1.name).First();
                                    await History.Create(History.Type.API, new History_API()
                                    {
                                        api_action   = "Movie " + movie.Movie_Info.title + " was added to the database as id " + movie.Id + "!",
                                        api_datetime = DateTime.Now,
                                        api_type     = "Movie added to database",
                                    });

                                    list.Add(movie.Id);
                                }
                                catch (Exception ex)
                                {
                                    await History.Create(History.Type.API, new History_API()
                                    {
                                        api_action   = "Exception : Inserting movie to Database --> " + ex.Message,
                                        api_datetime = DateTime.Now,
                                        api_type     = "Exception thrown InsertMoviesToDb",
                                    });
                                }
                            }
                            else
                            {
                                await History.Create(History.Type.API, new History_API()
                                {
                                    api_action   = "Movie " + item.Item2.Groups["title"].ToString() + " was not added as there was a problem!",
                                    api_datetime = DateTime.Now,
                                    api_type     = "Error on movie addition",
                                });
                            }
                        }
                        else
                        {
                            //update movie info but only directory at which the movie is located
                            await History.Create(History.Type.API, new History_API()
                            {
                                api_action   = "Movie update -> Updating movie: " + Db_Movie.name + " directory!",
                                api_datetime = DateTime.Now,
                                api_type     = "Task -> update movie data / info in SQL...",
                            });

                            if (await Database.Movie.Update.Directory(Db_Movie, item.Item1))
                            {
                                await History.Create(History.Type.API, new History_API()
                                {
                                    api_action   = "Movie update -> Succesfully updated movie: " + Db_Movie.name + " directory!",
                                    api_datetime = DateTime.Now,
                                    api_type     = "Task -> Success",
                                });
                            }
                            else
                            {
                                await History.Create(History.Type.API, new History_API()
                                {
                                    api_action   = "Movie update -> Failed to update movie: " + Db_Movie.name + " directory!",
                                    api_datetime = DateTime.Now,
                                    api_type     = "Task -> Failed",
                                });
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        await History.Create(History.Type.API, new History_API()
                        {
                            api_action   = "Error -> An error occured : " + ex.Message,
                            api_datetime = DateTime.Now,
                            api_type     = "Error occured on InsertMoviesToDb",
                        });
                    }
                }
                await History.Create(History.Type.API, new History_API()
                {
                    api_action   = "End of import of movies.",
                    api_datetime = DateTime.Now,
                    api_type     = "Status -> InsertMoviesToDb",
                });

                History_API hapi;

                if (list.Count == NewMovieEntry.Count)
                {
                    hapi = new History_API()
                    {
                        api_type     = "Movie to Db status",
                        api_datetime = DateTime.Now,
                        api_action   = "Info -> All movies added (" + list.Count + " - ADDED)",
                    };
                }
                else if (list.Count < NewMovieEntry.Count)
                {
                    hapi = new History_API()
                    {
                        api_type     = "Movie to Db status",
                        api_datetime = DateTime.Now,
                        api_action   = "Error -> Less movies added than found on local storage! Movies added " + list.Count,
                    };
                }
                else
                {
                    hapi = new History_API()
                    {
                        api_type     = "Movie to Db status",
                        api_datetime = DateTime.Now,
                        api_action   = "Error -> Something went wrong with importing data to Db! Movies added " + list.Count,
                    };
                }
                if (hapi != null)
                {
                    await History.Create(History.Type.API, hapi);
                }
            }