Exemple #1
0
        private static int RunUpload(string[] targetArgs, string[] paramArgs)
        {
            string remotePath;
            string localPath;

            if (targetArgs.Length < 3)
            {
                Console.Error.WriteLine("upload needs more 2 arguments.");
                Console.Error.WriteLine("upload (localpath) (remotetarget)");
                return(0);
            }

            remotePath = targetArgs[2];
            remotePath = remotePath.Replace('\\', '/');
            localPath  = targetArgs[1];
            if (!localPath.Contains(':') && !localPath.StartsWith(@"\\"))
            {
                localPath = Path.GetFullPath(localPath);
            }
            if (!localPath.StartsWith(@"\\"))
            {
                localPath = ItemControl.GetLongFilename(localPath);
            }

            Console.Error.WriteLine("upload");
            Console.Error.WriteLine("remote: " + remotePath);
            Console.Error.WriteLine("local: " + ItemControl.GetOrgFilename(localPath));

            bool createFolder = false;

            foreach (var p in paramArgs)
            {
                switch (p)
                {
                case "createfolder":
                    Console.Error.WriteLine("(--createfolder: create folders)");
                    createFolder = true;
                    break;
                }
            }

            var job = JobControler.CreateNewJob(JobClass.ControlMaster);

            job.DisplayName = "Upload";
            JobControler.Run(job, (j) =>
            {
                try
                {
                    var j2 = InitServer(j);
                    j2.Wait(ct: j.Ct);

                    var target         = FindItems(remotePath, ct: j.Ct);
                    IRemoteItem remote = null;

                    if (target.Count() != 1 && !createFolder)
                    {
                        Console.Error.WriteLine("upload needs 1 remote target item.");
                        j.ResultAsObject = 2;
                        return;
                    }
                    if (target.Count() == 0 && createFolder)
                    {
                        Console.Error.WriteLine("Create new folders.");
                        remote = CreateFolders(remotePath, j);
                        if (remote == null)
                        {
                            Console.Error.WriteLine("make folder failed.");
                            j.ResultAsObject = 3;
                            return;
                        }
                    }
                    else
                    {
                        remote = target.First();
                    }

                    ConsoleJobDisp.Run();

                    if (File.Exists(localPath))
                    {
                        ItemControl.UploadFiles(remote, new[] { localPath }, true, j);
                    }
                    else if (Directory.Exists(localPath))
                    {
                        if (!localPath.EndsWith(":\\") && localPath.EndsWith("\\"))
                        {
                            localPath = localPath.TrimEnd('\\');
                        }
                        ItemControl.UploadFolder(remote, localPath, true, j);
                    }
                    else
                    {
                        Console.Error.WriteLine("upload localitem not found.");
                        j.ResultAsObject = 2;
                        return;
                    }

                    while (JobControler.JobTypeCount(JobClass.Upload) > 0)
                    {
                        JobControler.JobList().Where(x => x.JobType == JobClass.Upload).FirstOrDefault()?.Wait(ct: j.Ct);
                    }

                    var SaveConfigJob         = JobControler.CreateNewJob(TSviewCloudPlugin.JobClass.Save);
                    SaveConfigJob.DisplayName = "Save server list";
                    JobControler.Run(SaveConfigJob, (j3) =>
                    {
                        j3.Progress    = -1;
                        j3.ProgressStr = "Save...";

                        TSviewCloudConfig.Config.Save();
                        RemoteServerFactory.Save();

                        j3.Progress    = 1;
                        j3.ProgressStr = "Done.";
                    });
                    SaveConfigJob.Wait();

                    job.ResultAsObject = 0;
                }
                catch (OperationCanceledException)
                {
                    job.ResultAsObject = -1;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("error: " + ex.ToString());
                    job.ResultAsObject = 1;
                }
            });
            try
            {
                job.Wait(ct: job.Ct);
            }
            catch (OperationCanceledException)
            {
            }
            TSviewCloudConfig.Config.ApplicationExit = true;
            Console.Out.Flush();
            return((job.ResultAsObject as int?) ?? -1);
        }
Exemple #2
0
        private static int RunDownload(string [] targetArgs, string[] paramArgs)
        {
            string remotePath;
            string localPath;

            if (targetArgs.Length < 3)
            {
                Console.Error.WriteLine("download needs more 2 arguments.");
                Console.Error.WriteLine("download (remotepath) (localpath)");
                return(0);
            }

            remotePath = targetArgs[1];
            remotePath = remotePath.Replace('\\', '/');
            localPath  = targetArgs[2];
            if (!localPath.Contains(':') && !localPath.StartsWith(@"\\"))
            {
                localPath = Path.GetFullPath(localPath);
            }
            if (!localPath.StartsWith(@"\\"))
            {
                localPath = ItemControl.GetLongFilename(localPath);
            }

            Console.Error.WriteLine("download");
            Console.Error.WriteLine("remote: " + remotePath);
            Console.Error.WriteLine("local: " + ItemControl.GetOrgFilename(localPath));

            ReloadType reload = ReloadType.Reload;

            foreach (var p in paramArgs)
            {
                switch (p)
                {
                case "cache":
                    Console.Error.WriteLine("(--cache: no reload)");
                    reload = ReloadType.Cache;
                    break;
                }
            }

            var job = JobControler.CreateNewJob(JobClass.ControlMaster);

            job.DisplayName = "Download";
            JobControler.Run(job, (j) =>
            {
                try
                {
                    var j2 = InitServer(j);
                    j2.Wait(ct: j.Ct);

                    var target = FindItems(remotePath, ct: j.Ct, reload: reload);

                    if (target.Count() < 1)
                    {
                        j.ResultAsObject = 2;
                        return;
                    }

                    string remotePathBase = null;
                    if (remotePath.IndexOfAny(new[] { '*', '?' }) < 0)
                    {
                        remotePathBase = ItemControl.GetCommonPath(target);
                    }
                    else
                    {
                        remotePathBase = GetBasePath(remotePath);
                    }

                    target = RemoveDup(target);

                    ConsoleJobDisp.Run();

                    var j3 = target
                             .Where(x => x.ItemType == RemoteItemType.File)
                             .Select(x => RemoteServerFactory.PathToItem(x.FullPath, ReloadType.Reload).Result)
                             .Select(x => ItemControl.DownloadFile(Path.Combine(localPath, ItemControl.GetLocalFullPath(x.FullPath, remotePathBase)), x, j, true));
                    var j4 = target
                             .Where(x => x.ItemType == RemoteItemType.Folder)
                             .Select(x => RemoteServerFactory.PathToItem(x.FullPath, ReloadType.Reload).Result)
                             .Select(x => ItemControl.DownloadFolder(Path.GetDirectoryName(Path.Combine(localPath, ItemControl.GetLocalFullPath(x.FullPath, remotePathBase))), new[] { x }, j, true));

                    foreach (var jx in j3.Concat(j4).ToArray())
                    {
                        j.Ct.ThrowIfCancellationRequested();
                        jx.Wait(ct: j.Ct);
                    }

                    job.ResultAsObject = 0;
                }
                catch (OperationCanceledException)
                {
                    job.ResultAsObject = -1;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("error: " + ex.ToString());
                    job.ResultAsObject = 1;
                }
            });
            try
            {
                job.Wait(ct: job.Ct);
            }
            catch (OperationCanceledException)
            {
            }
            TSviewCloudConfig.Config.ApplicationExit = true;
            Console.Out.Flush();
            return((job.ResultAsObject as int?) ?? -1);
        }