Ejemplo n.º 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);
        }