示例#1
0
        public Streamer CreateStreamerLogic(Streamer body)
        {
            Streamer returnStreamer;

            using (var context = new MainDataContext()) {
                Streamer streamer = context.Streamers.FirstOrDefault(item => item.streamerId == body.streamerId);

                if (streamer == null)
                {
                    // if streamer does not exist in database and want to add
                    _logger.Info("Adding new streamer...");

                    string etag = "";
                    if (GlobalConfig.GetGlobalConfig("contentRootPath") != null)
                    {
                        CreateFolder($"{GlobalConfig.GetGlobalConfig("contentRootPath")}/streamers/{body.streamerId}/");
                        if (!string.IsNullOrEmpty(body.thumbnailLocation))
                        {
                            etag = DownloadHelpers.DownloadFile(body.thumbnailLocation,
                                                                $"{GlobalConfig.GetGlobalConfig("contentRootPath")}/streamers/{body.streamerId}/thumbnail.png");
                        }
                    }


                    streamer = new Streamer {
                        streamerId        = body.streamerId,
                        displayName       = body.displayName,
                        username          = body.username,
                        isLive            = body.isLive ?? false,
                        quality           = body.quality == "{\"resolution\":0,\"fps\":0}" ? null : body.quality,
                        getLive           = body.getLive ?? false,
                        thumbnailLocation = $"streamers/{body.streamerId}/thumbnail.png",
                        thumbnailETag     = etag
                    };

                    context.Streamers.Add(streamer);
                    returnStreamer = streamer;
                } /*else if (streamer != null) {
                   * // if streamer exists then update
                   * Console.WriteLine("Updating streamer...");
                   * streamer.streamerId = body.streamerId;
                   * streamer.displayName = body.displayName;
                   * streamer.username = body.username;
                   * streamer.description = body.description;
                   * streamer.viewCount = body.viewCount;
                   * streamer.thumbnailLocation = $"streamers/{body.streamerId}/thumbnail.png";
                   * returnStreamer = streamer;
                   *
                   * IList<Parameter> headers = downloadHelpers.GetHeaders(body.thumbnailLocation);
                   * for (var x = 0; x < headers.Count; x++) {
                   *    if (headers[x].Name == "ETag") {
                   *        var etag = headers[x].Value;
                   *        if (etag != null) {
                   *            if (streamer.thumbnailETag != etag.ToString().Replace("\"", "")) {
                   *                if (contentRootPath != null)
                   *                    Console.WriteLine("Detected new thumbnail image, downloading...");
                   *                streamer.thumbnailETag = downloadHelpers.DownloadFile(body.thumbnailLocation,
                   *                    $"{contentRootPath.value}/streamers/{body.streamerId}/thumbnail.png");
                   *            }
                   *        }
                   *    }
                   * }
                   * }*/
                else
                {
                    //something strange has happened
                    returnStreamer = new Streamer();
                }

                //if (isNew) {
                //StartupJobs startupJobs = new StartupJobs();
                List <Streamer> streamers = new List <Streamer> {
                    streamer
                };                                                        //lazy
                JobHelpers.NormalJob <CheckForStreamerLiveStatusJob>("CreateStreamerUpdateLiveStatusJob", "CreateStreamerUpdateLiveStatusTrigger", QuartzSchedulers.PrimaryScheduler());
                IJobDetail job = JobBuilder.Create <UpdateStreamerDetailsJob>()
                                 .WithIdentity("UpdateStreamerDetailsJob")
                                 .Build();

                job.JobDataMap.Put("listOfStreamers", streamers);

                var        schedulerFactory = new StdSchedulerFactory(QuartzSchedulers.PrimaryScheduler());
                IScheduler scheduler        = schedulerFactory.GetScheduler().Result;
                scheduler.Start();

                ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
                                         .WithIdentity("UpdateStreamerDetailsTrigger")
                                         .StartNow()
                                         .Build();

                scheduler.ScheduleJob(job, trigger);
                //}

                context.SaveChanges();
            }

            return(returnStreamer);
        }