Beispiel #1
0
                /// <summary>
                /// Creating a session guid that allows users to play content
                /// </summary>
                /// <param name="data">DatabaseUserModels</param>
                /// <returns>string</returns>
                public static async Task <Object> Session <T>(DatabaseUserModels data)
                {
                    bool isGuest = false;
                    Guid g1;

                    if (data.user_id.Length > 30)
                    {
                        var s0 = await db.Session_Play.Where(x => x.movie_id == data.movie_id && x.user_id == data.user_id).FirstOrDefaultAsync();

                        if (s0 != null)
                        {
                            if (s0.movie_id != "" && s0.session_id != "" && s0.session_date < DateTime.Now && s0.user_id != "")
                            {
                                db.Session_Play.Remove(s0);
                                await db.SaveChangesAsync();
                            }
                        }
                        isGuest = false;
                        g1      = new Guid(data.user_id);
                    }
                    else
                    {
                        isGuest = true;
                        MD5 md5Hasher = MD5.Create();
                        g1 = new Guid(md5Hasher.ComputeHash(Encoding.Default.GetBytes(DateTime.Now.ToString())));
                    }
                    Guid g2 = new Guid(data.movie_id);

                    var result = g1.GetHashCode() ^ g2.GetHashCode() + DateTime.Now.GetHashCode();
                    List <CustomClasses.Random.values> v = new List <CustomClasses.Random.values>();

                    if (isGuest)
                    {
                        var s = new Session_Guest()
                        {
                            session_date = DateTime.Now,
                            session_id   = Movie.Create.Guid(result.ToString()).ToString(),
                            movie_id     = data.movie_id,
                        };
                        db.Session_Guest.Add(s);
                        await db.SaveChangesAsync();

                        return(s);
                    }
                    else
                    {
                        var s = new Session_Play()
                        {
                            session_date = DateTime.Now,
                            session_id   = Movie.Create.Guid(result.ToString()).ToString(),
                            movie_id     = data.movie_id,
                            user_id      = data.user_id
                        };
                        db.Session_Play.Add(s);
                        await db.SaveChangesAsync();

                        return(s);
                    }
                }
        public async Task <HttpResponseMessage> Play([FromUri] string value)
        {
            try
            {
                //get user id and movie id from session in database
                var s = await Database.User.Get.BySession(value);

                Session_Play  sp      = new Session_Play();
                Session_Guest sg      = new Session_Guest();
                string        movieId = "";
                Movie_Data    movie;
                if (s is Session_Play)
                {
                    sp      = (Session_Play)s;
                    movieId = sp.movie_id;
                    movie   = await Database.Movie.Get.ByGuidAndChangeCounter(sp.movie_id, true);
                }
                else
                {
                    sg      = (Session_Guest)s;
                    movieId = sg.movie_id;
                    movie   = await Database.Movie.Get.ByGuidAndChangeCounter(sg.movie_id, true);
                }
                if (sp != null || sg != null && movieId != null)
                {
                    await History.Create(History.Type.User, new History_User()
                    {
                        user_action   = "User requesting to watch movie: " + value,
                        user_datetime = DateTime.Now,
                        user_id       = sp.user_id,
                        user_movie    = movie.Movie_Info.title,
                        user_type     = "Status -> User Request -> Movie"
                    });

                    //Getting movie from DB
                    if (movie != null && movie.enabled)
                    {
                        await History.Create(History.Type.User, new History_User()
                        {
                            user_action   = "Movie " + value + " is being served.",
                            user_datetime = DateTime.Now,
                            user_id       = sp.user_id,
                            user_movie    = movie.Movie_Info.title,
                            user_type     = "Status -> Movie -> start send"
                        });

                        //streaming content to client
                        return(await Streaming.Content(movie, base.Request.Headers.Range));
                    }
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }
            catch (System.Web.HttpException ex)
            {
                await History.Create(History.Type.API, new History_API()
                {
                    api_action   = "Exception caught on Play() -> " + ex.Message,
                    api_datetime = DateTime.Now,
                    api_type     = "Exception thrown -> Play"
                });

                throw new HttpResponseException(HttpStatusCode.Conflict);
            }
        }