public IEnumerable <BlobDirectory> GetSubdirectories(bool flat)
        {
            if (!_blobsInitialized)
            {
                InitializeBlobs();
            }

            var dirsPaths = new List <string>();

            BlobCollection.Instance.GetStartingWith(Path + "/", flat)
            .Where(b => b.Exists())
            .Select(b => AzurePathHelper.GetBlobDirectory(b.Path))
            .Union(BlobDirectoryCollection.Instance.GetStartingWith(Path + "/", flat).Where(d => d.Exists()).Select(d => d.Path))
            .Distinct()
            .ToList()
            .ForEach(b => dirsPaths.AddRange(DirectoryHelper.PathToParent(b, Path)));

            dirsPaths = dirsPaths.Distinct().ToList();
            if (flat)
            {
                dirsPaths = dirsPaths.Where(d => AzurePathHelper.GetBlobDirectory(d) == Path).ToList();
            }

            return(dirsPaths.Select(p => BlobDirectoryCollection.Instance.GetOrCreate(p)));
        }
Exemplo n.º 2
0
        private byte[] TryGetFromFileSystemCache(string path, DateTime remoteLastModified)
        {
            byte[] data             = null;
            var    fileLastModified = DateTime.MinValue;

            var tempFilePath = AzurePathHelper.GetTempBlobPath(path);

            if (System.IO.File.Exists(tempFilePath))
            {
                fileLastModified = System.IO.File.GetLastWriteTimeUtc(tempFilePath);
                using (var stream = new System.IO.FileStream(tempFilePath, System.IO.FileMode.Open))
                {
                    data = new byte[stream.Length];
                    stream.Read(data, 0, data.Length);
                }
            }

            // no data in cache or outdated
            if (data == null || fileLastModified < remoteLastModified)
            {
                if (data != null)
                {
                    Discard(path);
                }

                return(null);
            }

            return(data);
        }
Exemplo n.º 3
0
        public override void Move(string sourceDirName, string destDirName)
        {
            var sourceDirPath = AzurePathHelper.GetBlobPath(sourceDirName);
            var destDirPath   = AzurePathHelper.GetBlobPath(destDirName);

            // if web farms are enabled, reinitialize source and destination folder to see if another web farm server has already moved the files
            if (SynchronizationHelper.Synchronizing())
            {
                BlobDirectoryCollection.Instance.GetOrCreate(sourceDirPath).Reinitialize();
                BlobDirectoryCollection.Instance.GetOrCreate(destDirPath).Reinitialize();
            }

            var sourceDirBlobs = Get(sourceDirName).GetBlobs(false);

            foreach (var blob in sourceDirBlobs)
            {
                blob.Move(destDirPath + "/" + blob.Path.Substring(sourceDirPath.Length + 1));
            }


            // we have to do this on file system too because of Dirs :(
            if (System.IO.Directory.Exists(sourceDirName))
            {
                System.IO.Directory.Move(sourceDirName, destDirName);
            }
        }
        private void End_Execute(object sender, EventArgs e)
        {
            // remove azure temp folder
            var tempFolder = AzurePathHelper.GetTempBlobPath(string.Empty);

            if (!string.IsNullOrEmpty(tempFolder) && System.IO.Directory.Exists(tempFolder))
            {
                System.IO.Directory.Delete(tempFolder, true);
            }
        }
Exemplo n.º 5
0
        public void SetExists(bool exists)
        {
            lock (_lock)
            {
                _exists      = exists;
                _lastRefresh = DateTime.UtcNow;

                if (_exists.Value)
                {
                    BlobDirectoryCollection.Instance.GetOrCreate(AzurePathHelper.GetBlobDirectory(Path)).SetExists();
                }
            }
        }
Exemplo n.º 6
0
        private void AddToFileSystem(string path, byte[] data, DateTime created)
        {
            var tempFilePath = AzurePathHelper.GetTempBlobPath(path);

            System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(tempFilePath));

            using (var stream = new System.IO.FileStream(tempFilePath, System.IO.FileMode.Create))
            {
                stream.Write(data, 0, data.Length);
            }

            // make sure the last write time matches cloud timestamp
            System.IO.File.SetLastWriteTimeUtc(tempFilePath, created);
        }
        public void ResetExistsForParents()
        {
            var blobDirectory = AzurePathHelper.GetBlobDirectory(Path);

            if (blobDirectory != string.Empty)
            {
                if (_exists.HasValue && _exists.Value)
                {
                    BlobDirectoryCollection.Instance.GetOrCreate(blobDirectory).SetExists();
                }
                else
                {
                    BlobDirectoryCollection.Instance.GetOrCreate(blobDirectory).ResetExists();
                }
            }
        }
Exemplo n.º 8
0
        public void Discard(string path)
        {
            switch (_cacheType)
            {
            case BlobCacheType.Memory:
                CacheHelper.TouchKey(BlobCacheHelper.GetCacheDependency(path));
                break;

            case BlobCacheType.FileSystem:
                var tempFilePath = AzurePathHelper.GetTempBlobPath(path);
                if (System.IO.File.Exists(tempFilePath))
                {
                    System.IO.File.Delete(tempFilePath);
                }
                break;
            }
        }
Exemplo n.º 9
0
        public void Delete()
        {
            if (Exists())
            {
                lock (_lock)
                {
                    if (!SynchronizationHelper.Synchronizing() || _cloudBlobService.Exists(Path))
                    {
                        _cloudBlobService.Delete(Path);
                    }

                    SetExists(false);
                    _blobCacheService.Discard(Path);

                    BlobDirectoryCollection.Instance.GetOrCreate(AzurePathHelper.GetBlobDirectory(Path)).ResetExists();
                }
            }
        }
Exemplo n.º 10
0
        private bool ExistsInternal()
        {
            if (_exists == null)
            {
                // if parent directory has been initialized, this blob can not exist
                var parentDirPath = AzurePathHelper.GetBlobDirectory(Path);
                if (BlobDirectoryCollection.Instance.GetOrCreate(parentDirPath).BlobsInitialized)
                {
                    SetExists(false);
                }
                else
                {
                    SetExists(_cloudBlobService.Exists(Path));
                }
            }

            return(_exists.Value);
        }
        public void DoesNotFailForInvalidDateFormat()
        {
            var appPath = AzurePathHelper.CurrentDirectory;
            var file    = new FileInfo(appPath + "\\FileInfoTests\\DoesNotFailForInvalidDateFormat\\test.txt");

            using (var writer = file.CreateText())
            {
                writer.Write("This is test");
            }

            file = new FileInfo(appPath + "\\FileInfoTests\\DoesNotFailForInvalidDateFormat\\test.txt");
            Assert.LessOrEqual(file.CreationTime, DateTime.UtcNow);

            BlobCollection.Instance.GetOrCreate(AzurePathHelper.GetBlobPath(appPath + "\\FileInfoTests\\DoesNotFailForInvalidDateFormat\\test.txt"))
            .SetMetadataAttributeAndSave(BlobMetadataEnum.DateCreated, "invalid date");

            Assert.AreEqual(file.CreationTime, DateTime.MinValue);
        }
        protected void InitializeBlobsInternal(bool forceRefresh = false)
        {
            if (!forceRefresh)
            {
                TryInitializeFromParent();
            }

            if (!forceRefresh && BlobsInitialized)
            {
                _exists = BlobCollection.Instance.GetStartingWith(Path + "/", false).Any(b => b.Exists());
                return;
            }

            // at this point we need to get data from remote
            var blobs = _cloudDirectoryService.GetBlobs(Path);

            if (LoggingHelper.LogsEnabled)
            {
                LoggingHelper.Log($"Blobs for path {Path}", $"{string.Join(",", blobs.Select(b => b.Path))}");
            }

            BlobCollection.Instance.AddRangeDistinct(blobs);
            _blobsInitialized = true;
            _exists           = blobs.Any();

            // update directories in collection
            var subDirectoriesPaths = new List <string>();

            blobs
            .Select(b => AzurePathHelper.GetBlobDirectory(b.Path))
            .Distinct()
            .Where(d => d != Path)
            .ToList()
            .ForEach(p => subDirectoriesPaths.AddRange(DirectoryHelper.PathToParent(p, Path)));

            var subDirectories = subDirectoriesPaths
                                 .Distinct()
                                 .Select(d => new BlobDirectory().InitializeWithFlag(d))
                                 .ToList();

            BlobDirectoryCollection.Instance.AddRangeDistinct(subDirectories);
        }
Exemplo n.º 13
0
        public string Execute(TaskInfo task)
        {
            if (_blobCacheService.CacheType != BlobCacheType.FileSystem)
            {
                return("Caching of Azure data is disabled or is set to memory. No need to run this task.");
            }

            var blobCacheService = Service <IBlobCacheService> .Entry();

            var minutes       = AccountInfo.Instance.BlobCacheMinutes;
            var dateThreshold = DateTime.UtcNow.AddMinutes(-minutes);

            var blobsToDelete = BlobCollection.Instance.GetOutdatedBlobPaths(dateThreshold);

            var blobsDeleted             = 0;
            var directoriesUninitialized = 0;

            foreach (var path in blobsToDelete)
            {
                // remove the blob
                blobCacheService.Discard(path);
                blobsDeleted++;
            }

            // clear empty folders in file system
            if (blobsDeleted > 0)
            {
                var folders = System.IO.Directory.GetDirectories(AzurePathHelper.GetTempBlobPath(string.Empty), "*", System.IO.SearchOption.AllDirectories).OrderByDescending(p => p.Length);
                foreach (var subFolder in folders)
                {
                    if (System.IO.Directory.Exists(subFolder) &&
                        !System.IO.Directory.GetFiles(subFolder).Any() &&
                        !System.IO.Directory.EnumerateDirectories(subFolder).Any())
                    {
                        System.IO.Directory.Delete(subFolder, false);
                    }
                }
            }

            return("OK, discarded metadata of " + blobsDeleted + " blobs, " + directoriesUninitialized + " dirs");
        }
        public void DoesNotGetsBlobCaseInvariantIfAlreadyExistsInBlob()
        {
            var temp = AzurePathHelper.ForceLowercase;

            AzurePathHelper.ForceLowercase = false;

            var appPath = AzurePathHelper.CurrentDirectory;
            var file    = new FileInfo(appPath + "\\GetsBlobCaseInvariantIfAlreadyExistsInBlob\\First.txt");

            using (var writer = file.CreateText())
            {
                writer.Write("This is test");
            }
            BlobCollection.Instance.GetOrCreate(AzurePathHelper.GetBlobPath(appPath + "\\GetsBlobCaseInvariantIfAlreadyExistsInBlob\\First.txt")).Uninitialize();

            AzurePathHelper.ForceLowercase = true;

            Assert.IsFalse(new FileInfo(appPath + "\\getsblobcaseinvariantifalreadyexistsinblob\\first.txt").Exists);

            AzurePathHelper.ForceLowercase = temp;
        }
        public void GetsBlobCaseInvariant()
        {
            var temp = AzurePathHelper.ForceLowercase;

            AzurePathHelper.ForceLowercase = true;

            var appPath = AzurePathHelper.CurrentDirectory;
            var file    = new FileInfo(appPath + "\\GetsBlobCaseInvariant\\First.txt");

            using (var writer = file.CreateText())
            {
                writer.Write("This is test");
            }

            // to make sure we will get hit in BLOB storage
            BlobCollection.Instance.GetOrCreate(AzurePathHelper.GetBlobPath(appPath + "\\GetsBlobCaseInvariant\\First.txt")).Uninitialize();

            Assert.IsTrue(new FileInfo(appPath + "\\geTSblobcaseiNVARiant\\first.txt").Exists);

            AzurePathHelper.ForceLowercase = temp;
        }
Exemplo n.º 16
0
        public void RemovesCachedFile()
        {
            _blobCacheService.SetCacheType(BlobCacheType.FileSystem);

            using (var ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes("string")))
            {
                Get("RemovesCachedFile.txt").Upload(ms);
            }

            var cachePath = AzurePathHelper.GetTempBlobPath(Get("RemovesCachedFile.txt").Path);

            // cache was created in file system
            Assert.IsTrue(System.IO.File.Exists(cachePath));

            Get("RemovesCachedFile.txt").Delete();

            // cache was removed
            Assert.IsFalse(System.IO.File.Exists(cachePath));

            Assert.AreEqual(3, _cloudBlobService.History.Count);
        }
Exemplo n.º 17
0
        public override string[] GetDirectories(string path, string searchPattern, SearchOption searchOption)
        {
            // weird fallback from original implementation that checks whether file exists on file system
            // should be absolutely removed!!!
            var fileSystemSubDirectories = System.IO.Directory.Exists(path) ? System.IO.Directory.GetDirectories(path, searchPattern, searchOption == SearchOption.AllDirectories ? System.IO.SearchOption.AllDirectories : System.IO.SearchOption.TopDirectoryOnly) : new string[0];

            if (AzurePathHelper.ForceLowercase)
            {
                fileSystemSubDirectories = fileSystemSubDirectories.Select(s => s.ToLowerInvariant()).ToArray();
            }
            // end of weird fallback!
            //

            var condition = new Func <string, bool>(p => true);

            if (!string.IsNullOrEmpty(searchPattern) || searchPattern == "*")
            {
                var regex = RegexHelper.GetRegex(searchPattern.Replace("*", ".*"), true);
                condition = new Func <string, bool>(p => regex.IsMatch(Path.GetFileName(p)));
            }

            var flat = searchOption == SearchOption.TopDirectoryOnly;

            var cloudSubDirectories = Get(path).GetSubdirectories(flat)
                                      .Where(d => d.Exists())
                                      .Select(d => AzurePathHelper.GetFileSystemPath(d.Path))
                                      .Where(condition)
                                      .ToArray();

            if (LoggingHelper.LogsEnabled)
            {
                LoggingHelper.Log($"GetDirectories {path}", $"Base path: {path}, cloud directories: {string.Join(",", cloudSubDirectories)}, file system directories: {string.Join(",", fileSystemSubDirectories)}");
            }

            var listOfAll = new List <string>(fileSystemSubDirectories);

            listOfAll.AddRange(cloudSubDirectories);

            return(listOfAll.Distinct().ToArray());
        }
        public void InitializesBlobAsLowercase()
        {
            var temp = AzurePathHelper.ForceLowercase;

            AzurePathHelper.ForceLowercase = false;

            var appPath = AzurePathHelper.CurrentDirectory;
            var file    = new FileInfo(appPath + "\\initializesblobaslowercase\\TEST.txt");

            using (var writer = file.CreateText())
            {
                writer.Write("This is test");
            }
            BlobCollection.Instance.GetOrCreate(AzurePathHelper.GetBlobPath(appPath + "\\initializesblobaslowercase\\TEST.txt")).Uninitialize();

            AzurePathHelper.ForceLowercase = true;
            var files = BlobDirectoryCollection.Instance.GetOrCreate(AzurePathHelper.GetBlobPath(appPath + "\\initializesblobaslowercase")).GetBlobs(false);

            Assert.AreEqual(1, files.Count());
            Assert.AreEqual("test.txt", System.IO.Path.GetFileName(files.Single().Path));

            AzurePathHelper.ForceLowercase = temp;
        }
Exemplo n.º 19
0
        public string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
        {
            var condition = new Func <string, bool>(p => true);

            if (!string.IsNullOrEmpty(searchPattern))
            {
                var regex = RegexHelper.GetRegex(searchPattern.Replace("*", ".*"), true);
                condition = new Func <string, bool>(p => regex.IsMatch(Path.GetFileName(p)));
            }

            var flat = searchOption == SearchOption.TopDirectoryOnly;

            var files = Get(path).GetBlobs(flat)
                        .Select(d => AzurePathHelper.GetFileSystemPath(d.Path))
                        .Where(condition)
                        .ToArray();

            if (LoggingHelper.LogsEnabled)
            {
                LoggingHelper.Log($"GetFiles {path}", $"Base path: {path}, condition: {condition}, files: {string.Join(",", files)}");
            }

            return(files);
        }
 public void GetParentFromPathEnsuresForwardSlashes()
 {
     Assert.AreEqual("dir22/dir23", AzurePathHelper.GetBlobDirectory("dir22/dir23/test.txt"));
 }
 public void GetsParentFromPath()
 {
     Assert.AreEqual("dir21", AzurePathHelper.GetBlobDirectory("dir21/test.txt"));
     Assert.AreEqual(string.Empty, AzurePathHelper.GetBlobDirectory("test.txt"));
 }
 public void GetsDirectoryForBlob()
 {
     Assert.AreEqual("dir10", AzurePathHelper.GetBlobDirectory("dir10/file.txt"));
     Assert.AreEqual("dir10/dir11", AzurePathHelper.GetBlobDirectory("dir10/dir11/file.txt"));
 }
        public void TranslatesPathsToBlob()
        {
            var path = AzurePathHelper.CurrentDirectory + "\\test.txt";

            Assert.AreEqual("test.txt", AzurePathHelper.GetBlobPath(path));
        }
Exemplo n.º 24
0
 /// <summary>
 /// Copies an existing file to a new file. Overwriting a file of the same name is allowed.
 /// </summary>
 /// <param name="sourceFileName">Path to source file.</param>
 /// <param name="destFileName">Path to destination file.</param>
 /// <param name="overwrite">If destination file should be overwritten.</param>
 public override void Copy(string sourceFileName, string destFileName, bool overwrite)
 {
     Get(sourceFileName).Copy(AzurePathHelper.GetBlobPath(destFileName), overwrite);
 }
Exemplo n.º 25
0
 /// <summary>
 /// Moves a specified file to a new location, providing the option to specify a new file name.
 /// </summary>
 /// <param name="sourceFileName">Source file name.</param>
 /// <param name="destFileName">Destination file name.</param>
 public override void Move(string sourceFileName, string destFileName)
 {
     Get(sourceFileName).Move(AzurePathHelper.GetBlobPath(destFileName));
 }
Exemplo n.º 26
0
 public string GetUrl()
 {
     return(BlobContainerCollection.Instance.GetOrCreate(AccountInfo.Instance.RootContainer).IsPublic() ?
            GetAttribute(a => a.AbsoluteUri) :
            AzurePathHelper.GetDownloadUri(Path));
 }
        protected override void ProcessRequestInternal(HttpContextBase context)
        {
            var hash = QueryHelper.GetString("hash", string.Empty);
            var path = QueryHelper.GetString("path", string.Empty);

            // Validate hash
            var settings = new HashSettings
            {
                Redirect = false
            };

            if (!ValidationHelper.ValidateHash("?path=" + URLHelper.EscapeSpecialCharacters(path), hash, settings))
            {
                RequestHelper.Respond403();
            }

            if (path.StartsWithCSafe("~"))
            {
                path = context.Server.MapPath(path);
            }

            var blobPath = AzurePathHelper.GetBlobPath(path);
            var blob     = BlobCollection.Instance.GetOrCreate(blobPath);

            if (!blob.Exists())
            {
                RequestHelper.Respond404();
            }

            CookieHelper.ClearResponseCookies();
            Response.Clear();

            SetRevalidation();

            var eTag         = blob.GetAttribute(a => a.Etag);
            var lastModified = ValidationHelper.GetDateTime(blob.GetAttribute(a => a.LastModified), DateTimeHelper.ZERO_TIME);

            SetResponseContentType(path);

            // Client caching - only on the live site
            if (AllowCache && AllowClientCache && ETagsMatch(eTag, lastModified))
            {
                // Set the file time stamps to allow client caching
                SetTimeStamps(lastModified);

                RespondNotModified(eTag);
                return;
            }

            SetDisposition(Path.GetFileName(path), Path.GetExtension(path));

            // Setup Etag property
            ETag = eTag;

            if (AllowCache)
            {
                // Set the file time stamps to allow client caching
                SetTimeStamps(lastModified);
                Response.Cache.SetETag(eTag);
            }
            else
            {
                SetCacheability();
            }

            WriteFile(path, CacheHelper.CacheImageAllowed(CurrentSiteName, Convert.ToInt32(blob.GetAttribute(a => a.Length))));

            CompleteRequest();
        }
Exemplo n.º 28
0
 private Blob Get(string path)
 {
     return(BlobCollection.Instance.GetOrCreate(AzurePathHelper.GetBlobPath(path)));
 }
        public bool IsCached(string path)
        {
            var tempFilePath = AzurePathHelper.GetTempBlobPath(path);

            return(System.IO.File.Exists(tempFilePath));
        }