コード例 #1
0
ファイル: Console.cs プロジェクト: lithium0003/TSviewACD
        static int Download(string[] targetArgs, string[] paramArgs, bool index_mode = false)
        {
            var masterjob = JobControler.CreateNewJob(JobControler.JobClass.ControlMaster);

            masterjob.DisplayName = "Download";
            var ct = masterjob.ct;

            JobControler.Run(masterjob, (j) =>
            {
                string remotepath          = null;
                string localpath           = null;
                string indexpath           = null;
                string ignorespath         = null;
                FileMetadata_Info[] target = null;

                if (index_mode)
                {
                    if (targetArgs.Length > 4)
                    {
                        ignorespath = targetArgs[4];
                    }
                    if (targetArgs.Length > 3)
                    {
                        localpath = targetArgs[3];
                    }
                    if (targetArgs.Length > 2)
                    {
                        remotepath = targetArgs[2];
                        remotepath = remotepath.Replace('\\', '/');
                    }
                    if (targetArgs.Length > 1)
                    {
                        indexpath = targetArgs[1];
                    }
                }
                else
                {
                    if (targetArgs.Length > 3)
                    {
                        ignorespath = targetArgs[3];
                    }
                    if (targetArgs.Length > 2)
                    {
                        localpath = targetArgs[2];
                    }
                    if (targetArgs.Length > 1)
                    {
                        remotepath = targetArgs[1];
                        remotepath = remotepath.Replace('\\', '/');
                    }
                }

                if (string.IsNullOrEmpty(remotepath))
                {
                    masterjob.Result = 0;
                    return;
                }

                bool autodecrypt = true;
                foreach (var p in paramArgs)
                {
                    switch (p)
                    {
                    case "nodecrypt":
                        Console.Error.WriteLine("(--nodecrypt: disable auto decrypt)");
                        autodecrypt = false;
                        break;
                    }
                }
                AmazonDriveControl.autodecrypt = autodecrypt;
                AmazonDriveControl.indexpath   = indexpath;

                string itembasepath;
                try
                {
                    var loginjob = Login();
                    var initjob  = AmazonDriveControl.InitAlltree(loginjob);
                    initjob.Wait(ct: ct);
                    target = FindItems(remotepath?.Split('/'));

                    var target2  = target.SelectMany(x => DriveData.GetAllChildrenfromId(x.id));
                    itembasepath = FormMatch.GetBasePath(target.Select(x => DriveData.GetFullPathfromId(x.id)).Distinct());
                    target       = target2.Where(x => x.kind == "FILE").ToArray();

                    if (target.Length < 1)
                    {
                        masterjob.Result = 2;
                        return;
                    }
                }
                catch (OperationCanceledException)
                {
                    masterjob.Result = -1;
                    return;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("error: " + ex.ToString());
                    masterjob.Result = 1;
                    return;
                }

                if (ignorespath != null)
                {
                    var targetdict = new ConcurrentDictionary <string, FileMetadata_Info>();
                    Parallel.ForEach(target, item =>
                    {
                        targetdict[DriveData.GetFullPathfromId(item.id)] = item;
                    });
                    Console.WriteLine("ignore list loading...");
                    using (var file = new FileStream(ignorespath, FileMode.Open))
                        using (var sr = new StreamReader(file))
                        {
                            while (!sr.EndOfStream)
                            {
                                var line = sr.ReadLine().Split('\t');
                                FileMetadata_Info o;
                                if (line.Length > 1)
                                {
                                    if (targetdict.TryGetValue(line[0], out o))
                                    {
                                        if (o.contentProperties?.md5 == line[1])
                                        {
                                            if (targetdict.TryRemove(line[0], out o))
                                            {
                                                Console.WriteLine(line[0]);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (targetdict.TryRemove(line[0], out o))
                                    {
                                        Console.WriteLine(line[0]);
                                    }
                                }
                            }
                        }
                    target = targetdict.Values.ToArray();
                    Console.WriteLine("remain target: " + target.Length);

                    if (target.Length < 1)
                    {
                        masterjob.Result = 2;
                        return;
                    }
                }

                bool SelectFullpath = false;
                if (String.IsNullOrEmpty(localpath))
                {
                    Thread t = new Thread(new ThreadStart(() =>
                    {
                        if (target.Length > 1)
                        {
                            using (var save = new FolderBrowserDialog())
                            {
                                save.Description = "Select Save Folder for Download Items";
                                if (save.ShowDialog() != DialogResult.OK)
                                {
                                    return;
                                }
                                localpath = save.SelectedPath;
                            }
                        }
                        else
                        {
                            using (var save = new SaveFileDialog())
                            {
                                var filename  = DriveData.AmazonDriveTree[target[0].id].DisplayName;
                                save.FileName = filename;
                                if (save.ShowDialog() != DialogResult.OK)
                                {
                                    return;
                                }
                                localpath      = save.FileName;
                                SelectFullpath = true;
                            }
                        }
                    }));
                    t.SetApartmentState(System.Threading.ApartmentState.STA);
                    t.Start();
                    t.Join();
                    if (localpath == null)
                    {
                        masterjob.Result = 0;
                        return;
                    }
                }

                try
                {
                    Console.Error.WriteLine("remote:" + remotepath);
                    Console.Error.WriteLine("local:" + localpath);
                    if (indexpath != null)
                    {
                        Console.Error.WriteLine("index:" + indexpath);
                    }

                    if (target.Length == 1)
                    {
                        var filename = DriveData.AmazonDriveTree[target[0].id].DisplayName;
                        if (!SelectFullpath)
                        {
                            localpath = Path.Combine(localpath, filename);
                        }
                    }
                    if (target.Length > 1 && Path.GetFileName(localpath) != "")
                    {
                        localpath += "\\";
                    }

                    ConsoleJobDisp.Run();

                    var jobs = AmazonDriveControl.downloadItems(target, localpath, masterjob);

                    int errorcount = 0;
                    Task.WaitAll(jobs.Select(x => x.WaitTask(ct: ct)).ToArray());
                    foreach (var j2 in jobs)
                    {
                        if (j2.IsError)
                        {
                            errorcount++;
                        }
                    }
                    masterjob.Result = (errorcount == 0) ? 0 : errorcount + 10;
                }
                catch (OperationCanceledException)
                {
                    masterjob.Result = -1;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("error: " + ex.ToString());
                    masterjob.Result = 1;
                }
            });
            try
            {
                masterjob.Wait(ct: ct);
            }
            catch (OperationCanceledException)
            {
            }
            Config.IsClosing = true;
            Console.Out.Flush();
            return((masterjob.Result as int?) ?? -1);
        }
コード例 #2
0
        private void button_Upload_Click(object sender, EventArgs e)
        {
            button_Upload.Enabled = false;
            try
            {
                var items = listBox_LocalOnly.SelectedItems;
                if (items.Count == 0)
                {
                    return;
                }

                // アップロード先を選択
                var tree = new FormDriveTree();
                tree.root = DriveData.AmazonDriveTree[DriveData.AmazonDriveRootID];
                if (tree.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                var targetID = tree.SelectedID;
                if (string.IsNullOrEmpty(targetID))
                {
                    return;
                }

                // 対象がフォルダでない場合は、その上に上がる
                while (targetID != DriveData.AmazonDriveRootID && DriveData.AmazonDriveTree[targetID].info.kind != "FOLDER")
                {
                    targetID = DriveData.AmazonDriveTree[targetID].info.parents[0];
                }

                // アップロードファイルのパス共通部分を検索
                var pathbase = FormMatch.GetBasePath(items.Cast <FormMatch.MatchItem>().Select(x => x.local.path));
                //bool createdir = items.Count > 1 && items.Cast<FormMatch.MatchItem>().GroupBy(x => Path.GetFileName(x.local.path)).Any(g => g.Count() > 1);
                bool createdir   = true;
                var  uploadfiles = items.Cast <FormMatch.MatchItem>().Select(x => x.local.path.Substring(pathbase.Length)).ToArray();

                var joblist = new List <JobControler.Job>();
                JobControler.Job prevjob = null;
                foreach (var upfile in uploadfiles)
                {
                    var job = JobControler.CreateNewJob(JobControler.JobClass.Normal, depends: prevjob);
                    prevjob         = job;
                    job.DisplayName = upfile;
                    job.ProgressStr = "wait for upload.";
                    joblist.Add(job);
                    JobControler.Run(job, (j) =>
                    {
                        var parentID = targetID;
                        var filename = Path.Combine(pathbase, upfile);
                        if (createdir)
                        {
                            // フォルダを確認してなければ作る
                            parentID = AmazonDriveControl.CreateDirectory(Path.GetDirectoryName(upfile), parentID);
                            if (parentID == null)
                            {
                                job.Error("Upload : (ERROR)createFolder");
                                return;
                            }
                        }
                        // アップロード
                        AmazonDriveControl.DoFileUpload(new string[] { filename }, parentID);
                    });
                }
                Program.MainForm.ReloadAfterJob(joblist.ToArray());
            }
            finally
            {
                button_Upload.Enabled = true;
            }
        }