Exemple #1
0
        public async Task <IActionResult> PutPlaylist(string id, Playlist playlist)
        {
            if (playlist == null || playlist.Id == null || id != playlist.Id || playlist.OfferingId == null)
            {
                return(BadRequest());
            }
            var offering = await _context.Offerings.FindAsync(playlist.OfferingId);

            if (offering == null)
            {
                return(BadRequest());
            }
            var authorizationResult = await _authorizationService.AuthorizeAsync(this.User, offering, Globals.POLICY_UPDATE_OFFERING);

            if (!authorizationResult.Succeeded)
            {
                if (User.Identity.IsAuthenticated)
                {
                    return(new ForbidResult());
                }
                else
                {
                    return(new ChallengeResult());
                }
            }
            var p = await _context.Playlists.FindAsync(playlist.Id);

            p.Name = playlist.Name;

            try
            {
                await _context.SaveChangesAsync();

                _wakeDownloader.UpdatePlaylist(playlist.Id);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PlaylistExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <ActionResult <Media> > PostMedia(IFormFile video1, IFormFile video2, [FromForm] string playlistId)
        {
            if (video1 == null)
            {
                return(BadRequest("video1 is compulsory"));
            }
            Media media = new Media
            {
                PlaylistId   = playlistId,
                SourceType   = SourceType.Local,
                JsonMetadata = new JObject()
            };

            // full path to file in temp location
            if (video1.Length > 0)
            {
                if (Path.GetExtension(video1.FileName) != ".mp4")
                {
                    return(BadRequest("File Format not permitted"));
                }
                var filePath = CommonUtils.GetTmpFile();
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await video1.CopyToAsync(stream);
                }
                // Only do this for the first (primary) video
                media.UniqueMediaIdentifier = await FileRecord.ComputeSha256HashForFileAsync(filePath);

                media.JsonMetadata.Add("video1", JsonConvert.SerializeObject(video1));
                media.JsonMetadata.Add("video1Path", filePath);
            }
            // Copy second File
            if (video2 != null && video2.Length > 0)
            {
                if (Path.GetExtension(video2.FileName) != ".mp4")
                {
                    return(BadRequest("File Format not permitted"));
                }
                var filePath = CommonUtils.GetTmpFile();
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await video2.CopyToAsync(stream);
                }
                media.JsonMetadata.Add("video2", JsonConvert.SerializeObject(video2));
                media.JsonMetadata.Add("video2Path", filePath);
            }

            _context.Medias.Add(media);

            await _context.SaveChangesAsync();


            // The following async update of the playlists (and then the media/vido entity tasks) is the probable cause of
            // https://github.com/classtranscribe/WebAPI/issues/92

            // TODO/TOREVIEW Do we kick off multiple updaters during multiple uploads?
            _wakeDownloader.UpdatePlaylist(playlistId);
            //TODO/TOREVIEW: Do we need a way to wait for the playlist to be updated?
            // FrontEnd should see the playlist after the media has been processed
            // WE need to run the DownloadPlaylist task and the Media task to fix up the Video links
            // Calling GetName in DownloadPlayListInfoTask is likley NOT sufficient because
            // UpdatePlaylist alsofires off an additional task
            // So this is an awful short term hack bandaid until we refactor the code and can do te database housekeeping here instead
            // ie. we want to think about how to fix the *design*, not just the symptons.
            await Task.Delay(5000); // milliseconds

            return(CreatedAtAction("GetMedia", new { id = media.Id }, media));
        }