public void ValetKey_GetSasTokenFromSasUrl_Succeeds()
        {
            var sasToken = BlobContainerValet.GetSasTokenFromValetKeyUrl(ValetKeyUrl);

            Assert.IsNotNull(sasToken);
            Assert.IsTrue(sasToken[0] == '?');
        }
        static int GetSnapshotCount(ref DateTimeOffset?mostRecentlyUpdatedAt)
        {
            var valetKeyUrl    = ConfigurationManager.AppSettings["BlobValetKeyUrl"];
            var destinationUrl = ConfigurationManager.AppSettings["BlobDestinationUrl"];
            var blob           = BlobContainerValet.GetCloudBlockBlob(valetKeyUrl, new Uri(destinationUrl));

            var blobRequestOptions = new BlobRequestOptions();
            var snapCount          = 0;

            mostRecentlyUpdatedAt = null;
            foreach (CloudBlockBlob b in blob.Container.ListBlobs(useFlatBlobListing: true, blobListingDetails: BlobListingDetails.Snapshots))
            {
                snapCount++;
                if (mostRecentlyUpdatedAt.HasValue)
                {
                    if (mostRecentlyUpdatedAt.Value < b.SnapshotTime)
                    {
                        mostRecentlyUpdatedAt = b.SnapshotTime;
                    }
                }
                else
                {
                    mostRecentlyUpdatedAt = b.SnapshotTime;
                }
            }

            return(snapCount);
        }
        public void ValetKey_UpdateBlobWithinContainer_Succeeds()
        {
            BlobContainerValet.UploadToBlobContainer(ValetKeyUrl, PublicFileUrl1);
            BlobContainerValet.UploadToBlobContainer(ValetKeyUrl, PublicFileUrl2);
            BlobContainerValet.UploadToBlobContainer(ValetKeyUrl, PublicFileUrl1);
            BlobContainerValet.UploadToBlobContainer(ValetKeyUrl, PublicFileUrl3);

            // should be up there!
        }
        public void ValetKey_GetFileFromValidFileUrl_Succeeds()
        {
            var expected = "expected.txt";
            var source   = @"http://foo/bar/xyz/" + expected;

            var result = BlobContainerValet.GetFileNameFromUrl(source);

            Assert.AreEqual(expected, result);
        }
        static bool WriteToBlobIfChanged(Uri blobUri, string thumbprint, byte[] contents)
        {
            Console.Write("[thumbprint = {0} ... ", thumbprint);

            var valetKeyUrl    = ConfigurationManager.AppSettings["BlobValetKeyUrl"];
            var destinationUrl = ConfigurationManager.AppSettings["BlobDestinationUrl"];


            var blob = BlobContainerValet.GetCloudBlockBlob(valetKeyUrl, new Uri(destinationUrl));

            try
            {
                blob.FetchAttributes();
            }
            catch (Microsoft.WindowsAzure.Storage.StorageException ex)
            {
                // if (Enum.GetName(typeof(RequestResult), ex.RequestInformation.HttpStatusCode) == HttpStatusCode.NotFound))
                if (ex.RequestInformation.HttpStatusCode == Convert.ToInt32(HttpStatusCode.NotFound))
                {
                    // write it the first time
                    WriteBlob(blobUri, thumbprint, contents);
                    Console.WriteLine(" (created)");
                    return(true);
                }
                else
                {
                    throw;
                }
            }
            var oldthumbprint = blob.Metadata["thumbprint"];

            if (oldthumbprint != thumbprint)
            {
                Console.Write("Change detected: {0}... ", DateTime.Now.ToLongTimeString()); // TODO: DateTimeOffset

                var snapshot = blob.CreateSnapshot();
                WriteBlob(blobUri, thumbprint, contents);

                Console.WriteLine("updated.");
                return(true);
            }
            else
            {
                Console.WriteLine("No change at {0}...", DateTime.Now.ToLongTimeString());
                return(false);
            }
        }
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var client = new HttpClient();
            var result =
                client.GetAsync("http://www.mass.gov/dor/docs/dor/law-changes/faqss-computer-software-2013.pdf").Result;
            var contents   = result.Content.ReadAsByteArrayAsync().Result;
            var thumbprint = GetThumbprint(contents);
            var blobUrl    = ConfigurationManager.AppSettings["BlobValetKeyUrl"];
            var blobUri    = BlobContainerValet.GetDestinationPathFromValetKey(blobUrl);

            WriteToBlobIfChanged(blobUri, thumbprint, contents); // TODO: issue if this gets too large

            DateTimeOffset?mostRecentlyUpdatedAt = default(DateTimeOffset);
            int            verCount        = GetSnapshotCount(ref mostRecentlyUpdatedAt);
            var            urlHistoryModel = new UrlHistoryModel()
            {
                Url           = blobUrl,
                LastUpdatedAt = mostRecentlyUpdatedAt,
                VersionCount  = verCount
            };

            return(View(urlHistoryModel));
        }
        public void ValetKey_BuildDestinationUri_RetainsContainerName()
        {
            var destinationUri = BlobContainerValet.BuildDesinationUri(ValetKeyUrl, PublicFileUrl1);

            Assert.AreEqual(destinationUri.AbsoluteUri, DestinationUrl1);
        }
        public void ValetKey_GetPathFromValidValetKey_Succeeds()
        {
            var uri = BlobContainerValet.GetDestinationPathFromValetKey(ValetKeyUrl);

            Assert.IsNotNull(uri);
        }