Example #1
0
        public void GenerateAudioFileFolderInvalidProjectIdThrows()
        {
            // Arrange
            var projectId = Guid.Empty;

            // Assert
            Assert.Throws <ArgumentNullException>(() => FileStorageHelper.AudioFileFolder(projectId));
        }
        // FUTURE Also support other audio formats.
        // TODO This is windows coupled. See https://github.com/tabeckers/AbleSync/issues/19 and https://github.com/tabeckers/AbleSync/issues/28
        // TODO Pick correct audio file, there may be multiple mp3 files.
        /// <summary>
        ///     Execute the upload for an audio file.
        /// </summary>
        /// <remarks>
        ///     This only handles mp3.
        /// </remarks>
        /// <param name="task">The task.</param>
        /// <param name="token">The cancellation token.</param>
        public async Task ExecuteUploadAudioAsync(ProjectTask task, CancellationToken token)
        {
            if (task == null)
            {
                throw new ArgumentNullException(nameof(task));
            }
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }
            if (task.ProjectTaskType != ProjectTaskType.UploadAudio)
            {
                throw new InvalidOperationException(nameof(task.ProjectTaskType));
            }

            _logger.LogTrace($"Starting mp3 audio upload for project {task.ProjectId}");

            var project = await _projectRepository.GetAsync(task.ProjectId, token);

            var directoryInfo = DirectoryInfoHelper.GetFromProject(_options.RootDirectoryPath, project);
            var audioFiles    = directoryInfo.GetFiles().Where(x => Constants.ExportedAudioFileExtensions.Contains(x.Extension));

            // TODO Do elegantly.
            if (!audioFiles.Where(x => x.Extension == Constants.AudioMp3FileExtension).Any())
            {
                // TODO Custom exception.
                throw new InvalidOperationException("Could not get mp3 file in project folder");
            }

            // Get the actual physical file because we need its name.
            var audioFilePhysical = audioFiles.Where(x => x.Extension == Constants.AudioMp3FileExtension).First();

            // Get or create the audio file.
            var audioFileEntity = await _audioFileRepository.ExistsForProjectAsync(task.ProjectId, AudioFormat.Mp3, token)
                ? await _audioFileRepository.GetFromProjectAsync(task.ProjectId, AudioFormat.Mp3, token)
                : await _audioFileRepository.CreateGetAsync(new AudioFile
            {
                AudioFormat = AudioFormat.Mp3,
                Name        = audioFilePhysical.Name,
                ProjectId   = project.Id
            }, token);

            var fileNamePhysical   = audioFilePhysical.Name;
            var folderNamePhysical = $"{DirectoryInfoHelper.ParsePathFromProject(_options.RootDirectoryPath, project)}";

            using var fileStream = new FileStream($"{folderNamePhysical}/{fileNamePhysical}", FileMode.Open, FileAccess.Read);

            var fileNameStorage   = FileStorageHelper.AudioFileName(audioFileEntity.Id);
            var folderNameStorage = FileStorageHelper.AudioFileFolder(project.Id);
            await _blobStorageService.StoreFileAsync(folderNameStorage, fileNameStorage, Constants.ContentTypeMp3, fileStream, token);

            // Mark our synchronization
            await _audioFileRepository.MarkSyncedAsync(audioFileEntity.Id, token);

            _logger.LogTrace($"Finished mp3 audio upload for project {task.ProjectId} - uploaded {fileNamePhysical} as {fileNameStorage}");
        }
Example #3
0
        public void GenerateAudioFileFolder()
        {
            // Arrange.
            var projectId = Guid.NewGuid();

            // Act.
            var result = FileStorageHelper.AudioFileFolder(projectId);

            // Assert.
            Assert.Equal($"{Constants.StorageProjectFolderBase}/{projectId}/{Constants.StorageAudioFilesFolder}", result);
        }