private void CreateDirectory( AbstractDownloadsDirectory directory) { if (_fileSystem.Directory.Exists(directory) is false) { _logger.LogInformation($"Creating {directory} directory."); } /* If DownloadPath directory already exists this line does nothing. */ _fileSystem.Directory.CreateDirectory(directory); }
/// <summary> /// Naive way to check if we can create files in DownloadPath. Standard .NET tools to perform such check are /// currently only available on Windows. So we just try to create a random file and delete it - we'll get /// an exception if that's not permitted. It is an unrecoverable situation so an exception is appropriate /// here, as it will terminate the app. /// </summary> private void AssertDownloadPathHasWriteAccess( AbstractDownloadsDirectory directory) { var testFilePath = _fileSystem.Path.Combine( directory, $"downloaderPermissionsTest.{Guid.NewGuid()}"); using (_fileSystem.File.Create(testFilePath)) { /* Close and dispose the stream */ } _fileSystem.File.Delete(testFilePath); }
private void CreateDirectoryIfNotExistsAndCheckPermissions( AbstractDownloadsDirectory directory) { try { CreateDirectory(directory); } catch (UnauthorizedAccessException) { throw new DownloadPathAccessDeniedException( $"'{directory}' directory cannot be created"); } try { AssertDownloadPathHasWriteAccess(directory); } catch (UnauthorizedAccessException) { throw new DownloadPathAccessDeniedException( $"'{directory}' directory is not writable"); } }