Exemplo n.º 1
0
        public ActionResult RegenerateAllEpisodeLinks()
        {
            try
            {
                using (var upd = Repo.Instance.CrossRef_AniDB_Provider.BeginBatchUpdate(() => Repo.Instance.CrossRef_AniDB_Provider.GetByType(Shoko.Models.Enums.CrossRefType.TvDB)))
                {
                    foreach (SVR_CrossRef_AniDB_Provider p in upd)
                    {
                        p.EpisodesList.DeleteAllUnverifiedLinks();
                        if (p.EpisodesList.NeedPersitance)
                        {
                            p.EpisodesList.Persist();
                            upd.Update(p);
                        }
                    }
                    upd.Commit();
                }
                Repo.Instance.AnimeSeries.GetAll().ToList().AsParallel().ForAll(animeseries => LinkingHelper.GenerateEpisodeMatches(animeseries.AniDB_ID, Shoko.Models.Enums.CrossRefType.TvDB, true));
            }
            catch (Exception e)
            {
                logger.Error(e);
                return(APIStatus.InternalError(e.Message));
            }

            return(APIStatus.OK());
        }
Exemplo n.º 2
0
        public ActionResult ImportConfig(CL_ServerSettings settings)
        {
            if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only do this before server init"));
            }

            string raw_settings = settings.ToJSON();

            if (raw_settings.Length == new CL_ServerSettings().ToJSON().Length)
            {
                return(APIStatus.BadRequest("Empty settings are not allowed"));
            }

            string path = Path.Combine(ServerSettings.ApplicationPath, "temp.json");

            System.IO.File.WriteAllText(path, raw_settings, Encoding.UTF8);
            try
            {
                ServerSettings.LoadSettingsFromFile(path, true);
                return(APIStatus.OK());
            }
            catch
            {
                return(APIStatus.InternalError("Error while importing settings"));
            }
        }
Exemplo n.º 3
0
        public ActionResult ImportConfig(CL_ServerSettings settings)
        {
            return(BadRequest("The model that this method takes is deprecated and will break the settings file. Use APIv3"));

            string raw_settings = settings.ToJSON();

            if (raw_settings.Length == new CL_ServerSettings().ToJSON().Length)
            {
                return(APIStatus.BadRequest("Empty settings are not allowed"));
            }

            string path = Path.Combine(ServerSettings.ApplicationPath, "temp.json");

            System.IO.File.WriteAllText(path, raw_settings, Encoding.UTF8);
            try
            {
                ServerSettings.LoadSettingsFromFile(path, true);
                ServerSettings.Instance.SaveSettings();
                return(APIStatus.OK());
            }
            catch
            {
                return(APIStatus.InternalError("Error while importing settings"));
            }
        }
Exemplo n.º 4
0
        public ActionResult <Setting> GetSetting(Setting setting)
        {
            try
            {
                // TODO Refactor Settings to a POCO that is serialized, and at runtime, build a dictionary of types to validate against
                if (string.IsNullOrEmpty(setting?.setting))
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }
                var value = typeof(ServerSettings).GetProperty(setting.setting)?.GetValue(null, null);
                if (value == null)
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }

                return(new Setting
                {
                    setting = setting.setting,
                    value = value.ToString()
                });
            }
            catch
            {
                return(APIStatus.InternalError());
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get url for update and start update
        /// </summary>
        /// <param name="tag_name"></param>
        /// <returns></returns>
        internal object WebUIGetUrlAndUpdate(string tag_name, string channel)
        {
            try
            {
                var client = new System.Net.WebClient();
                client.Headers.Add("Accept: application/vnd.github.v3+json");
                client.Headers.Add("User-Agent", "jmmserver");
                var response = client.DownloadString(
                    new Uri("https://api.github.com/repos/japanesemediamanager/shokoserver-webui/releases/tags/" +
                            tag_name));

                dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(response);
                string  url    = string.Empty;
                foreach (dynamic obj in result.assets)
                {
                    if (obj.name == "latest.zip")
                    {
                        url = obj.browser_download_url;
                        break;
                    }
                }

                //check if tag was parsed corrently as it make the url
                return(url != string.Empty
                    ? WebUIUpdate(url, channel, tag_name)
                    : new APIMessage(204, "Content is missing"));
            }
            catch
            {
                return(APIStatus.InternalError());
            }
        }
        public ActionResult EditFolder(ImportFolder folder)
        {
            if (String.IsNullOrEmpty(folder.ImportFolderLocation) || folder.ImportFolderID == 0)
            {
                return(new APIMessage(400, "ImportFolderLocation and ImportFolderID missing"));
            }

            if (folder.IsDropDestination == 1 && folder.IsDropSource == 1)
            {
                return(new APIMessage(StatusCodes.Status409Conflict,
                                      "The Import Folder can't be both Destination and Source"));
            }

            if (folder.ImportFolderID == 0)
            {
                return(new APIMessage(StatusCodes.Status409Conflict, "The Import Folder must have an ID"));
            }

            try
            {
                RepoFactory.ImportFolder.SaveImportFolder(folder);
                return(Ok());
            }
            catch (Exception e)
            {
                return(APIStatus.InternalError(e.Message));
            }
        }
Exemplo n.º 7
0
        public ActionResult EditFolder(ImportFolder folder)
        {
            if (!String.IsNullOrEmpty(folder.ImportFolderLocation) && folder.ImportFolderID != 0)
            {
                try
                {
                    // TODO Do this correctly without calling APIv1
                    if (folder.IsDropDestination == 1 && folder.IsDropSource == 1)
                    {
                        return(new APIMessage(409, "The Import Folder can't be both Destination and Source"));
                    }

                    if (folder.ImportFolderID == 0)
                    {
                        return(new APIMessage(409, "The Import Folder must have an ID"));
                    }
                    CL_Response <ImportFolder> response =
                        new ShokoServiceImplementation().SaveImportFolder(folder);

                    if (!string.IsNullOrEmpty(response.ErrorMessage))
                    {
                        return(new APIMessage(500, response.ErrorMessage));
                    }

                    return(APIStatus.OK());
                }
                catch
                {
                    return(APIStatus.InternalError());
                }
            }
            return(new APIMessage(400, "ImportFolderLocation and ImportFolderID missing"));
        }
Exemplo n.º 8
0
        public ActionResult AddFolder(ImportFolder folder)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (folder.ImportFolderLocation != string.Empty)
            {
                try
                {
                    // TODO Do this correctly without calling APIv1
                    CL_Response <ImportFolder> response = new ShokoServiceImplementation().SaveImportFolder(folder);

                    if (string.IsNullOrEmpty(response.ErrorMessage))
                    {
                        return(APIStatus.OK());
                    }
                    return(new APIMessage(500, response.ErrorMessage));
                }
                catch
                {
                    return(APIStatus.InternalError());
                }
            }
            return(new APIMessage(400, "Bad Request: The Folder path must not be Empty"));
        }
Exemplo n.º 9
0
        private ActionResult <Setting> GetSetting(Setting setting)
        {
            try
            {
                return(NoContent());

                // TODO recursive reflection to get into the settings
                if (string.IsNullOrEmpty(setting?.setting))
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }
                try
                {
                    var value = ServerSettings.Instance;
                    if (value == null)
                    {
                        return(APIStatus.BadRequest("An invalid setting was passed"));
                    }

                    return(new Setting
                    {
                        setting = setting.setting,
                        value = value.ToString()
                    });
                }
                catch
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }
            }
            catch
            {
                return(APIStatus.InternalError());
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Return given setting
        /// </summary>
        /// <returns></returns>
        private object GetSetting()
        {
            try
            {
                // TODO Refactor Settings to a POCO that is serialized, and at runtime, build a dictionary of types to validate against
                Settings setting = this.Bind();
                if (string.IsNullOrEmpty(setting?.setting))
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }
                var value = typeof(ServerSettings).GetProperty(setting.setting)?.GetValue(null, null);
                if (value == null)
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }

                Settings return_setting = new Settings
                {
                    setting = setting.setting,
                    value   = value.ToString()
                };
                return(return_setting);
            }
            catch
            {
                return(APIStatus.InternalError());
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Set given setting
        /// </summary>
        /// <returns></returns>
        private object SetSetting()
        {
            if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only do this before server init"));
            }

            // TODO Refactor Settings to a POCO that is serialized, and at runtime, build a dictionary of types to validate against
            try
            {
                Settings setting = this.Bind();
                if (string.IsNullOrEmpty(setting.setting))
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }

                if (setting.value == null)
                {
                    return(APIStatus.BadRequest("An invalid value was passed"));
                }

                var property = typeof(ServerSettings).GetProperty(setting.setting);
                if (property == null)
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }
                if (!property.CanWrite)
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }
                var settingType = property.PropertyType;
                try
                {
                    var converter = TypeDescriptor.GetConverter(settingType);
                    if (!converter.CanConvertFrom(typeof(string)))
                    {
                        return(APIStatus.BadRequest("An invalid value was passed"));
                    }
                    var value = converter.ConvertFromInvariantString(setting.value);
                    if (value == null)
                    {
                        return(APIStatus.BadRequest("An invalid value was passed"));
                    }
                    property.SetValue(null, value);
                }
                catch
                {
                    // ignore, we are returning the error below
                }

                return(APIStatus.BadRequest("An invalid value was passed"));
            }
            catch
            {
                return(APIStatus.InternalError());
            }
        }
Exemplo n.º 12
0
 public ActionResult <ServerSettings> ExportConfig()
 {
     try
     {
         return(ServerSettings.Instance);
     }
     catch
     {
         return(APIStatus.InternalError("Error while reading settings."));
     }
 }
Exemplo n.º 13
0
 /// <summary>
 /// Return body of current working settings.json - this could act as backup
 /// </summary>
 /// <returns></returns>
 private object ExportConfig()
 {
     try
     {
         return(ServerSettings.appSettings);
     }
     catch
     {
         return(APIStatus.InternalError("Error while reading settings."));
     }
 }
Exemplo n.º 14
0
        public ActionResult SetSetting(Setting setting)
        {
            return(NoContent());

            // TODO Refactor Settings to a POCO that is serialized, and at runtime, build a dictionary of types to validate against
            try
            {
                if (string.IsNullOrEmpty(setting.setting))
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }

                if (setting.value == null)
                {
                    return(APIStatus.BadRequest("An invalid value was passed"));
                }

                var property = typeof(ServerSettings).GetProperty(setting.setting);
                if (property == null)
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }
                if (!property.CanWrite)
                {
                    return(APIStatus.BadRequest("An invalid setting was passed"));
                }
                var settingType = property.PropertyType;
                try
                {
                    var converter = TypeDescriptor.GetConverter(settingType);
                    if (!converter.CanConvertFrom(typeof(string)))
                    {
                        return(APIStatus.BadRequest("An invalid value was passed"));
                    }
                    var value = converter.ConvertFromInvariantString(setting.value);
                    if (value == null)
                    {
                        return(APIStatus.BadRequest("An invalid value was passed"));
                    }
                    property.SetValue(null, value);
                }
                catch
                {
                    // ignore, we are returning the error below
                }

                return(APIStatus.BadRequest("An invalid value was passed"));
            }
            catch
            {
                return(APIStatus.InternalError());
            }
        }
Exemplo n.º 15
0
 public ActionResult SetDefaultUserCredentials(Credentials credentials)
 {
     try
     {
         ServerSettings.Instance.Database.DefaultUserUsername = credentials.login;
         ServerSettings.Instance.Database.DefaultUserPassword = credentials.password;
         return(APIStatus.OK());
     }
     catch
     {
         return(APIStatus.InternalError());
     }
 }
Exemplo n.º 16
0
        /// <summary>
        /// Delete user from his ID
        /// </summary>
        /// <returns></returns>
        private object DeleteUser()
        {
            SVR_JMMUser _user = (SVR_JMMUser)Context.CurrentUser;

            if (_user.IsAdmin == 1)
            {
                SVR_JMMUser user = this.Bind();
                return(new ShokoServiceImplementation().DeleteUser(user.JMMUserID) == string.Empty
                    ? APIStatus.OK()
                    : APIStatus.InternalError());
            }

            return(APIStatus.AdminNeeded());
        }
Exemplo n.º 17
0
        /// <summary>
        /// Get Trakt code and url
        /// </summary>
        /// <returns></returns>
        private object GetTraktCode()
        {
            var code = new ShokoServiceImplementation().GetTraktDeviceCode();

            if (code.UserCode == string.Empty)
            {
                return(APIStatus.InternalError());
            }

            Dictionary <string, object> result = new Dictionary <string, object>();

            result.Add("usercode", code.UserCode);
            result.Add("url", code.VerificationUrl);
            return(result);
        }
Exemplo n.º 18
0
        public ActionResult <Dictionary <string, object> > GetTraktCode()
        {
            var code = new ShokoServiceImplementation().GetTraktDeviceCode();

            if (code.UserCode == string.Empty)
            {
                return(APIStatus.InternalError("Trakt code doesn't exist on the server"));
            }

            Dictionary <string, object> result = new Dictionary <string, object>();

            result.Add("usercode", code.UserCode);
            result.Add("url", code.VerificationUrl);
            return(result);
        }
Exemplo n.º 19
0
        public ActionResult <ServerSettings> ExportConfig()
        {
            if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only do this before server init"));
            }

            try
            {
                return(ServerSettings.Instance);
            }
            catch
            {
                return(APIStatus.InternalError("Error while reading settings."));
            }
        }
Exemplo n.º 20
0
        public ActionResult RegenerateAllEpisodeLinks()
        {
            try
            {
                RepoFactory.CrossRef_AniDB_TvDB_Episode.DeleteAllUnverifiedLinks();
                RepoFactory.AnimeSeries.GetAll().ToList().AsParallel().ForAll(animeseries =>
                                                                              TvDBLinkingHelper.GenerateTvDBEpisodeMatches(animeseries.AniDB_ID, true));
            }
            catch (Exception e)
            {
                logger.Error(e);
                return(APIStatus.InternalError(e.Message));
            }

            return(APIStatus.OK());
        }
Exemplo n.º 21
0
 public object SetWebUIConfig(WebUI_Settings settings)
 {
     if (settings.Valid())
     {
         try
         {
             ServerSettings.Instance.WebUI_Settings = JsonConvert.SerializeObject(settings);
             return(APIStatus.OK());
         }
         catch
         {
             return(APIStatus.InternalError("error at saving webui settings"));
         }
     }
     return(new APIMessage(400, "Config is not a Valid."));
 }
Exemplo n.º 22
0
        /// <summary>
        /// Return body of current working settings.json - this could act as backup
        /// </summary>
        /// <returns></returns>
        private object ExportConfig()
        {
            if (ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only do this before server init"));
            }

            try
            {
                return(ServerSettings.appSettings);
            }
            catch
            {
                return(APIStatus.InternalError("Error while reading settings."));
            }
        }
Exemplo n.º 23
0
        public ActionResult <WebUI_Settings> GetWebUIConfig()
        {
            if (!string.IsNullOrEmpty(ServerSettings.Instance.WebUI_Settings))
            {
                try
                {
                    return(JsonConvert.DeserializeObject <WebUI_Settings>(ServerSettings.Instance.WebUI_Settings));
                }
                catch
                {
                    return(APIStatus.InternalError("error while reading webui settings"));
                }
            }

            return(APIStatus.OK());
        }
Exemplo n.º 24
0
 public ActionResult <Credentials> GetAniDB()
 {
     try
     {
         return(new Credentials
         {
             login = ServerSettings.Instance.AniDb.Username,
             port = ServerSettings.Instance.AniDb.ClientPort,
             apiport = ServerSettings.Instance.AniDb.AVDumpClientPort
         });
     }
     catch
     {
         return(APIStatus.InternalError(
                    "The ports are not set as integers. Set them and try again.\n\rThe default values are:\n\rAniDB Client Port: 4556\n\rAniDB AVDump Client Port: 4557"));
     }
 }
Exemplo n.º 25
0
        /// <summary>
        /// Create user from Contract_JMMUser
        /// </summary>
        /// <returns></returns>
        private object CreateUser()
        {
            SVR_JMMUser _user = (SVR_JMMUser)Context.CurrentUser;

            if (_user.IsAdmin == 1)
            {
                JMMUser user = this.Bind();
                user.Password       = Digest.Hash(user.Password);
                user.HideCategories = string.Empty;
                user.PlexUsers      = string.Empty;
                return(new ShokoServiceImplementation().SaveUser(user) == string.Empty
                    ? APIStatus.OK()
                    : APIStatus.InternalError());
            }

            return(APIStatus.AdminNeeded());
        }
Exemplo n.º 26
0
        public ActionResult SetDefaultUserCredentials(Credentials credentials)
        {
            if (!ServerSettings.Instance.FirstRun || ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only set the default user's credentials on first run"));
            }

            try
            {
                ServerSettings.Instance.Database.DefaultUserUsername = credentials.login;
                ServerSettings.Instance.Database.DefaultUserPassword = credentials.password;
                return(APIStatus.OK());
            }
            catch
            {
                return(APIStatus.InternalError());
            }
        }
Exemplo n.º 27
0
        /// <summary>
        /// Sets the default user's credentials
        /// </summary>
        /// <returns></returns>
        private object SetDefaultUserCredentials()
        {
            if (!ServerSettings.FirstRun || ServerState.Instance.ServerOnline || ServerState.Instance.ServerStarting)
            {
                return(APIStatus.BadRequest("You may only set the default user's credentials on first run"));
            }

            try
            {
                Credentials credentials = this.Bind();
                ServerSettings.DefaultUserUsername = credentials.login;
                ServerSettings.DefaultUserPassword = credentials.password;
                return(APIStatus.OK());
            }
            catch
            {
                return(APIStatus.InternalError());
            }
        }
Exemplo n.º 28
0
        public ActionResult ImportConfig([FromBody] CL_ServerSettings settings)
        {
            string raw_settings = settings.ToJSON();

            if (raw_settings.Length != new CL_ServerSettings().ToJSON().Length)
            {
                string path = Path.Combine(ServerSettings.ApplicationPath, "temp.json");
                System.IO.File.WriteAllText(path, raw_settings, Encoding.UTF8);
                try
                {
                    ServerSettings.LoadSettingsFromFile(path, true);
                    return(APIStatus.OK());
                }
                catch
                {
                    return(APIStatus.InternalError("Error while importing settings"));
                }
            }
            return(APIStatus.BadRequest("Empty settings are not allowed"));
        }
Exemplo n.º 29
0
 public ActionResult <ImportFolder> AddFolder(ImportFolder folder)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (folder.ImportFolderLocation == string.Empty)
     {
         return(new APIMessage(StatusCodes.Status400BadRequest,
                               "Bad Request: The Folder path must not be Empty"));
     }
     try
     {
         return(RepoFactory.ImportFolder.SaveImportFolder(folder));
     }
     catch (Exception e)
     {
         return(APIStatus.InternalError(e.Message));
     }
 }
Exemplo n.º 30
0
 /// <summary>
 /// Read json file that is converted into string from .config file of jmmserver
 /// </summary>
 /// <returns></returns>
 private object GetWebUIConfig()
 {
     if (!String.IsNullOrEmpty(ServerSettings.WebUI_Settings))
     {
         try
         {
             WebUI_Settings settings =
                 JsonConvert.DeserializeObject <WebUI_Settings>(ServerSettings.WebUI_Settings);
             return(settings);
         }
         catch
         {
             return(APIStatus.InternalError("error while reading webui settings"));
         }
     }
     else
     {
         return(APIStatus.NotFound());
     }
 }