private static void ConfigureForLocalFileSystem(ContainerBuilder builder, IGalleryConfigurationService configuration)
        {
            builder.RegisterType <FileSystemFileStorageService>()
            .AsSelf()
            .As <IFileStorageService>()
            .SingleInstance();

            builder.RegisterInstance(NullReportService.Instance)
            .AsSelf()
            .As <IReportService>()
            .SingleInstance();

            builder.RegisterInstance(NullStatisticsService.Instance)
            .AsSelf()
            .As <IStatisticsService>()
            .SingleInstance();

            // Setup auditing
            var auditingPath = Path.Combine(
                FileSystemFileStorageService.ResolvePath(configuration.Current.FileStorageDirectory),
                FileSystemAuditingService.DefaultContainerName);

            builder.RegisterInstance(new FileSystemAuditingService(auditingPath, FileSystemAuditingService.GetAspNetOnBehalfOf))
            .AsSelf()
            .As <AuditingService>()
            .SingleInstance();

            // If we're not using azure storage, then aggregate stats comes from SQL
            builder.RegisterType <SqlAggregateStatsService>()
            .AsSelf()
            .As <IAggregateStatsService>()
            .InstancePerLifetimeScope();
        }
Exemplo n.º 2
0
            public async Task WillThrowIfFileExistsAndOverwriteFalseAndRealFileSystemIsUsed()
            {
                // Arrange
                using (var testDirectory = TestDirectory.Create())
                {
                    var fileSystemService = new FileSystemService();

                    var configuration = new Mock <IAppConfiguration>();
                    configuration
                    .Setup(x => x.FileStorageDirectory)
                    .Returns(testDirectory);

                    var service = new FileSystemFileStorageService(
                        configuration.Object,
                        fileSystemService);

                    var directory = Path.Combine(testDirectory, FolderName);
                    var filePath  = Path.Combine(directory, FileName);
                    Directory.CreateDirectory(directory);
                    File.WriteAllText(filePath, FileContent);

                    // Act
                    var exception = await Assert.ThrowsAsync <InvalidOperationException>(() => service.SaveFileAsync(
                                                                                             FolderName,
                                                                                             FileName,
                                                                                             new MemoryStream(),
                                                                                             overwrite: false));

                    Assert.True(File.Exists(filePath), $"The file at path {filePath} should exist, but does not.");
                    Assert.Equal(FileContent, File.ReadAllText(filePath));
                }
            }
Exemplo n.º 3
0
            public async Task WillOverwriteFileIfOverwriteTrueAndRealFileSystemIsUsed()
            {
                // Arrange
                using (var testDirectory = TestDirectory.Create())
                {
                    var fileSystemService = new FileSystemService();

                    var configuration = new Mock <IAppConfiguration>();
                    configuration
                    .Setup(x => x.FileStorageDirectory)
                    .Returns(testDirectory);

                    var service = new FileSystemFileStorageService(
                        configuration.Object,
                        fileSystemService);

                    var directory = Path.Combine(testDirectory, FolderName);
                    var filePath  = Path.Combine(directory, FileName);
                    Directory.CreateDirectory(directory);
                    File.WriteAllText(filePath, string.Empty);

                    // Act
                    await service.SaveFileAsync(
                        FolderName,
                        FileName,
                        new MemoryStream(Encoding.ASCII.GetBytes(FileContent)),
                        overwrite : true);

                    Assert.True(File.Exists(filePath), $"The file at path {filePath} should exist, but does not.");
                    Assert.Equal(FileContent, File.ReadAllText(filePath));
                }
            }
        private static IAuditingService GetAuditingServiceForLocalFileSystem(IGalleryConfigurationService configuration)
        {
            var auditingPath = Path.Combine(
                FileSystemFileStorageService.ResolvePath(configuration.Current.FileStorageDirectory),
                FileSystemAuditingService.DefaultContainerName);

            return(new FileSystemAuditingService(auditingPath, AuditActor.GetAspNetOnBehalfOfAsync));
        }
Exemplo n.º 5
0
            private static async Task <bool> SaveFileAsync(FileSystemFileStorageService service, string fileName, Barrier barrier)
            {
                await Task.Yield();

                try
                {
                    barrier.SignalAndWait();
                    await service.SaveFileAsync(
                        FolderName,
                        fileName,
                        new MemoryStream(),
                        overwrite : false);

                    return(true);
                }
                catch
                {
                    return(false);
                }
            }
            public TheCopyFileAsyncMethod()
            {
                _directory = TestDirectory.Create();

                _srcFolderName  = "validation";
                _srcFileName    = "4b6f16cc-7acd-45eb-ac21-33f0d927ec14/nuget.versioning.4.5.0.nupkg";
                _destFolderName = "packages";
                _destFileName   = "nuget.versioning.4.5.0.nupkg";

                _appConfiguration = new Mock <IAppConfiguration>();
                _appConfiguration
                .Setup(x => x.FileStorageDirectory)
                .Returns(() => _directory);

                _fileSystemService = new Mock <FileSystemService> {
                    CallBase = true
                };

                _target = new FileSystemFileStorageService(
                    _appConfiguration.Object,
                    _fileSystemService.Object);
            }
Exemplo n.º 7
0
            public async Task WillThrowIfFileExistsWhenManyThreadsAreTryingToSaveWithoutOverwriting()
            {
                // Arrange
                using (var testDirectory = TestDirectory.Create())
                {
                    var fileSystemService = new FileSystemService();

                    var configuration = new Mock <IAppConfiguration>();
                    configuration
                    .Setup(x => x.FileStorageDirectory)
                    .Returns(testDirectory);

                    var service = new FileSystemFileStorageService(
                        configuration.Object,
                        fileSystemService);

                    for (var i = 0; i < 10; i++)
                    {
                        var fileName = FileName + i;
                        var barrier  = new Barrier(TaskCount);
                        var tasks    = new List <Task <bool> >();

                        // Act
                        for (var taskIndex = 0; taskIndex < TaskCount; taskIndex++)
                        {
                            var task = SaveFileAsync(service, fileName, barrier);
                            tasks.Add(task);
                        }

                        var results = await Task.WhenAll(tasks);

                        // Assert
                        // One task should succeed. One should fail.
                        Assert.Equal(1, results.Count(success => success));
                        Assert.Equal(TaskCount - 1, results.Count(success => !success));
                    }
                }
            }