/// <summary> /// Sync-SFItem Command is invoked /// </summary> protected override void ProcessRecord() { FileSupport = new FileSupport(TestMethod); ProviderInfo providerInfo; PSDriveInfo driveInfo; if (Upload && Download) { throw new PSArgumentException("Provide only one switch to Upload or Download the files"); } if (!string.IsNullOrEmpty(ShareFilePath)) { if (!Upload && !Download) { throw new PSArgumentException("Upload or Download switch must be specified"); } if (string.IsNullOrEmpty(LocalPath) || string.IsNullOrEmpty(LocalPath.Trim())) { // use current user directory location if Local path is not specified in arguments LocalPath = this.SessionState.Path.CurrentFileSystemLocation.Path; } if (Synchronize) { Recursive = true; } if (Move) { Recursive = true; } ShareFilePath = ShareFilePath.Trim(); LocalPath = LocalPath.Trim(); ActionType actionType = OverWrite ? ActionType.Force : (Synchronize ? ActionType.Sync : ActionType.None); int transactionId = new Random((int)DateTime.Now.Ticks).Next(); // if current user directory is local storage and ShareFile path provided withouth drive letter then append the drive letter with sharefile location if (this.SessionState.Path.CurrentLocation.Provider.ImplementingType != typeof(ShareFileProvider) && ShareFilePath.IndexOf(":") < 1) { Collection <PSDriveInfo> providerDrives = this.SessionState.Drive.GetAll();// ForProvider("ShareFile"); foreach (PSDriveInfo driveObj in providerDrives) { if (driveObj.Provider.ImplementingType == typeof(ShareFileProvider)) { if (ShareFilePath.StartsWith("/") || ShareFilePath.StartsWith(@"\")) { ShareFilePath = ShareFilePath.Substring(1); } string sfDrive = String.Format("{0}:/", driveObj.Name); ShareFilePath = Path.Combine(sfDrive, ShareFilePath); break; } } } var sourcePath = Upload ? LocalPath : ShareFilePath; // it will resolve paths if wildcards are used e.g. "D:\\*.txt" then get paths of all files with txt extension from D:\\ location var resolvedPaths = this.GetResolvedProviderPathFromPSPath(sourcePath, out providerInfo); if (Download) { this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(sourcePath, out providerInfo, out driveInfo); var client = ((ShareFileDriveInfo)driveInfo).Client; StartDownload(client, driveInfo, resolvedPaths, actionType); } else { var unresolvedPath = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(ShareFilePath, out providerInfo, out driveInfo); var client = ((ShareFileDriveInfo)driveInfo).Client; Item targetItem = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)driveInfo, unresolvedPath); if (targetItem == null && !unresolvedPath.StartsWith(String.Format(@"\{0}\", Utility.DefaultSharefileFolder))) { string updatedPath = String.Format(@"\{0}\{1}", Utility.DefaultSharefileFolder, unresolvedPath); targetItem = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)driveInfo, updatedPath, null, null); } //else if (targetItem == null) //{ // targetItem = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)driveInfo, ShareFilePath, null, null); //} if (targetItem == null) { throw new FileNotFoundException("Destination path not found on ShareFile server."); } // if user didn't specify the Sharefile HomeFolder in path then appending in path // e.g. if user tries sf:/Folder1 as sharefile target then resolve this path to sf:/My Files & Folders/Folder1 if ((targetItem as Folder).Info.IsAccountRoot == true) { string updatedPath = String.Format(@"\{0}\{1}", Utility.DefaultSharefileFolder, unresolvedPath); targetItem = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)driveInfo, updatedPath, null, null); } StartUpload(client, transactionId, targetItem, resolvedPaths, actionType); } WriteObject("Sync operation successfully completed."); } if (Help) { WriteObject("SFCLI version " + Resources.Version); } }
/// <summary> /// Start download process /// </summary> private void StartDownload(ShareFileClient client, PSDriveInfo driveInfo, ICollection <string> resolvedPaths, ActionType actionType) { int transactionId = new Random((int)DateTime.Now.Ticks).Next(); Logger.Instance.Info("Downloading files from ShareFile server."); ActionManager actionManager = new ActionManager(); bool firstIteration = true; foreach (string path in resolvedPaths) { var item = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)driveInfo, path, null, null); // if user didn't specify the Sharefile HomeFolder in path then append in path // e.g. if user tries sf:/Folder1 as sharefile source then resolve this path to sf:/My Files & Folders/Folder1 if (item == null && !path.StartsWith(String.Format(@"\{0}\", DefaultSharefileFolder))) { string updatedPath = String.Format(@"\{0}\{1}", DefaultSharefileFolder, path); item = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)driveInfo, updatedPath, null, null); } var target = new DirectoryInfo(LocalPath); // if create root folder flag is specified then create a container folder first if (firstIteration && CreateRoot) { Models.Folder parentFolder = client.Items.GetParent(item.url).Execute() as Folder; target = CreateLocalFolder(target, parentFolder); firstIteration = false; } if (item is Models.Folder) { // if user downloading the root drive then download its root folders if ((item as Folder).Info.IsAccountRoot == true) { var children = client.Items.GetChildren(item.url).Execute(); foreach (var child in children.Feed) { if (child is Models.Folder) { DownloadRecursive(client, transactionId, child, target, actionType); } } } else { DownloadRecursive(client, transactionId, item, target, actionType); } } else if (item is Models.File) { DownloadAction downloadAction = new DownloadAction(FileSupport, client, transactionId, (Models.File)item, target, actionType); actionManager.AddAction(downloadAction); } } actionManager.Execute(); // on move remove source files if (Move) { foreach (string path in resolvedPaths) { var item = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)driveInfo, path, null, null); var target = new DirectoryInfo(LocalPath); // if strict flag is specified then also clean the target files which are not in source if (Strict) { DeleteLocalStrictRecursive(client, item, target); } DeleteShareFileItemRecursive(client, item, CreateRoot && Recursive); } } }
private void StartCopying(String[] paramPath, String paramDestination, bool paramForce, String paramDetails) { foreach (string path in paramPath) { // Handle the source Paths. They may be provided as a wildcard, or literal paths ProviderInfo sourceProvider; PSDriveInfo sourceDrive; List <string> filePaths = new List <string>(); if (_shouldExpandWildcards) { // Turn *.txt into foo.txt,foo2.txt etc. // if path is just "foo.txt," it will return unchanged. var expandedPaths = this.GetResolvedProviderPathFromPSPath(path.Trim(), out sourceProvider); // Not sure how to get the sourceDrive from this expansion // I will get from the first element on this collection // Is it possible to enumerate from multiple drives?? Is that why the method won't return the drive info? if (expandedPaths.Count == 0) { continue; } var firstPath = expandedPaths.FirstOrDefault(); this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(path.Trim(), out sourceProvider, out sourceDrive); filePaths.AddRange(expandedPaths); } else { // no wildcards, so don't try to expand any * or ? symbols. filePaths.Add(this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(path.Trim(), out sourceProvider, out sourceDrive)); } bool isSourceLocal = sourceProvider.ImplementingType == typeof(Microsoft.PowerShell.Commands.FileSystemProvider); bool isSourceSF = sourceProvider.ImplementingType == typeof(ShareFileProvider); // Handle the target Path. if (paramDestination == null) { paramDestination = this.SessionState.Path.CurrentFileSystemLocation.ProviderPath; } ProviderInfo targetProvider = null; PSDriveInfo targetDrive = null; var targetProviderPath = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(paramDestination, out targetProvider, out targetDrive); bool isTargetLocal = targetProvider.ImplementingType == typeof(Microsoft.PowerShell.Commands.FileSystemProvider); bool isTargetSF = targetProvider.ImplementingType == typeof(ShareFileProvider); Models.Item targetItem = null; if (isTargetSF) { targetItem = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)targetDrive, targetProviderPath); //targetItem = Utility.ResolveShareFilePath(sourceDrive, targetProviderPath); } // Process each input path foreach (string filePath in filePaths) { if (isSourceSF && isTargetLocal) { // ShareFile to local: perform download var client = ((ShareFileDriveInfo)sourceDrive).Client; var item = Utility.ResolveShareFilePath(sourceDrive, filePath); //ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)sourceDrive, filePath, null, null); if (item == null) { throw new Exception(string.Format("Source path '{0}' not found on ShareFile server.", filePath)); } var target = new DirectoryInfo(paramDestination); if (!target.Exists) { throw new Exception(string.Format("Destination path '{0}' not found on local drive.", paramDestination)); } RecursiveDownload(client, new Random((int)DateTime.Now.Ticks).Next(), item, target); } else if (isSourceSF && isTargetSF) { // ShareFile to ShareFile: perform API copy var sourceClient = ((ShareFileDriveInfo)sourceDrive).Client; var targetClient = ((ShareFileDriveInfo)targetDrive).Client; // TODO: verify that source and target drives are on the same account var sourceItem = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)sourceDrive, filePath); sourceClient.Items.Copy(sourceItem.url, targetItem.Id, paramForce).Execute(); } else if (isSourceLocal && isTargetSF) { // Local to Sharefile: perform upload var client = ((ShareFileDriveInfo)targetDrive).Client; FileSystemInfo source = null; FileAttributes attr = System.IO.File.GetAttributes(filePath); if ((attr & FileAttributes.Directory) == FileAttributes.Directory) { source = new DirectoryInfo(filePath); } else { source = new FileInfo(filePath); } if (targetItem == null) { throw new Exception(string.Format("Destination path '{0}' not found on ShareFile server.", targetProviderPath)); } if (!source.Exists) { throw new Exception(string.Format("Local path '{0}' not found on local drive.", filePath)); } RecursiveUpload(client, new Random((int)DateTime.Now.Ticks).Next(), source, targetItem); } else if (isSourceLocal && isTargetLocal) { // Local copy... FileSystemInfo source = null; FileAttributes attr = System.IO.File.GetAttributes(filePath); if ((attr & FileAttributes.Directory) == FileAttributes.Directory) { source = new DirectoryInfo(filePath); } else { source = new FileInfo(filePath); } RecursiveCopy(source, new DirectoryInfo(paramDestination)); } } } }
/// <summary> /// Sync-SFItem Command is invoked /// </summary> protected override void ProcessRecord() { FileSupport = new FileSupport(TestMethod); ProviderInfo providerInfo; PSDriveInfo driveInfo; if (Upload && Download) { throw new PSArgumentException("Provide only one switch to Upload or Download the files"); } if (!string.IsNullOrEmpty(ShareFilePath)) { if (!Upload && !Download) { throw new PSArgumentException("Upload or Download switch must be specified"); } if (string.IsNullOrEmpty(LocalPath)) { // use current user directory location if Local path is not specified in arguments LocalPath = this.SessionState.Path.CurrentFileSystemLocation.Path; } if (Synchronize) { Recursive = true; } if (Strict) { Move = true; } if (Move) { Recursive = true; } ActionType actionType = OverWrite ? ActionType.Force : (Synchronize ? ActionType.Sync : ActionType.None); int transactionId = new Random((int)DateTime.Now.Ticks).Next(); var sourcePath = Upload ? LocalPath : ShareFilePath; // it will resolve paths if wildcards are used e.g. "D:\\*.txt" then get paths of all files with txt extension from D:\\ location var resolvedPaths = this.GetResolvedProviderPathFromPSPath(sourcePath, out providerInfo); if (Download) { this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(sourcePath, out providerInfo, out driveInfo); var client = ((ShareFileDriveInfo)driveInfo).Client; StartDownload(client, driveInfo, resolvedPaths, actionType); } else { var unresolvedPath = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(ShareFilePath, out providerInfo, out driveInfo); var client = ((ShareFileDriveInfo)driveInfo).Client; Item targetItem = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)driveInfo, unresolvedPath); // if user didn't specify the Sharefile HomeFolder in path then appending in path // e.g. if user tries sf:/Folder1 as sharefile target then resolve this path to sf:/My Files & Folders/Folder1 if ((targetItem as Folder).Info.IsAccountRoot == true) { string updatedPath = String.Format(@"\{0}\{1}", DefaultSharefileFolder, unresolvedPath); targetItem = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)driveInfo, updatedPath, null, null); } StartUpload(client, transactionId, targetItem, resolvedPaths, actionType); } WriteObject("Sync operation successfully completed."); } if (Help) { WriteObject("SFCLI version " + Resources.Version); } }
/// <summary> /// Start download process /// </summary> private void StartDownload(ShareFileClient client, PSDriveInfo driveInfo, ICollection <string> resolvedPaths, ActionType actionType) { int transactionId = new Random((int)DateTime.Now.Ticks).Next(); ActionManager actionManager = new ActionManager(this, string.Empty); bool firstIteration = true; foreach (string path in resolvedPaths) { var item = Utility.ResolveShareFilePath(driveInfo, path); if (item == null) { throw new FileNotFoundException(string.Format("Source path '{0}' not found on ShareFile server.", path)); } var target = new DirectoryInfo(LocalPath); if (!target.Exists) { throw new Exception(string.Format("Destination '{0}' path not found on local drive.", LocalPath)); } // if create root folder flag is specified then create a container folder first if (firstIteration && CreateRoot) { Models.Folder parentFolder = client.Items.GetParent(item.url).Execute() as Folder; target = CreateLocalFolder(target, parentFolder); firstIteration = false; } if (item is Models.Folder) { // if user downloading the root drive then download its root folders if ((item as Folder).Info.IsAccountRoot == true) { var children = client.Items.GetChildren(item.url).Execute(); foreach (var child in children.Feed) { if (child is Models.Folder) { DownloadRecursive(client, transactionId, child, target, actionType); } } } else { DownloadRecursive(client, transactionId, item, target, actionType); } } else if (item is Models.File) { DownloadAction downloadAction = new DownloadAction(FileSupport, client, transactionId, (Models.File)item, target, actionType); actionManager.AddAction(downloadAction); } } actionManager.Execute(); // if strict flag is specified then also clean the target files which are not in source if (Strict) { var target = new DirectoryInfo(LocalPath); var directories = target.GetDirectories(); foreach (string path in resolvedPaths) { var item = Utility.ResolveShareFilePath(driveInfo, path); if (item is Folder) { foreach (DirectoryInfo directory in directories) { if (directory.Name.Equals(item.Name)) { DeleteLocalStrictRecursive(client, item, directory); break; } } } } } // on move remove source files if (Move) { foreach (string path in resolvedPaths) { var item = ShareFileProvider.GetShareFileItem((ShareFileDriveInfo)driveInfo, path, null, null); var target = new DirectoryInfo(LocalPath); DeleteShareFileItemRecursive(client, item, CreateRoot && Recursive); } } }