public static async Task SeedLiveStreams() { IEnumerable <StationDirbleDto> stationDtos; var page = 1; do { stationDtos = await _dirble.GetStations(page); foreach (var stationDto in stationDtos) { var existingLiveStream = _liveStreamService.GetLiveStream(stationDto.Id); if (existingLiveStream != null) { string coverImageName = null; if (stationDto.Image.Url != null) { using (var webClient = new WebClient()) { try { using (var imageStream = webClient.OpenRead(new Uri(stationDto.Image.Url))) { using (var memoryStream = new MemoryStream()) { await imageStream.CopyToAsync(memoryStream); memoryStream.Position = 0; new FileExtensionContentTypeProvider().TryGetContentType(stationDto.Image.Url, out string contentType); try { coverImageName = await _uploadService.UploadCoverImage(stationDto.Image.Url, memoryStream, contentType); } catch (Exception) { } } } } catch (WebException) { } } } var liveStream = new LiveStream { Id = stationDto.Id, Name = stationDto.Name, Country = stationDto.Country, Description = String.IsNullOrWhiteSpace(stationDto.Description) ? null : stationDto.Description, CoverImageName = coverImageName, WebsiteUrl = String.IsNullOrWhiteSpace(stationDto.Website) ? null : stationDto.Website, TwitterUrl = String.IsNullOrWhiteSpace(stationDto.Twitter) ? null : stationDto.Twitter, FacebookUrl = String.IsNullOrWhiteSpace(stationDto.Facebook) ? null : stationDto.Facebook, DateAdded = DateTimeOffset.Parse(stationDto.CreatedAt), DateUpdated = DateTimeOffset.Parse(stationDto.UpdatedAt), Slug = stationDto.Slug, }; foreach (var category in stationDto.Categories) { liveStream.AudioGenres.Add(new AudioGenre { GenreId = category.Id, }); } foreach (var streamDto in stationDto.Streams) { liveStream.StreamDatas.Add(new StreamData { LiveStreamUrl = streamDto.Stream, Bitrate = streamDto.Bitrate == 0 ? null : streamDto.Bitrate, ContentType = streamDto.ContentType == "?" ? null : streamDto.ContentType, }); } _liveStreamService.Add(liveStream); } } page++; } while (stationDtos.Any()); }
public override object MutateAndGetPayload(MutationInputs inputs, ResolveFieldContext <object> context) { var name = inputs.Get <string>("name"); var coverImageName = inputs.Get <string>("coverImageName"); var liveStreamUrl = inputs.Get <string>("liveStreamUrl"); var websiteUrl = inputs.Get <string>("websiteUrl"); var country = inputs.Get <string>("country"); var genreIds = inputs.Get("genreIds", new object[0]).Cast <string>().ToList(); var tags = inputs.Get("tags", new object[0]).Select(x => x.As <Dictionary <string, object> >().ToObject <TagInput>()); var user = context.UserContext.As <Context>().CurrentUser; var placeholderImage = _cloudStorage.CloudBlobContainers[CloudStorageType.AppImage].GetBlockBlobReference(Audio.Image.PlaceholderImageName); var liveStream = new Models.LiveStream { CoverImageName = coverImageName, Name = name, // LiveStreamUrl = liveStreamUrl, WebsiteUrl = websiteUrl, Country = country, }; //foreach (var genreId in genreIds) //{ // liveStream.AudioGenres.Add(new AudioGenre { GenreId = genreId }); //} foreach (var tag in tags) { Tag.Tag tagModel; if (tag.Tag != null) { tagModel = new Tag.Tag { Name = tag.Tag }; } else if (tag.Id.HasValue) { var existingTag = _tagService.GetTag(tag.Id.Value); tagModel = existingTag; } else { throw new Exception("A tag id or name must be supplied"); } if (liveStream.AudioTags.All(x => x.Tag.Id != tagModel.Id)) { liveStream.AudioTags.Add(new AudioTag { Tag = tagModel }); } } _liveStreamService.Add(liveStream); return(new { liveStream }); }