Beispiel #1
0
        public void Can_Get_Storage_Info()
        {
            // Arrange
            string currentDir = GetCurrentDir();

            var partitionId    = Guid.NewGuid();
            var cabinetStorage = new PhysicalFileStorageProvider(currentDir, partitionId);

            var cabinet = new Cabinet(cabinetStorage);
            var fileDir = "/foo/bar/";

            cabinet.Storage.CreateFile(new StringFileInfo("super content", "baz.txt"), fileDir);

            var storageInfo = cabinet.StorageInfo;
            var size        = storageInfo.CalculateUsedStorageSize();

            // add another file
            cabinet.Storage.CreateFile(new StringFileInfo("super content", "bat.txt"), fileDir);

            // get size
            var newSize = storageInfo.CalculateUsedStorageSize();

            Assert.Equal(size * 2, newSize);
            var readable = StorageInfo.GetBytesReadable(newSize);
        }
Beispiel #2
0
        public ICabinet Build()
        {
            // Base physical folder needs to exist. This is the folder where the tenant specific folder will be created within.
            if (!Directory.Exists(BaseFolder))
            {
                Directory.CreateDirectory(BaseFolder);
            }

            var cabinetStorage = new PhysicalFileStorageProvider(BaseFolder, PartitionId);

            return(new Cabinet(cabinetStorage, _parentFileProvider));
        }
Beispiel #3
0
        public void Cannot_Add_File_Above_Root(string dir)
        {
            string currentDir = GetCurrentDir();

            var partitionId    = Guid.NewGuid();
            var cabinetStorage = new PhysicalFileStorageProvider(currentDir, partitionId);

            var cabinet = new Cabinet(cabinetStorage);

            Assert.Throws <ArgumentException>(
                () => cabinet.Storage.CreateFile(new StringFileInfo("hi there", "baz.txt"), dir));
        }
Beispiel #4
0
        public void Cannot_Add_File_From_DirectoryFileInfo()
        {
            string currentDir = GetCurrentDir();

            var partitionId    = Guid.NewGuid();
            var cabinetStorage = new PhysicalFileStorageProvider(currentDir, partitionId);

            var cabinet = new Cabinet(cabinetStorage);
            var fileDir = "/foo/bar/";

            Assert.Throws <ArgumentOutOfRangeException>(
                () => cabinet.Storage.CreateFile(new DirectoryFileInfo("baz"), fileDir));
        }
Beispiel #5
0
        public void Can_Read_From_File_And_Write_To_File()
        {
            string currentDir = GetCurrentDir();

            var partitionId    = Guid.NewGuid();
            var cabinetStorage = new PhysicalFileStorageProvider(currentDir, partitionId);

            var cabinet = new Cabinet(cabinetStorage);
            var fileDir = "/foo/bar/";

            cabinet.Storage.CreateFile(new StringFileInfo("super content", "baz.txt"), fileDir);

            // Now ma
            var filePath     = fileDir + "baz.txt";
            var existingFile = cabinet.FileProvider.EnsureFile(filePath);


            cabinet.Storage.ReplaceFileContents(filePath, (content) =>
            {
                return(content + " read");
            });

            //// read each line of content from the file, and write it back with " read" appended".
            //using (var readStream = new StreamReader(existingFile.CreateReadStream()))
            //{
            //    cabinet.Storage.OpenWrite(filePath, (writeStream) =>
            //    {
            //        using (var writer = new StreamWriter(writeStream))
            //        {
            //            while (!readStream.EndOfStream)
            //            {
            //                var readLine = readStream.ReadLine();
            //                writer.WriteLine(readLine + " read");
            //            }

            //            writer.Flush();
            //        }
            //    });

            //}

            var updatedFile = cabinet.FileProvider.EnsureFile(filePath);

            // make sure the contents have been amended.
            var contents = updatedFile.ReadAllContent();

            Assert.Equal(contents, "super content read");
        }
Beispiel #6
0
        public void Can_Add_File_From_FileInfo_And_Null_Directory()
        {
            string currentDir = GetCurrentDir();

            var partitionId    = Guid.NewGuid();
            var cabinetStorage = new PhysicalFileStorageProvider(currentDir, partitionId);

            var    cabinet = new Cabinet(cabinetStorage);
            string fileDir = null;

            cabinet.Storage.CreateFile(new StringFileInfo("super content", "baz.txt"), fileDir);

            // Ensure we can read the file to verify it was created.
            var existingFile = cabinet.FileProvider.EnsureFile("baz.txt");
            var contents     = existingFile.ReadAllContent();

            Assert.Equal(contents, "super content");
        }
Beispiel #7
0
        public void Can_Override_File(string fileDir)
        {
            var partitionId = Guid.NewGuid();

            string currentDir = GetCurrentDir();

            // system
            var      systemFilesPartionId = new Guid("43bf6778-ff16-41c8-a72c-cd319d84b8bb");
            var      systemStorage        = new PhysicalFileStorageProvider(currentDir, partitionId);
            ICabinet system = new Cabinet(systemStorage);

            // create a system file /foo/bar/baz.txt
            // var fileDir = "/foo/bar/";
            system.Storage.CreateFile(new StringFileInfo("super content", "baz.txt"), fileDir);

            // Module A has access to system, and so can override system.
            var      moduleAPartitionId = new Guid("cbf0c93a-8840-46ba-921c-b85e81265c81");
            var      moduleAStorage     = new PhysicalFileStorageProvider(currentDir, moduleAPartitionId);
            ICabinet moduleA            = new Cabinet(moduleAStorage, system.FileProvider);

            var filePath   = fileDir.TrimEnd('/') + '/' + "baz.txt";
            var systemFile = moduleA.FileProvider.EnsureFile(filePath);
            var contents   = systemFile.ReadAllContent();

            Assert.Equal(contents, "super content");

            // now override it for moduleA.
            moduleA.Storage.CreateFile(new StringFileInfo("MODULE A OVERRIDE", "baz.txt"), fileDir);

            var updatedFile = moduleA.FileProvider.EnsureFile(filePath);

            contents = updatedFile.ReadAllContent();
            Assert.Equal(contents, "MODULE A OVERRIDE");

            // make sure system module unmodified.
            updatedFile = system.FileProvider.EnsureFile(filePath);
            contents    = updatedFile.ReadAllContent();
            Assert.Equal(contents, "super content");
            //var systemFile = moduleACabinet.FileProvider.GetFileInfo("/foo/bar/baz.txt");
        }
Beispiel #8
0
        public void Cannot_OpenFile_For_Write_Whilst_Being_Read()
        {
            string currentDir = GetCurrentDir();

            var partitionId    = Guid.NewGuid();
            var cabinetStorage = new PhysicalFileStorageProvider(currentDir, partitionId);

            var cabinet = new Cabinet(cabinetStorage);
            var fileDir = "/foo/bar/";

            cabinet.Storage.CreateFile(new StringFileInfo("super content", "baz.txt"), fileDir);

            // Now ma
            var filePath     = fileDir + "baz.txt";
            var existingFile = cabinet.FileProvider.EnsureFile(filePath);


            // read each line of content from the file, and write it back with " read" appended".
            using (var readStream = new StreamReader(existingFile.CreateReadStream()))
            {
                Assert.Throws <IOException>(() =>
                {
                    cabinet.Storage.OpenWrite(filePath, (writeStream) =>
                    {
                        using (var writer = new StreamWriter(writeStream))
                        {
                            while (!readStream.EndOfStream)
                            {
                                var readLine = readStream.ReadLine();
                                writer.WriteLine(readLine + " read");
                            }
                            writer.Flush();
                        }
                    });
                });
            }
        }
Beispiel #9
0
        public void Can_Delete_File()
        {
            // Arrange
            string currentDir = GetCurrentDir();

            var partitionId    = Guid.NewGuid();
            var cabinetStorage = new PhysicalFileStorageProvider(currentDir, partitionId);

            var cabinet = new Cabinet(cabinetStorage);
            var fileDir = "/foo/bar/";

            cabinet.Storage.CreateFile(new StringFileInfo("super content", "baz.txt"), fileDir);


            // Act
            cabinet.Storage.DeleteFile(fileDir + "baz.txt");


            // Assert
            Assert.Throws <FileNotFoundException>(() =>
            {
                var existingFile = cabinet.FileProvider.EnsureFile(fileDir + "baz.txt");
            });
        }