Exemplo n.º 1
0
        public ActionResult Index(Models.SetupModel Model)
        {
            #if(DEBUG)
            System.Threading.Thread.Sleep(2000);
            #endif
            try
            {
                if (!new Models.Configuration().FirstRun)
                    return RedirectToAction("Login", "Account");
            }
            catch (Exception)
            {
                // db likely not created or setup.
            }

            if (!ModelState.IsValid)
                return View();

            // is valid

            // load recording directories from NextPVR and import as shared directories.
            #region import recording directories
            string defaultRecordingDirectory = Models.NextPvrConfigHelper.DefaultRecordingDirectory;
            KeyValuePair<string, string>[] extras = Models.NextPvrConfigHelper.ExtraRecordingDirectories;

            if (!String.IsNullOrWhiteSpace(defaultRecordingDirectory))
                new Models.RecordingDirectory() { Name = "Default", UserOid = Globals.SHARED_USER_OID, Path = defaultRecordingDirectory, Username = Globals.SHARED_USER_USERNAME, IsDefault = true }.Save();
            foreach (var extra in extras)
            {
                // make sure there aren't any directories from NextPVRWebConsole
                string name = "[{0}]".FormatStr(extra.Key);
                if (name.StartsWith("[Shared - "))
                    name = "[" + name.Substring("[Shared - ".Length);
                if (Regex.IsMatch(name, @"^\[[^\]\-]+\-[^\]]+\]$"))
                    continue; // must be a user directory, so skip it
                if (name.StartsWith("[") && name.EndsWith("]"))
                    name = name.Substring(1, name.Length - 2);
                new Models.RecordingDirectory() { Name = name, UserOid = Globals.SHARED_USER_OID, Path = extra.Value, Username = Globals.SHARED_USER_USERNAME }.Save();
            }
            #endregion

            var db = DbHelper.GetDatabase(false);
            #region import channels and groups
            db.BeginTransaction();
            try
            {
                // insert channels
                var channels = NUtility.Channel.LoadAll().OrderBy(x => x.Number).Select(x => new Models.Channel() { Oid = x.OID, Name = x.Name, Number = x.Number, Enabled = true }).ToArray();
                foreach (var c in channels)
                    db.Insert("channel", "oid", false, c);

                // insert groups
                var groups = NUtility.Channel.GetChannelGroups().Where(x => x != "All Channels" && x != "All TV Channels").Select(x => new Models.ChannelGroup() { Name = x, UserOid = Globals.SHARED_USER_OID }).ToArray();
                for (int i = 0; i < groups.Length; i++)
                {
                    groups[i].OrderOid = i + 1;
                    db.Insert("channelgroup", "oid", true, groups[i]);
                    foreach (int channelOid in NUtility.Channel.LoadForGroup(groups[i].Name).Select(x => x.OID))
                        db.Execute("insert into [channelgroupchannel](channelgroupoid, channeloid) values (@0, @1)", groups[i].Oid, channelOid);
                }

                db.CompleteTransaction();
            }
            catch (Exception ex) { db.AbortTransaction(); throw ex; }
            #endregion

            // create user, do this after importing folders, otherwise this users new folder with have an oid lower than the origial defaults (not a big deal, just prettier this way)
            var user = Models.User.CreateUser(Model.Username, Model.EmailAddress, Model.Password, Globals.USER_ROLE_ALL, true, DateTime.UtcNow);
            if (user == null)
                throw new Exception("Failed to create user: "******"/") + 1);

            // turn off first run
            config.FirstRun = false;
            config.Save();
            db.CompleteTransaction();

            return RedirectToAction("Index", "Home");
        }