Beispiel #1
0
        public async Task AppendTextWorksWithSpecificCulture()
        {
            // --- Arrange
            const string BODY = "1,25-1,25";
            var          wfs  = new AzureFileStorage(ROOT);
            var          file = new AbstractFileDescriptor("Container", null, "TestFile.txt");

            using (var textFile = await wfs.CreateTextAsync(file, new CultureInfo("hu-hu")))
            {
                textFile.Writer.Write(1.25);
            }

            // --- Act
            using (var textFile = await wfs.AppendTextAsync(file, new CultureInfo("hu-hu")))
            {
                textFile.Writer.Write(-1.25);
            }

            // --- Assert
            using (var savedFile = await wfs.OpenTextAsync(file))
            {
                var text = savedFile.Reader.ReadToEnd();
                text.ShouldBe(BODY);
            }
        }
        public string CreateAssets()
        {
            var storage = new AzureFileStorage();
            var sb      = new StringBuilder();

            string[] assets = new string[] {
                "parchment.jpg",
                "parchment2.jpg",
                "parchment3.jpg",
                "YeOldeParchment4.png",
                "marble1.jpg",
                "paper.jpg",
                "home-parchment22.gif",
                "cloud1.png",
                "cloud2.png",
                "cloud3.png",
                "10277-m-001.wav",
            };

            foreach (var asset in assets)
            {
                byte[] fileData = null;
                using (
                    var localFile = System.IO.File.Open(System.Web.Hosting.HostingEnvironment.MapPath("~/Content/" + asset),
                                                        System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
                {
                    fileData = new byte[localFile.Length];
                    localFile.Read(fileData, 0, fileData.Length);
                }

                storage.Store(string.Concat(ConfigurationManager.AppSettings["AssetsRelativePath"], asset), fileData, true);
                sb.AppendLine(string.Format("Stored {0}", asset));
            }
            return(sb.ToString());
        }
Beispiel #3
0
        public async Task RemoveContainerFailsWithNonEmptyContainer()
        {
            // --- Arrange
            const string CONTAINER = "NonEmptyContainer";
            const string FOLDER    = @"C:\Temp\AbstractFiles";
            var          afs       = new AzureFileStorage(STORAGE_IN_APP_CONFIG);
            await afs.CreateContainerAsync("Container7");

            await afs.CreateContainerAsync(CONTAINER);

            if (!Directory.Exists(FOLDER))
            {
                Directory.CreateDirectory(FOLDER);
            }
            var filePath = Path.Combine(FOLDER, "file.txt");

            File.WriteAllText(filePath, "TextContents");
            var blobClient = afs.StorageAccount.CreateCloudBlobClient();
            var container  = blobClient.GetContainerReference(CONTAINER.ToLower());
            var blockBlob  = container.GetBlockBlobReference("myblob1");

            using (var fileStream = File.OpenRead(filePath))
            {
                blockBlob.UploadFromStream(fileStream);
            }

            // --- Act
            var removed = await afs.RemoveContainerAsync(CONTAINER);

            // --- Assert
            removed.ShouldBeTrue();
            (await afs.ContainerExistsAsync(CONTAINER)).ShouldBeFalse();
        }
Beispiel #4
0
        public async Task CreateOrAppendTextWorksWithExistingFile()
        {
            // --- Arrange
            const string BODY = "FirstSecond";
            var          wfs  = new AzureFileStorage(ROOT);
            var          file = new AbstractFileDescriptor("Container", null, "TestFile.txt");

            using (var textFile = await wfs.CreateTextAsync(file))
            {
                textFile.Writer.Write("First");
            }

            // --- Act
            using (var textFile = await wfs.CreateOrAppendTextAsync(file))
            {
                textFile.Writer.Write("Second");
            }

            // --- Assert
            using (var savedFile = await wfs.OpenTextAsync(file))
            {
                var text = savedFile.Reader.ReadToEnd();
                text.ShouldBe(BODY);
            }
        }
Beispiel #5
0
        static async Task Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("Pass first argument with connection string to database, second with file storage type (fs/azure), third a connection to azure storage or local path template (relative path not supported).");
                return;
            }

            string connectionString = args[0];

            Console.WriteLine("Creating context.");
            using var entries = new EntriesDataContext(DbContextOptions <EntriesDataContext>(connectionString, "Entries"), Schema <EntriesDataContext>("Entries"));

            var imageFormat = ImageFormatDefinition.Jpeg;

            IFileStorage fileStorage = null;
            string       storageType = args[1];

            if (storageType == "fs")
            {
                fileStorage = new SystemIoFileStorage(path => path, Options.Create(new SystemIoStorageOptions()
                {
                    PathTemplate = args[2]
                }), imageFormat);
            }
            else if (storageType == "azure")
            {
                fileStorage = new AzureFileStorage(Options.Create(new AzureStorageOptions()
                {
                    ConnectionString = args[2]
                }));
            }
            else
            {
                Console.WriteLine($"Not supported type of file storage '{storageType}'.");
                return;
            }

            var resizeService = new ImageResizeService(imageFormat);

            Console.WriteLine("Getting images.");
            var images = await entries.Images
                         .Include(i => i.Entry)
                         //.Where(i => i.OriginalWidth == 0 || i.OriginalHeight == 0)
                         .ToListAsync();

            Console.WriteLine($"Found '{images.Count}' images.");
            foreach (var image in images)
            {
                if (!await TryUpdateOriginalSizeAsync(entries, fileStorage, resizeService, image, ImageType.Original))
                {
                    await TryUpdateOriginalSizeAsync(entries, fileStorage, resizeService, image, ImageType.Preview);
                }
            }

            Console.WriteLine("Saving changes.");
            await entries.SaveChangesAsync();

            Console.WriteLine("Done.");
        }
Beispiel #6
0
        public void ConstructionWorksWithAppConfig()
        {
            // --- Act
            var afs = new AzureFileStorage(STORAGE_IN_APP_CONFIG);

            // --- Assert
            afs.StorageAccount.ShouldNotBeNull();
            afs.StorageAccount.Credentials.AccountName.ShouldBe(STORAGE_NAME);
        }
Beispiel #7
0
        public void ConstructionWorksWithAccountCredentials()
        {
            // --- Act
            var afs = new AzureFileStorage(STORAGE_ACCOUNT_INFO);

            // --- Assert
            afs.StorageAccount.ShouldNotBeNull();
            afs.StorageAccount.Credentials.AccountName.ShouldBe(STORAGE_NAME);
        }
Beispiel #8
0
        public async Task CreateContainerFailsWithExistingName()
        {
            // --- Arrange
            const string CONTAINER = "Container2";
            var          afs       = new AzureFileStorage(STORAGE_IN_APP_CONFIG);
            await afs.CreateContainerAsync(CONTAINER);

            // --- Act
            await afs.CreateContainerAsync("Container2");
        }
Beispiel #9
0
        protected override void Initialize()
        {
            var afs = new AzureFileStorage();

            // hmm
            Container.RegisterInstance <IStorage>(afs);
            //this.RegisterType<ILinkBuilder, MockLink>();
            Container.RegisterType <ISaltBuilder, SaltBuilder>();
            Container.RegisterType <IPasswordHasher, PasswordHasher>();
            //thisis.RegisterType<IHash, SHA256Hash>();
            Container.RegisterInstance <IHash>(new SHA256Hash());//(this.Resolve<SHA256Hash>());
            Container.RegisterType <IAccessVerifier, AccessVerifier>();
        }
Beispiel #10
0
        public async Task DeleteWorksWithNonExistingFile()
        {
            // --- Arrange
            var wfs  = new AzureFileStorage(ROOT);
            var file = new AbstractFileDescriptor("Container", null, "TestFile.txt");
            await wfs.DeleteAsync(file);

            // --- Act
            var deleted = await wfs.DeleteAsync(file);

            // --- Assert
            deleted.ShouldBeFalse();
            (await wfs.ExistsAsync(file)).ShouldBeFalse();
        }
Beispiel #11
0
        public async Task RemoveContainerWorksWithNonExistingContainer()
        {
            // --- Arrange
            const string CONTAINER = "NonExisting";
            var          afs       = new AzureFileStorage(STORAGE_IN_APP_CONFIG);
            await afs.CreateContainerAsync("Container5");

            // --- Act
            var removed = await afs.RemoveContainerAsync(CONTAINER);

            // --- Assert
            removed.ShouldBeFalse();
            (await afs.ContainerExistsAsync(CONTAINER)).ShouldBeFalse();
        }
Beispiel #12
0
        public async Task GetContainersWorks()
        {
            // --- Arrange
            var afs = new AzureFileStorage(STORAGE_IN_APP_CONFIG);
            await afs.CreateContainerAsync("Container3");

            await afs.CreateContainerAsync("Container4");

            // --- Act
            var containers = await afs.GetContainersAsync();

            // --- Assert
            containers.Count.ShouldBeGreaterThanOrEqualTo(2);
        }
Beispiel #13
0
        public async Task CreateContainerWorksWithNewName()
        {
            // --- Arrange
            const string CONTAINER = "Container1";
            var          afs       = new AzureFileStorage(STORAGE_IN_APP_CONFIG);
            var          before    = await afs.ContainerExistsAsync(CONTAINER);

            // --- Act

            await afs.CreateContainerAsync("Container1");

            // --- Assert
            before.ShouldBeFalse();
            (await afs.ContainerExistsAsync(CONTAINER)).ShouldBeTrue();
        }
Beispiel #14
0
        private static void RemoveContainers()
        {
            var afs = new AzureFileStorage(STORAGE_IN_APP_CONFIG);

            Task.WaitAll(
                afs.RemoveContainerAsync("Container1", true),
                afs.RemoveContainerAsync("Container2", true),
                afs.RemoveContainerAsync("Container3", true),
                afs.RemoveContainerAsync("Container4", true),
                afs.RemoveContainerAsync("Container5", true),
                afs.RemoveContainerAsync("Container6", true),
                afs.RemoveContainerAsync("Container7", true),
                afs.RemoveContainerAsync("EmptyContainer", true),
                afs.RemoveContainerAsync("NonEmptyContainer", true));
        }
        public AzureFileStorageTests(AzureStorageTestFixture fixture)
        {
            var storageAccount = fixture.StorageAccount;

            var storageClient = storageAccount.CreateCloudFileClient();

            var fileShareName = Guid.NewGuid().ToString();

            _fileShare = storageClient.GetShareReference(fileShareName);

            _fileShare.CreateIfNotExistsAsync()
            .GetAwaiter()
            .GetResult();

            _sut = new AzureFileStorage(storageClient, fileShareName);
        }
Beispiel #16
0
        public async Task DeleteWorksWithExistingFile()
        {
            // --- Arrange
            var wfs  = new AzureFileStorage(ROOT);
            var file = new AbstractFileDescriptor("Container", null, "TestFile.txt");

            using (var textFile = await wfs.CreateTextAsync(file))
            {
                textFile.Writer.Write("Awesome");
            }

            // --- Act
            var deleted = await wfs.DeleteAsync(file);

            // --- Assert
            deleted.ShouldBeTrue();
            (await wfs.ExistsAsync(file)).ShouldBeFalse();
        }
Beispiel #17
0
        static void Main(string[] args)
        {
            Config.Setup("appsettings.json", Directory.GetCurrentDirectory(), null, "MistwareFilesTest");
            string connection = Config.Get("AzureConnectionString");
            string container  = Config.Get("AzureContainer");
            string root       = Config.Get("LocalFilesRoot");
            string logs       = Config.Get("Logs");
            string testfile   = "Test.log";
            string testfolder = "ZZTestZZ";

            // Setup Testing - creates a file called Test.log in ~/Temp
            Directory.SetCurrentDirectory(root);
            Log.Me.LogFile = testfile;
            Log.Me.Info("Hello, World");
            long length = (new FileInfo(testfile)).Length;

            Directory.CreateDirectory(testfolder);
            string dirsep   = Path.DirectorySeparatorChar.ToString();
            string upload   = root;
            string download = root + dirsep + testfolder;

            IFile local = new LocalFileStorage(root);

            Directory.SetCurrentDirectory(root);
            FileTest(local, "Local", testfile, upload, "Test1", "Test2", download, length, true);
            Console.WriteLine("");

            IFile azure = new AzureFileStorage(connection, container);

            FileTest(azure, "Azure", testfile, upload, "Test1", "Test2", download, length, true);

            // Clear up test artifacts
            Directory.SetCurrentDirectory(download);
            File.Delete(testfile);
            Directory.SetCurrentDirectory(upload);
            Directory.Delete(testfolder);
            File.Delete(testfile);

            // Test Azure Blob Logging
            Log.Me.Logger  = new BlobLogger(connection, logs);
            Log.Me.LogFile = "LoggerTest.log";
            Log.Me.Info("This is a test");
        }
Beispiel #18
0
        public async Task CreateTextWorksAsExpected()
        {
            // --- Arrange
            const string BODY = "This is a text file";
            var          wfs  = new AzureFileStorage(ROOT);
            var          file = new AbstractFileDescriptor("Container", null, "TestFile.txt");

            // --- Act
            using (var textFile = await wfs.CreateTextAsync(file))
            {
                textFile.Writer.Write(BODY);
            }

            // --- Assert
            using (var savedFile = await wfs.OpenTextAsync(file))
            {
                var text = savedFile.Reader.ReadToEnd();
                text.ShouldBe(BODY);
            }
        }
        public JsonResult UploadFile()
        {
            HttpPostedFileBase file = HttpContext.Request.Files[0];

            var    uploadLocation = Path.GetTempPath();
            string result         = "";

            if (file != null)
            {
                var image            = Image.FromStream(file.InputStream, true, true);
                var fileNameWithPath = ProccessImage(new Bitmap(image), 200, 250, 75, uploadLocation, file.FileName);

                var fileSystem = new AzureFileStorage();
                var imageUrl   = fileSystem.SaveImage(fileNameWithPath);


                var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                result = serializer.Serialize(new { name = file.FileName, imgPath = imageUrl });
            }

            return(Json(result));
        }
Beispiel #20
0
 public AppendBlobHandler(AppendBlobClient blobClient, AzureFileStorage azureFileStorage)
 {
     _blobClient       = blobClient;
     _azureFileStorage = azureFileStorage;
 }
 public AzureUpload(AzureFileStorage azureFileStorage)
 {
     storage = azureFileStorage;
 }