Beispiel #1
0
        public async Task <IActionResult> ServerInitialize()
        {
            List <string> errors = new List <string>();

            // Check we have an admin user
            var users = await userManager.GetUsersInRoleAsync(UserRoles.User);

            if (users.Count == 0)
            {
                errors.Add("No user registered!");
            }

            var admins = await userManager.GetUsersInRoleAsync(UserRoles.Admin);

            if (admins.Count == 0)
            {
                errors.Add("No administrator users are present!");
            }

            // Complete setup
            if (errors.Count == 0)
            {
                optionManager.SetGlobal(Options.Server_Initialized, true);
                return(Ok(responseFactory.Success()));
            }
            else
            {
                string allErrors = errors.Aggregate("Some errors have been encountered", (x, y) => x + "\n* " + y);
                return(StatusCode(StatusCodes.Status405MethodNotAllowed, responseFactory.Error(allErrors)));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> Validate([FromBody] SubscriptionValidateRequest request)
        {
            try
            {
                string provider = null;

                if (request.Url != null)
                {
                    var url = new Uri(request.Url);
                    provider = await subscriptionManager.TestUrl(url);
                }

                if (request.Name != null)
                {
                    subscriptionManager.ValidateName(request.Name, request.ParentFolderId);
                }

                return(Ok(responseFactory.Success(new SubscriptionValidateResponse()
                {
                    ProviderName = provider,
                })));
            }
            catch (UriFormatException)
            {
                return(BadRequest(responseFactory.Error("Invalid URL format!")));
            }
            catch (ArgumentNullException)
            {
                return(BadRequest(responseFactory.Error("Missing URL argument!")));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(responseFactory.Error(ex.Message)));
            }
            catch (Exception ex)
            {
                return(BadRequest(responseFactory.Error(ex.Message, ex.Message + "\n" + ex.StackTrace)));
            }
        }
Beispiel #3
0
        public async Task <IActionResult> Login([FromBody] UserLoginRequest login)
        {
            var user = await userManager.FindByNameAsync(login.Username);

            if (user != null && await userManager.CheckPasswordAsync(user, login.Password))
            {
                JwtSecurityToken token = await GenerateAuthToken(user, login.RememberMe);

                return(Ok(responseFactory.Success(new AuthResponse()
                {
                    Token = new JwtSecurityTokenHandler().WriteToken(token),
                    ValidTo = token.ValidTo
                })));
            }

            return(Unauthorized(responseFactory.Error("Invalid username or password.")));
        }
Beispiel #4
0
        public async Task <IActionResult> List([FromBody] VideoListRequest request)
        {
            var user = await userManager.GetUserAsync(User);

            var query = videoManager.GetAll(user);

            // Apply filters
            if (request.Ids != null)
            {
                query = query.Where(x => request.Ids.Contains(x.Id));
            }

            if (request.SubscriptionId != null)
            {
                query = query.Where(x => x.SubscriptionId == request.SubscriptionId.Value);
            }

            if (request.SubscriptionFolderId != null)
            {
                var sub = subscriptionManager.GetFolder(user, request.SubscriptionFolderId.Value);
                if (sub == null)
                {
                    return(BadRequest(responseFactory.Error("Invalid subscription folder ID.")));
                }

                var validSubscriptionIds = subscriptionManager.GetSubscriptionsRecursive(sub).Select(x => x.Id).ToArray();
                query = query.Where(x => validSubscriptionIds.Contains(x.SubscriptionId));
            }

            if (request.IsWatched.HasValue)
            {
                query = query.Where(x => x.IsWatched == request.IsWatched.Value);
            }

            if (request.IsDownloaded.HasValue)
            {
                if (request.IsDownloaded.Value)
                {
                    query = query.Where(x => x.DownloadedPath != null);
                }
                else
                {
                    query = query.Where(x => x.DownloadedPath == null);
                }
            }

            // TODO: proper search
            if (request.Query != null)
            {
                query = query.Where(x => x.Name.ToLower().Contains(request.Query.ToLower()));
            }

            // Get the item count here, before applying the limit and offset
            int itemCount = query.Count();

            // Sorting, limit and offset
            query = query.OrderBy(request.Order)
                    .Skip(request.Offset ?? 0)
                    .Take(request.Limit ?? 50);

            // Obtain mime types
            var videos    = query.ToArray();
            var apiVideos = new List <ApiVideo>();

            foreach (var video in videos)
            {
                var apiVideo = modelFactory.ToApi(video);
                apiVideo.StreamMimeType = await videoStorage.GetMimeType(video);

                apiVideos.Add(apiVideo);
            }

            return(Ok(responseFactory.Success(new VideoListResponse
            {
                TotalCount = itemCount,
                Videos = apiVideos.ToArray(),
            })));
        }
        public async Task <IActionResult> Edit([FromBody] SubscriptionFolderEditRequest request)
        {
            var user = await userManager.GetUserAsync(User);

            try
            {
                subscriptionManager.UpdateFolder(user, request.Id, request.Name, request.ParentFolderId);
            }
            catch (Exception ex)
            {
                return(BadRequest(responseFactory.Error(ex.Message)));
            }

            // Update settings
            if (request.AutoDownload.HasValue)
            {
                optionManager.SetForSubscriptionFolder(Options.Subscriptions_AutoDownload, request.Id, request.AutoDownload.Value);
            }
            else
            {
                optionManager.UnsetForSubscriptionFolder(Options.Subscriptions_AutoDownload, request.Id);
            }

            if (request.DownloadMaxCount.HasValue)
            {
                optionManager.SetForSubscriptionFolder(Options.Subscriptions_MaxCount, request.Id, request.DownloadMaxCount.Value);
            }
            else
            {
                optionManager.UnsetForSubscriptionFolder(Options.Subscriptions_MaxCount, request.Id);
            }

            if (request.DownloadOrder.HasValue)
            {
                optionManager.SetForSubscriptionFolder(Options.Subscriptions_DownloadOrder, request.Id, request.DownloadOrder.Value);
            }
            else
            {
                optionManager.UnsetForSubscriptionFolder(Options.Subscriptions_DownloadOrder, request.Id);
            }

            if (request.AutomaticDeleteWatched.HasValue)
            {
                optionManager.SetForSubscriptionFolder(Options.Subscriptions_AutoDeleteWatched, request.Id, request.AutomaticDeleteWatched.Value);
            }
            else
            {
                optionManager.UnsetForSubscriptionFolder(Options.Subscriptions_AutoDeleteWatched, request.Id);
            }

            if (!string.IsNullOrEmpty(request.DownloadPath))
            {
                optionManager.SetForSubscriptionFolder(Options.Subscriptions_DownloadPath, request.Id, request.DownloadPath);
            }
            else
            {
                optionManager.UnsetForSubscriptionFolder(Options.Subscriptions_DownloadPath, request.Id);
            }

            return(Ok(responseFactory.Success()));
        }