Beispiel #1
0
 protected override bool InitClient(ConnectEntryData ci, ref SFTPSyncClient client)
 {
     try
     {
         client = SFTPSyncClient.Create(ci.ce);
     }
     catch (Exception eClient)
     {
         ClouderSyncPackage.Log(eClient.Message);
         return(false);
     }
     try
     {
         if (!client.Connect())
         {
             client.Log("Connection has failed" + '\n');
             return(false);
         }
     }
     catch (Exception eConnect)
     {
         ClouderSyncPackage.WriteToOutputWindow(eConnect.Message + '\n');
         string testResult = client.getLogMessages();
         ClouderSyncPackage.WriteToOutputWindow(testResult);
         client.clearLogMessages();
         return(false);
     }
     return(true);
 }
        private async void Launch()
        {
            //CompressTask task = null;

            ClouderSyncPackage.WriteToOutputWindow("-----\n", "ClouderSync", null, true);

            if (!ConnectEntryData.SettingsExist())
            {
                ShowMessageBox(@"It does not appear that SFTP connection has been configured. Please configure in the next step", "ClouderSync SSH");
                cmdConfigureSSH.ConfigureSSH(this);
                return;
            }
            if (CompressTask.GetIsRunning())
            {
                ShowMessageBox(@"It appears that a compression task is running. Please cancel, or wait, or restart Visual Studio", "ClouderSync Compress");
                return;
            }
            CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken       token       = tokenSource.Token;

            CompressTask task = new CompressTask(this);
            await Task.Run(() =>
            {
                task.Execute(tokenSource);
            });
        }
Beispiel #3
0
        protected override bool ProcessTask()
        {
            ci.ReadEntry();


            ZipArchive archive     = null;
            string     archiveFile = string.Empty;

            if (!InitArchive(ref archive, ref ci, ref archiveFile))
            {
                return(false);
            }

            Array arrInfo = arrItems;

            ProcessCompression(archive, arrInfo, ci);
            ClouderSyncPackage.WriteToOutputWindow(string.Format("Compression complete: file {0}\n", archiveFile));
            try
            {
                archive.Dispose();
            }
            catch { }
            archive = null;
            return(true);
        }
Beispiel #4
0
        protected override bool ProcessTask()
        {
            SFTPSyncClient client = null;

            ci.ReadEntry();

            Array arrInfo = arrItems;

            ProcessTransfer(arrInfo, ref client, ref ci);
            ClouderSyncPackage.WriteToOutputWindow("Transfer complete" + "\n");
            return(true);
        }
 public void Execute(CancellationTokenSource cancellationTokenSource = null)
 {
     _cancelTokenSrc = cancellationTokenSource;
     try
     {
         ExecuteInternal();
     }
     catch (OperationCanceledException eCxl)
     {
         SetIsRunning(false);
         ClouderSyncPackage.WriteToOutputWindow("Transfer cancelled, " + eCxl.Message + "\n");
     }
     finally
     {
         cancellationTokenSource.Dispose();
     }
 }
        protected void ExecuteInternal()
        {
            SetIsRunning(true);
            if (_cancelTokenSrc != null)
            {
                _cancelToken = _cancelTokenSrc.Token;
            }
            if (_cancelToken.IsCancellationRequested)
            {
                _cancelToken.ThrowIfCancellationRequested();
            }
            SFTPSyncClient client = null;

            ci.ReadEntry();

            Array arrInfo = arrItems;

            ProcessTransfer(arrInfo, ref client, ref ci);
            ClouderSyncPackage.WriteToOutputWindow("Transfer complete" + "\n");
            SetIsRunning(false);
        }
Beispiel #7
0
        /*
         * protected virtual void ExecuteInternal()
         * {
         *  SetIsRunning(true);
         *  if (_cancelTokenSrc != null)
         *  {
         *      _cancelToken = _cancelTokenSrc.Token;
         *  }
         *  if (_cancelToken.IsCancellationRequested)
         *  {
         *      _cancelToken.ThrowIfCancellationRequested();
         *  }
         *  SFTPSyncClient client = null;
         *
         *  ci.ReadEntry();
         *
         *  Array arrInfo = arrItems;
         *
         *  ProcessTask(arrInfo, ref client, ref ci);
         *  ClouderSyncPackage.WriteToOutputWindow("Transfer complete" + "\n");
         *  SetIsRunning(false);
         * }
         */
        protected virtual bool InitArchive(ref ZipArchive archive, ref ConnectEntryData ci, ref string _archiveFileName)
        {
            //string archiveName = Path.ChangeExtension(selectedDir, "zip");
            string archiveName  = selectedFullFilePath + ".zip";
            string errorMessage = string.Empty;

            if (!FileTools.DeleteFile(archiveName, ref errorMessage))
            {
                try
                {
                    if (File.Exists(archiveName))
                    {
                        File.Delete(archiveName);
                    }
                }
                catch (Exception eDelete)
                {
                    ClouderSyncPackage.WriteToOutputWindow(String.Format("Failed to delete {0}:{1}\n", archiveName, eDelete.Message));
                    return(false);
                }
            }
            try
            {
                archive = ZipFile.Open(archiveName, ZipArchiveMode.Create);
            }
            catch (Exception eCreate)
            {
                ClouderSyncPackage.WriteToOutputWindow(String.Format("Failed to create {0}:{1}\n", archiveName, eCreate.Message));
                return(false);
            }
            if (_archiveFileName != null)
            {
                _archiveFileName = archiveName;
            }
            //archive.CreateEntryFromFile()
            return(true);
        }
 private async void Cancel()
 {
     ClouderSyncPackage.WriteToOutputWindow("\nCancellation requested \n", "ClouderSync", null, false);
     CompressTask.Cancel();
 }
Beispiel #9
0
        protected bool ProcessTransfer(Array arrInfo, ref SFTPSyncClient client, ref ConnectEntryData ci)
        {
            if ((arrInfo == null) || (arrInfo.Length < 1))
            {
                ClouderSyncPackage.WriteToOutputWindow("Nothing selected, nothing to do \n");
                return(false);
            }
            if (!InitClient(ci, ref client))
            {
                return(false);
            }
            string remoteSrcPath = FileTools.NormalizeDirName(ci.ce.remotesrcpath);
            int    iItem         = 0;
            int    iItemCount    = arrInfo.Length;

            if ((_cancelToken != null) && _cancelToken.IsCancellationRequested)
            {
                _cancelToken.ThrowIfCancellationRequested();
            }

#if !TEST_MODE
            if (!client.EnterDirectory(remoteSrcPath))
            {
                ClouderSyncPackage.WriteToOutputWindow("Failed to enter remote directory " + remoteSrcPath + "\n");
            }
            else
#endif
            {
                ClouderSyncPackage.WriteToOutputWindow("Entering remote directory " + remoteSrcPath + "...\n");
            }

            foreach (ItemInfo item in arrInfo)
            {
                iItem++;
                if (!item.HasPath)
                {
                    continue;
                }
                if (ci.ce.ignoreexcludedfiles)
                {
                    if (!item.IncludedInProject)
                    {
                        ClouderSyncPackage.WriteToOutputWindow("Skipping item excluded from project " + item.FilePath + "...\n");
                        continue;
                    }
                }
                if (!FileTools.CanMapRoots(item.FilePath, projectDirectory))
                {
                    int iMinLen = projectDirectory.Length;
                    if (item.FilePath.Length < iMinLen)
                    {
                        iMinLen = item.FilePath.Length;
                    }
                    ClouderSyncPackage.WriteToOutputWindow("Can not map " + item.FilePath.Substring(0, iMinLen) + " to " + projectDirectory + "\n");
                    continue;
                }
                string pathTrail  = FileTools.MapRoots(item.FilePath, projectDirectory);
                string szFileName = Path.Combine(projectDirectory, pathTrail);
                if (!File.Exists(szFileName))
                {
                    ClouderSyncPackage.WriteToOutputWindow("Entering local directory " + szFileName + "\n");
                    continue;
                }
                string fileName          = Path.GetFileName(item.FilePath);
                string localDir          = Path.GetDirectoryName(pathTrail);
                string remoteRelativeDir = FileTools.NormalizeDirName(localDir);

                if (fileName == string.Empty)
                {
                    ClouderSyncPackage.WriteToOutputWindow("Entering local directory " + item.FilePath + "\n");
                    client.EnterDirectory(FileTools.CombinePaths(remoteSrcPath, remoteRelativeDir));
                    continue;
                }


                string message = string.Format(CultureInfo.CurrentCulture, "{0}/{1} {2}=>{3}", iItem, iItemCount, pathTrail, remoteRelativeDir + fileName);
                ClouderSyncPackage.WriteToOutputWindow(message + "\n");
#if !TEST_MODE
                FileStream fs = null;
                try
                {
                    fs = new FileStream(szFileName, FileMode.Open, FileAccess.Read);
                }
                catch (Exception eFile)
                {
                    ClouderSyncPackage.WriteToOutputWindow(string.Format(CultureInfo.CurrentCulture, "Failed to open local file {0}: {1}\n", szFileName, eFile.Message));
                }
                if (fs != null)
                {
                    client.BufferSize = 1024;
                    client.UploadAndCreateFile(fs, remoteSrcPath, remoteRelativeDir, fileName, _cancelToken);
                    fs.Close();
                    fs.Dispose();
                    fs = null;
                }
#endif
                if ((_cancelToken != null) && _cancelToken.IsCancellationRequested)
                {
                    _cancelToken.ThrowIfCancellationRequested();
                }
            }
            if (client != null)
            {
                client.Disconnect();
                client.Dispose();
            }

            return(true);
        }
Beispiel #10
0
        protected bool ProcessCompression(ZipArchive archive, Array arrInfo, ConnectEntryData ci)
        {
            if ((arrInfo == null) || (arrInfo.Length < 1))
            {
                ClouderSyncPackage.WriteToOutputWindow("Nothing selected, nothing to do \n");
                return(false);
            }

            int iItem      = 0;
            int iItemCount = arrInfo.Length;

            if ((_cancelToken != null) && _cancelToken.IsCancellationRequested)
            {
                _cancelToken.ThrowIfCancellationRequested();
            }

            foreach (ItemInfo item in arrInfo)
            {
                iItem++;
                if (!item.HasPath)
                {
                    continue;
                }
                if (ci.ce.ignoreexcludedfiles)
                {
                    if (!item.IncludedInProject)
                    {
                        ClouderSyncPackage.WriteToOutputWindow("Skipping item excluded from project " + item.FilePath + "...\n");
                        continue;
                    }
                }
                string pathTrail  = FileTools.MapRoots(item.FilePath, projectDirectory);
                string szFileName = Path.Combine(projectDirectory, pathTrail);
                if (!File.Exists(szFileName))
                {
                    ClouderSyncPackage.WriteToOutputWindow("Entering local directory " + szFileName + "\n");
                    continue;
                }
                string          fileName          = Path.GetFileName(item.FilePath);
                string          localDir          = Path.GetDirectoryName(pathTrail);
                string          remoteRelativeDir = FileTools.NormalizeDirName(localDir);
                string          archiveRoot       = remoteRelativeDir;
                ZipArchiveEntry archiveEntry      = null;

                remoteRelativeDir = remoteRelativeDir.TrimStart(FileTools.DirectorySeparators);
                if (NeedsNewArchiveEntry(archiveEntry, remoteRelativeDir))
                {
                    ClouderSyncPackage.WriteToOutputWindow("Entering local directory " + Path.GetDirectoryName(item.FilePath) + "\n");
                    archiveRoot = remoteRelativeDir;
                    if (string.IsNullOrEmpty(archiveRoot))
                    {
                        archiveRoot = "/";
                    }
                    if (!string.IsNullOrEmpty(archiveRoot))
                    {
                        try
                        {
                            archiveEntry = archive.CreateEntry(archiveRoot);
                        }
                        catch (Exception eFile)
                        {
                            ClouderSyncPackage.WriteToOutputWindow(string.Format(CultureInfo.CurrentCulture, "Failed to create archive entry {0}: {1}\n", remoteRelativeDir, eFile.Message));
                        }
                    }
                    if (fileName == string.Empty)
                    {
                        continue;
                    }
                }
                string message = string.Format(CultureInfo.CurrentCulture, "{0}/{1} {2}=>{3}", iItem, iItemCount, pathTrail, remoteRelativeDir + fileName);
                ClouderSyncPackage.WriteToOutputWindow(message + "\n");
#if !TEST_MODE
                try
                {
                    string remoteFile = Path.Combine(remoteRelativeDir, fileName);
                    if (archiveEntry != null)
                    {
//                        remoteFile = archiveEntry.FullName + "/" + fileName;
                    }
                    ZipArchiveEntry fileZipEntry = archive.CreateEntryFromFile(szFileName, remoteFile, CompressionLevel.Optimal);
                }
                catch (Exception eAddFile)
                {
                    ClouderSyncPackage.WriteToOutputWindow(string.Format(CultureInfo.CurrentCulture, "Skipping file {0}: {1}", szFileName, eAddFile.Message));
                }
#endif
                if ((_cancelToken != null) && _cancelToken.IsCancellationRequested)
                {
                    _cancelToken.ThrowIfCancellationRequested();
                }
            }
            return(true);
        }
Beispiel #11
0
 private void Cancel()
 {
     ClouderSyncPackage.WriteToOutputWindow("\nCancellation requested \n", "ClouderSync", null, false);
     DeploymentTask.Cancel();
 }