Ejemplo n.º 1
0
        public async Task <IActionResult> Import(string playlistName, string playlistUrl, string provider, IFormFile file, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(playlistName))
            {
                throw new ArgumentNullException(nameof(provider));
            }

            if (string.IsNullOrEmpty(playlistName))
            {
                playlistName = file.FileName.Replace(Path.GetExtension(file.FileName), string.Empty); //TODO : catch all ArgumentException et les passer en BadRequest
            }
            //Vérifier si la playlist existe bien avant

            PlaylistProvider <Playlist <TvgMedia>, TvgMedia> providerInstance = null;

            try
            {
                providerInstance = _providerFactory.CreateInstance(playlistUrl, provider);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            var playlistStream = file.OpenReadStream();

            if (string.IsNullOrEmpty(playlistUrl))
            {
                //TODO : Save file and get Url
                throw new ApplicationException("Import playlist from file not supported yet !!");
            }

            using (providerInstance)
            {
                var user = await _dbContext.Users.FindAsync(new object[] { UserId }, cancellationToken);

                if (user == null)
                {
                    return(BadRequest($"User {this.UserEmail} not found"));
                }

                var pl = await _playlistService.SynkPlaylistAsync(() => new Playlist
                {
                    UserId       = user.Id,
                    Freindlyname = playlistName,
                    Status       = PlaylistStatus.Enabled,
                    SynkConfig   = null,
                    Tags         = new Dictionary <string, string> {
                        { PlaylistTags.ImportProvider, provider }
                    }
                }, providerInstance, _xtreamService.IsXtreamPlaylist(playlistUrl), true, cancellationToken);

                ClearCache();

                var result = PlaylistModel.ToLightModel(pl, Url);
                return(Created(result.PublicUrl, result));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetAsync(string id, [FromQuery] bool light = true, CancellationToken cancellationToken = default)
        {
            var idGuid   = GetInternalPlaylistId(id);
            var playlist = await _dbContext.Playlist.FirstOrDefaultAsync(x => x.UniqueId == idGuid, cancellationToken);

            if (playlist == null)
            {
                return(NotFound(id));
            }

            //var tmp = playlist.TvgMedias.Where(x => x.Name.Contains("SPORTS MAX") && x.Group.Equals("beIN SPORTS"));

            //foreach (var item in tmp)
            //{
            //    if (item.Name.Contains('1'))
            //    {
            //        item.Tvg.Id = "max1";
            //        item.Tvg.Name = "max1";
            //        item.Tvg.TvgIdentify = "mena_sports/max1";
            //        item.Tvg.TvgSiteSource = "Bein.net";
            //    }
            //    else if (item.Name.Contains('2'))
            //    {
            //        item.Tvg.Id = "max2";
            //        item.Tvg.Name = "max2";
            //        item.Tvg.TvgIdentify = "mena_sports/max2";
            //        item.Tvg.TvgSiteSource = "Bein.net";
            //    }
            //    else if (item.Name.Contains('3'))
            //    {
            //        item.Tvg.Id = "max3";
            //        item.Tvg.Name = "max3";
            //        item.Tvg.TvgIdentify = "mena_sports/max3";
            //        item.Tvg.TvgSiteSource = "Bein.net";
            //    }
            //    else if (item.Name.Contains('4'))
            //    {
            //        item.Tvg.Id = "max4";
            //        item.Tvg.Name = "max4";
            //        item.Tvg.TvgIdentify = "mena_sports/max4";
            //        item.Tvg.TvgSiteSource = "Bein.net";
            //    }
            //}
            //await _dbContext.SaveChangesAsync(cancellationToken);

            var res = light ? Ok(PlaylistModel.ToLightModel(playlist, Url)) : Ok(PlaylistModel.ToModel(playlist, Url));

            return(res);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> ImportFromUrl([FromBody] PlaylistPostModel playlistPostModel, CancellationToken cancellationToken)
        {
            //Vérifier si la playlist existe-elle avant
            var stopwatch = Stopwatch.StartNew();

            PlaylistProvider <Playlist <TvgMedia>, TvgMedia> providerInstance = null;

            try
            {
                providerInstance = _providerFactory.CreateInstance(playlistPostModel.Url, playlistPostModel.Provider);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            using (providerInstance)
            {
                var user = await _dbContext.Users.FindAsync(new object[] { UserId }, cancellationToken);

                if (user == null)
                {
                    return(BadRequest($"User {this.UserEmail} not found"));
                }

                var pl = await _playlistService.SynkPlaylistAsync(() => new Playlist
                {
                    UserId       = user.Id,
                    Freindlyname = playlistPostModel.Freindlyname,
                    Status       = PlaylistStatus.Enabled,
                    SynkConfig   = new SynkConfig {
                        Url = playlistPostModel.Url
                    },
                    Tags = new Dictionary <string, string> {
                        { PlaylistTags.ImportProvider, playlistPostModel.Provider }
                    }
                }, providerInstance, _xtreamService.IsXtreamPlaylist(playlistPostModel.Url), cancellationToken : cancellationToken);

                ClearCache();

                stopwatch.Stop();
                _logger.LogInformation($"Elapsed time : {stopwatch.Elapsed.ToString("c")}");

                var model = PlaylistModel.ToLightModel(pl, Url);
                return(Created(model.PublicUrl, model));
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> ListAsync([FromBody] QueryListBaseModel query, [FromQuery] bool light = true,
                                                    CancellationToken cancellationToken = default)
        {
            var user = await _dbContext.Users.FindAsync(new object[] { UserId }, cancellationToken);

            if (user == null)
            {
                _logger.LogError($"User {this.UserEmail} not founded");
                return(BadRequest($"User {this.UserEmail} not found"));
            }
            var plCacheKey = $"{UserCachePlaylistKey}_{query.GetHashCode()}_{light}";
            var playlists  = await _memoryCache.GetOrCreateAsync(plCacheKey, async entry =>
            {
                if (_memoryCache.TryGetValue(UserCachePlaylistKey, out List <string> list))
                {
                    list.Add(UserCachePlaylistKey);
                }
                else
                {
                    list = new List <string> {
                        plCacheKey
                    };
                }

                _memoryCache.Set(UserCachePlaylistKey, list);
                entry.SlidingExpiration = TimeSpan.FromHours(2);

                return(await Task.Run(() =>
                {
                    var response = _dbContext.Playlist
                                   .Where(x => x.UserId == user.Id)
                                   .OrderByDescending(x => x.Id)
                                   .Select(pl => light ? PlaylistModel.ToLightModel(pl, Url) : PlaylistModel.ToModel(pl, Url))
                                   .GetPaged(query.PageNumber, query.PageSize);
                    return response;
                }));
            });

            return(new OkObjectResult(playlists));
        }