Example #1
0
        public void ShouldFailOnUploadInOneTime()
        {
            SqlLiteDatabaseTest(options =>
            {
                var uploadService = _server.Host.Services.GetService(typeof(IUploadService)) as IUploadService;
                var fileResolver  = _server.Host.Services.GetService(typeof(IFileResolver)) as IFileResolver;

                using (var context = new PlayCatDbContext(options))
                {
                    uploadService.SetDbContext(context);

                    var uploadAudioRequest = new UploadAudioRequest
                    {
                        Artist = "Flume",
                        Song   = "Say It (feat. Tove Lo) (Illenium Remix)",
                        Url    = "https://www.youtube.com/watch?v=80AlC3LaPqQ"
                    };

                    Guid userId = GetUserId(context);

                    Task.Run(() => {
                        uploadService.UploadAudioAsync(userId, uploadAudioRequest);

                        string audioFilePath = fileResolver.GetAudioFolderPath(StorageType.FileSystem);

                        File.Delete(Path.Combine(audioFilePath, "80AlC3LaPqQ.mp3"));
                    });
                    Thread.Sleep(500);
                    UploadResult result = uploadService.UploadAudioAsync(userId, uploadAudioRequest).Result;

                    CheckIfFail(result);
                    Assert.Equal("User already uploading audio", result.Info);
                }
            });
        }
Example #2
0
        public async Task <BaseResult> UploadAudio([FromBody] UploadAudioRequest request)
        {
            CheckTokenResult checkTokenResult = _authService.CheckToken(AccessToken);

            if (!checkTokenResult.Ok)
            {
                return(checkTokenResult);
            }

            return(await _uploadService.UploadAudioAsync(checkTokenResult.AuthToken.UserId, request));
        }
Example #3
0
        public void IsValidUrl()
        {
            SqlLiteDatabaseTest(options =>
            {
                var uploadService = _server.Host.Services.GetService(typeof(IUploadService)) as IUploadService;
                var fileResolver  = _server.Host.Services.GetService(typeof(IFileResolver)) as IFileResolver;

                using (var context = new PlayCatDbContext(options))
                {
                    uploadService.SetDbContext(context);

                    var uploadAudioRequest = new UploadAudioRequest
                    {
                        Artist = "Flume",
                        Song   = "Say It (feat. Tove Lo) (Illenium Remix)",
                        Url    = "https://www.youtube.com/watch?v=80AlC3LaPqQ"
                    };

                    Guid userId         = GetUserId(context);
                    UploadResult result = uploadService.UploadAudioAsync(userId, uploadAudioRequest).Result;

                    _output.WriteLine(JsonConvert.SerializeObject(result));

                    CheckIfSuccess(result);

                    var audio = context.Audios.Single();

                    Assert.Equal("Say It (feat. Tove Lo) (Illenium Remix)", result.Audio.Song);
                    Assert.Equal("Flume", result.Audio.Artist);
                    Assert.Equal("80AlC3LaPqQ", audio.UniqueIdentifier);

                    var audioPlaylists  = context.AudioPlaylists.Single();
                    var generalPlaylist = context.Playlists.Single();

                    Assert.True(generalPlaylist.IsGeneral);
                    Assert.Equal(generalPlaylist.OwnerId, userId);

                    Assert.Equal(generalPlaylist.OrderValue, 1);
                    Assert.Equal(audioPlaylists.Order, 0);

                    Assert.Equal(audioPlaylists.AudioId, audio.Id);
                    Assert.Equal(audioPlaylists.PlaylistId, generalPlaylist.Id);

                    string audioFilePath = fileResolver.GetAudioFolderPath(StorageType.FileSystem);
                    string videoFilePath = fileResolver.GetVideoFolderPath(StorageType.FileSystem);

                    Assert.True(File.Exists(Path.Combine(audioFilePath, "80AlC3LaPqQ.mp3")));
                    Assert.False(File.Exists(Path.Combine(videoFilePath, "80AlC3LaPqQ.mp4")));

                    File.Delete(Path.Combine(audioFilePath, "80AlC3LaPqQ.mp3"));
                }
            });
        }
Example #4
0
        public void IsInvalidModel()
        {
            var uploadService = _server.Host.Services.GetService(typeof(IUploadService)) as IUploadService;

            var uploadAudioRequest = new UploadAudioRequest();

            UploadResult result = uploadService.UploadAudioAsync(Guid.Empty, uploadAudioRequest).Result;

            CheckIfFail(result);

            Assert.Equal("Model is not valid", result.Info);
            Assert.False(result.ShowInfo);
            Assert.NotNull(result.Errors);
            Assert.Equal(result.Errors.Count, 3);
        }
Example #5
0
        public void IsAlreadyExists()
        {
            SqlLiteDatabaseTest(options =>
            {
                var uploadService = _server.Host.Services.GetService(typeof(IUploadService)) as IUploadService;
                var fileResolver  = _server.Host.Services.GetService(typeof(IFileResolver)) as IFileResolver;

                using (var context = new PlayCatDbContext(options))
                {
                    uploadService.SetDbContext(context);

                    var uploadAudioRequest = new UploadAudioRequest
                    {
                        Artist = "Flume",
                        Song   = "Say It (feat. Tove Lo) (Illenium Remix)",
                        Url    = "https://www.youtube.com/watch?v=80AlC3LaPqQ"
                    };

                    Guid userId = GetUserId(context);

                    UploadResult result           = uploadService.UploadAudioAsync(userId, uploadAudioRequest).Result;
                    UploadResult resultDownloaded = uploadService.UploadAudioAsync(userId, uploadAudioRequest).Result;

                    CheckIfFail(resultDownloaded);

                    Assert.Equal("Video already uploaded", resultDownloaded.Info);

                    string audioFilePath = fileResolver.GetAudioFolderPath(StorageType.FileSystem);
                    string videoFilePath = fileResolver.GetVideoFolderPath(StorageType.FileSystem);

                    Assert.True(File.Exists(Path.Combine(audioFilePath, "80AlC3LaPqQ.mp3")));
                    Assert.False(File.Exists(Path.Combine(videoFilePath, "80AlC3LaPqQ.mp4")));

                    File.Delete(Path.Combine(audioFilePath, "80AlC3LaPqQ.mp3"));
                }
            });
        }
Example #6
0
        public async Task <UploadResult> UploadAudioAsync(Guid userId, UploadAudioRequest request)
        {
            return(await BaseInvokeCheckModelAsync(request, async() =>
            {
                User user = _dbContext.Users.FirstOrDefault(x => x.Id == userId);
                if (user == null)
                {
                    throw new Exception("User not found, but token does");
                }

                var responseBuilder =
                    ResponseBuilder <UploadResult>
                    .Fail();

                if (user.IsUploadingAudio)
                {
                    return responseBuilder.SetInfoAndBuild("User already uploading audio");
                }

                user.IsUploadingAudio = true;
                _dbContext.SaveChanges();

                using (var transaction = _dbContext.Database.BeginTransaction())
                {
                    try
                    {
                        GetInfoResult result = await GetInfoAsync(new UrlRequest {
                            Url = request.Url
                        });

                        if (!result.Ok)
                        {
                            return responseBuilder
                            .SetErrors(result.Errors)
                            .SetInfoAndBuild(result.Info);
                        }

                        string videoId = UrlFormatter.GetYoutubeVideoIdentifier(request.Url);

                        IFile videoFile = await _saveVideo.SaveAsync(request.Url);
                        IFile audioFile = await _extractAudio.ExtractAsync(videoFile);

                        //TODO: create upload for FileSystem, Blob, etc...
                        string accessUrl = _uploadAudio.Upload(audioFile, StorageType.FileSystem);

                        var generalPlayList = _dbContext.Playlists.FirstOrDefault(x => x.OwnerId == userId && x.IsGeneral);

                        if (generalPlayList == null)
                        {
                            throw new Exception("Playlist not found");
                        }

                        var audio = new Audio
                        {
                            Id = Guid.NewGuid(),
                            AccessUrl = accessUrl,
                            DateCreated = DateTime.Now,
                            Artist = request.Artist,
                            Song = request.Song,
                            Duration = audioFile.Duration,
                            Extension = audioFile.Extension,
                            FileName = audioFile.Filename,
                            UniqueIdentifier = videoId,
                            UploaderId = userId
                        };

                        var audioPlaylist = new AudioPlaylist
                        {
                            AudioId = audio.Id,
                            DateCreated = DateTime.Now,
                            PlaylistId = generalPlayList.Id,
                            Order = generalPlayList.OrderValue
                        };


                        //update max index in playlist
                        generalPlayList.OrderValue++;

                        //add entities
                        _dbContext.AudioPlaylists.Add(audioPlaylist);
                        Audio audioEntity = _dbContext.Audios.Add(audio).Entity;

                        _dbContext.SaveChanges();

                        transaction.Commit();
                        return ResponseBuilder <UploadResult> .SuccessBuild(new UploadResult
                        {
                            Audio = AudioMapper.ToApi.FromData(audioEntity)
                        });
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw ex;
                    }
                    finally
                    {
                        //skip upload process
                        user.IsUploadingAudio = false;

                        _dbContext.SaveChanges();
                    }
                }
            }));
        }