/// <summary>Deletes files and optionally directories in given path.</summary> /// <param name="path">Path to delete.</param> /// <param name="directories">Indicates, whether directories should be also deleted.</param> private void DeleteInternal(string path, bool directories) { path = PathHelper.GetValidPath(path); var keyVersions = this.Provider .GetObjectsList(path, directories ? ObjectTypeEnum.FilesAndDirectories : ObjectTypeEnum.Files, true, true, false) .ConvertAll(p => new KeyVersion() { Key = PathHelper.GetObjectKeyFromPath(p, true) }) .Batch(S3ObjectInfoProvider.MAX_OBJECTS_PER_REQUEST); foreach (IEnumerable <KeyVersion> source in keyVersions) { DeleteObjectsRequest request = new DeleteObjectsRequest() { BucketName = AccountInfo.Current.BucketName }; request.Objects = source.ToList(); try { AccountInfo.Current.S3Client.DeleteObjects(request); } catch (DeleteObjectsException) { throw new Exception($"Some of the directory '{path}' underlying objects weren't deleted correctly"); } } this.Provider.DeleteObject(S3ObjectFactory.GetInfo(CMS.IO.Path.EnsureEndBackslash(path))); }
/// <summary>Moves directory.</summary> /// <param name="sourceDirName">Source directory name.</param> /// <param name="destDirName">Destination directory name.</param> /// <param name="level">Current nested level.</param> private void Move(string sourceDirName, string destDirName, int level) { sourceDirName = PathHelper.GetValidPath(sourceDirName); destDirName = PathHelper.GetValidPath(destDirName); destDirName = destDirName.Trim('\\'); if (!this.Exists(sourceDirName)) { throw new Exception($"Source path {sourceDirName} does not exist."); } if (!ExistsInS3Storage(destDirName)) { this.CreateDirectory(destDirName); } foreach (string file in this.GetFiles(sourceDirName)) { CMS.IO.File.Copy(file, $"{destDirName}\\{CMS.IO.Path.GetFileName(file)}"); } foreach (string directory in this.GetDirectories(sourceDirName)) { this.Move(directory, $"{destDirName}\\{CMS.IO.Path.GetFileName(directory)}", level + 1); } if (level == 0) { this.Delete(sourceDirName, true); } }
/// <summary>Constructor.</summary> /// <param name="path">Path to directory</param> public DirectoryInfo(string path) { this.currentPath = PathHelper.GetValidPath(path); this.currentPath = this.currentPath.TrimEnd('\\'); if (Directory.ExistsInFileSystem(path)) { this.systemDirectory = new System.IO.DirectoryInfo(this.currentPath); } this.InitCMSValues(); }
/// <summary> /// Determines whether the given path refers to an existing directory on Amazon S3 storage. /// </summary> /// <param name="path">Path to test.</param> public static bool ExistsInS3Storage(string path) { path = PathHelper.GetValidPath(path); string objectKeyFromPath = PathHelper.GetObjectKeyFromPath(path); if (!string.IsNullOrEmpty(objectKeyFromPath)) { return(S3ObjectFactory.Provider.ObjectExists(S3ObjectFactory.GetInfo($"{objectKeyFromPath}/", true))); } return(true); }
/// <summary>Prepares files for import. Converts them to lower case.</summary> /// <param name="path">Path.</param> public override void PrepareFilesForImport(string path) { path = PathHelper.GetValidPath(path); foreach (string objects in this.Provider.GetObjectsList(path, ObjectTypeEnum.FilesAndDirectories, true, false, true)) { IS3ObjectInfo info1 = S3ObjectFactory.GetInfo(PathHelper.GetObjectKeyFromPath(objects, false), true); string lowerInvariant = info1.Key.ToLowerInvariant(); if (lowerInvariant != info1.Key) { IS3ObjectInfo info2 = S3ObjectFactory.GetInfo(info1.Key, true); IS3ObjectInfo info3 = S3ObjectFactory.GetInfo(lowerInvariant, true); this.Provider.CopyObjects(info2, info3); this.Provider.DeleteObject(info2); } } }
/// <summary> /// Returns an enumerable collection of file names that match a search pattern in a specified path. /// </summary> private IEnumerable <string> EnumerateFilesCore(string path, string searchPattern) { IEnumerable <string> first = null; if (ExistsInFileSystem(path)) { first = System.IO.Directory.EnumerateFiles(path, searchPattern).Select(f => f.ToLowerInvariant()); } path = PathHelper.GetValidPath(path); Func <string, bool> searchCondition = this.GetSearchCondition(searchPattern); IEnumerable <string> second = this.Provider .GetObjectsList(path, ObjectTypeEnum.Files, false, true, true) .Where(f => searchCondition(CMS.IO.Path.GetFileName(f))) .Select(f => f.ToLowerInvariant()); if (first != null) { return(first.Union(second, StringComparer.Ordinal)); } return(second); }
/// <summary> /// Creates all directories and subdirectories as specified by path. /// </summary> /// <param name="path">Path to create.</param> public override CMS.IO.DirectoryInfo CreateDirectory(string path) { path = PathHelper.GetValidPath(path); if (this.Exists(path)) { return(new DirectoryInfo(path)); } IS3ObjectInfo info = S3ObjectFactory.GetInfo(path); info.Key = $"{info.Key}/"; this.Provider.CreateEmptyObject(info); DirectoryInfo directoryInfo = new DirectoryInfo(path) { CreationTime = DateTime.Now, Exists = true, FullName = path }; directoryInfo.LastWriteTime = directoryInfo.CreationTime; directoryInfo.Name = System.IO.Path.GetFileName(path); return(directoryInfo); }