Example #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (SelectedRemoteFiles == null)
            {
                return;
            }

            List <MatchItem> RemoteOnly     = new List <MatchItem>();
            List <MatchItem> LocalOnly      = new List <MatchItem>();
            List <MatchItem> BothAndMatch   = new List <MatchItem>();
            List <MatchItem> BothAndUnmatch = new List <MatchItem>();
            Dictionary <string, LocalItemInfo[]>  LocalDup  = new Dictionary <string, LocalItemInfo[]>();
            Dictionary <string, RemoteItemInfo[]> RemoteDup = new Dictionary <string, RemoteItemInfo[]>();

            var  synchronizationContext = SynchronizationContext.Current;
            bool TreeFlag     = radioButton_Tree.Checked;
            bool FilenameFlag = radioButton_filename.Checked;
            bool MD5Flag      = radioButton_MD5.Checked;

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

            job.DisplayName = "Match";
            job.ProgressStr = "wait for run";
            runningJob      = job;
            bool done = false;

            JobControler.Run(job, (j) =>
            {
                job.ProgressStr = "running...";
                job.Progress    = -1;

                synchronizationContext.Post((o) =>
                {
                    button_start.Enabled = false;
                }, null);

                var remote         = SelectedRemoteFiles.Select(x => new RemoteItemInfo(x, DriveData.GetFullPathfromId(x.id), null)).ToArray();
                var remotebasepath = GetBasePath(remote.Select(x => x.path));

                if (TreeFlag)
                {
                    remote = remote.Select(x => new RemoteItemInfo(x.info, x.path, x.path.Substring(remotebasepath.Length))).ToArray();
                }
                if (FilenameFlag)
                {
                    remote = remote.Select(x => new RemoteItemInfo(x.info, x.path, DriveData.AmazonDriveTree[x.info.id].DisplayName)).ToArray();
                }
                if (MD5Flag)
                {
                    remote = remote.Select(x => new RemoteItemInfo(x.info, x.path, x.info.contentProperties?.md5)).ToArray();
                }

                var localpath     = listBox_local.Items.Cast <string>();
                var localbasepath = GetBasePath(localpath);
                var len           = localpath.Count();
                int i             = 0;
                foreach (var ritem in remote.GroupBy(x => x.name).Where(g => g.Count() > 1))
                {
                    RemoteDup[ritem.Key] = ritem.ToArray();
                }
                var local = ((radioButton_MD5.Checked) ?
                             (localpath.Select(x =>
                {
                    byte[] md5 = null;
                    ++i;
                    synchronizationContext.Post((o) =>
                    {
                        if (runningJob?.ct.IsCancellationRequested ?? true)
                        {
                            return;
                        }
                        label_info.Text = o as string;
                    }, string.Format("{0}/{1} Check file MD5...{2}", i, len, x));
                    using (var md5calc = new System.Security.Cryptography.MD5CryptoServiceProvider())
                        using (var hfile = File.Open(x, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            md5 = md5calc.ComputeHash(hfile);
                            var MD5 = BitConverter.ToString(md5).ToLower().Replace("-", "");
                            return(new LocalItemInfo(x, MD5, hfile.Length, MD5));
                        }
                })) :
                             (radioButton_Tree.Checked) ?
                             localpath.Select(x =>
                {
                    if (checkBox_MD5.Checked)
                    {
                        byte[] md5 = null;
                        ++i;
                        synchronizationContext.Post((o) =>
                        {
                            if (runningJob?.ct.IsCancellationRequested ?? true)
                            {
                                return;
                            }
                            label_info.Text = o as string;
                        }, string.Format("{0}/{1} Check file MD5...{2}", i, len, x));
                        using (var md5calc = new System.Security.Cryptography.MD5CryptoServiceProvider())
                            using (var hfile = File.Open(x, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                md5 = md5calc.ComputeHash(hfile);
                                var MD5 = BitConverter.ToString(md5).ToLower().Replace("-", "");
                                return(new LocalItemInfo(x, x.Substring(localbasepath.Length).Replace('\\', '/'), hfile.Length, MD5));
                            }
                    }
                    else
                    {
                        ++i;
                        synchronizationContext.Post((o) =>
                        {
                            if (runningJob?.ct.IsCancellationRequested ?? true)
                            {
                                return;
                            }
                            label_info.Text = o as string;
                        }, string.Format("{0}/{1} Check file ...{2}", i, len, x));
                        return(new LocalItemInfo(x, x.Substring(localbasepath.Length).Replace('\\', '/'), new FileInfo(x).Length, null));
                    }
                }) :
                             localpath.Select(x =>
                {
                    if (checkBox_MD5.Checked)
                    {
                        byte[] md5 = null;
                        ++i;
                        synchronizationContext.Post((o) =>
                        {
                            if (runningJob?.ct.IsCancellationRequested ?? true)
                            {
                                return;
                            }
                            label_info.Text = o as string;
                        }, string.Format("{0}/{1} Check file MD5...{2}", i, len, x));
                        using (var md5calc = new System.Security.Cryptography.MD5CryptoServiceProvider())
                            using (var hfile = File.Open(x, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                md5 = md5calc.ComputeHash(hfile);
                                var MD5 = BitConverter.ToString(md5).ToLower().Replace("-", "");
                                return(new LocalItemInfo(x, Path.GetFileName(x), hfile.Length, MD5));
                            }
                    }
                    else
                    {
                        ++i;
                        synchronizationContext.Post((o) =>
                        {
                            if (runningJob?.ct.IsCancellationRequested ?? true)
                            {
                                return;
                            }
                            label_info.Text = o as string;
                        }, string.Format("{0}/{1} Check file ...{2}", i, len, x));
                        return(new LocalItemInfo(x, Path.GetFileName(x), new FileInfo(x).Length, null));
                    }
                }))
                            .GroupBy(x => x.name).ToArray();

                i = 0;
                foreach (var litem in local)
                {
                    job.ct.ThrowIfCancellationRequested();
                    var matchitem = remote.Where(x => x.name == litem.FirstOrDefault()?.name).ToArray();

                    if (litem.Count() > 1)
                    {
                        LocalDup[litem.Key] = litem.ToArray();
                    }

                    if (matchitem.Length > 0)
                    {
                        List <RemoteItemInfo> RemoteMatched = new List <RemoteItemInfo>();
                        List <LocalItemInfo> LocalUnMatched = new List <LocalItemInfo>();
                        // match test
                        foreach (var item in litem)
                        {
                            ++i;
                            synchronizationContext.Post((o) =>
                            {
                                if (runningJob?.ct.IsCancellationRequested ?? true)
                                {
                                    return;
                                }
                                label_info.Text = o as string;
                            }, string.Format("{0}/{1} {2}", i, len, item.path));

                            List <RemoteItemInfo> Matched = new List <RemoteItemInfo>();
                            foreach (var ritem in matchitem)
                            {
                                if (item.size == ritem.info.contentProperties?.size)
                                {
                                    if (item.MD5 == null || item.MD5 == ritem.info.contentProperties?.md5)
                                    {
                                        Matched.Add(ritem);
                                    }
                                }
                            }

                            if (Matched.Count() == 0)
                            {
                                LocalUnMatched.Add(item);
                            }

                            BothAndMatch.AddRange(Matched.Select(x => new MatchItem(item, x)));
                            RemoteMatched.AddRange(Matched);
                        }

                        var RemoteUnMatched = matchitem.Except(RemoteMatched);
                        if (RemoteUnMatched.Count() < LocalUnMatched.Count())
                        {
                            BothAndUnmatch.AddRange(RemoteUnMatched.Concat(RemoteMatched).Zip(LocalUnMatched, (r, l) => new MatchItem(l, r)));
                        }
                        else if (RemoteUnMatched.Count() > LocalUnMatched.Count())
                        {
                            BothAndUnmatch.AddRange(LocalUnMatched.Concat(litem).Zip(RemoteUnMatched, (l, r) => new MatchItem(l, r)));
                        }
                        else
                        {
                            if (RemoteUnMatched.Count() > 0)
                            {
                                BothAndUnmatch.AddRange(LocalUnMatched.Zip(RemoteUnMatched, (l, r) => new MatchItem(l, r)));
                            }
                        }
                    }
                    else
                    {
                        //nomatch
                        foreach (var item in litem)
                        {
                            ++i;
                            synchronizationContext.Post((o) =>
                            {
                                if (runningJob?.ct.IsCancellationRequested ?? true)
                                {
                                    return;
                                }
                                label_info.Text = o as string;
                            }, string.Format("{0}/{1} {2}", i, len, item.path));
                            LocalOnly.Add(new MatchItem(item, null));
                        }
                    }
                }
                RemoteOnly.AddRange(remote.Select(x => x.info)
                                    .Except(BothAndMatch.Select(x => x.remote.info))
                                    .Except(BothAndUnmatch.Select(x => x.remote.info))
                                    .Select(x => remote.Where(y => y.info == x).FirstOrDefault())
                                    .Select(x => new MatchItem(null, x)));
                done            = true;
                job.Progress    = 1;
                job.ProgressStr = "done.";
            });
            var afterjob = JobControler.CreateNewJob(JobControler.JobClass.Clean, depends: job);

            afterjob.DisplayName = "clean up";
            afterjob.DoAlways    = true;
            JobControler.Run(afterjob, (j) =>
            {
                afterjob.ProgressStr = "done.";
                afterjob.Progress    = 1;
                runningJob           = null;
                synchronizationContext.Post((o) =>
                {
                    label_info.Text      = "";
                    button_start.Enabled = true;
                    if (done)
                    {
                        var result        = new FormMatchResult();
                        result.RemoteOnly = RemoteOnly;
                        result.LocalOnly  = LocalOnly;
                        result.Unmatch    = BothAndUnmatch;
                        result.Match      = BothAndMatch;
                        result.RemoteDup  = RemoteDup;
                        result.LocalDup   = LocalDup;
                        result.Show();
                    }
                }, null);
            });
        }
Example #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;
            }
        }
Example #3
0
 private static JobControler.Job[] DoUpload(string uploadpath, string target_id, JobControler.Job prevJob)
 {
     if (File.Exists(uploadpath))
     {
         return(AmazonDriveControl.DoFileUpload(new string[] { uploadpath }, target_id, WeekDepend: true, parentJob: prevJob));
     }
     else if (Directory.Exists(uploadpath))
     {
         return(AmazonDriveControl.DoDirectoryUpload(new string[] { uploadpath }, target_id, WeekDepend: true, parentJob: prevJob));
     }
     return(null);
 }
Example #4
0
        private void button_Start_Click(object sender, EventArgs e)
        {
            if (SelectedRemoteFiles == null)
            {
                return;
            }

            button_Start.Enabled = false;
            var synchronizationContext = SynchronizationContext.Current;

            var masterjob = JobControler.CreateNewJob(JobControler.JobClass.Normal);

            masterjob.DisplayName = "Download test";
            masterjob.ProgressStr = "wait for download";
            runningjob            = masterjob;
            var joblist = new ConcurrentBag <JobControler.Job>();

            Parallel.ForEach(SelectedRemoteFiles, (item) =>
            {
                var job         = JobControler.CreateNewJob(JobControler.JobClass.Download, depends: masterjob);
                job.WeekDepend  = true;
                job.DisplayName = DriveData.GetFullPathfromId(item.id);
                job.ProgressStr = "Wait for download";
                joblist.Add(job);
                var ct = CancellationTokenSource.CreateLinkedTokenSource(masterjob.ct, job.ct).Token;
                JobControler.Run(job, (j) =>
                {
                    synchronizationContext.Post(
                        (o) =>
                    {
                        if (ct.IsCancellationRequested)
                        {
                            return;
                        }
                        var listitem = listView1.Items.Find(item.id, false).FirstOrDefault();
                        listitem.SubItems[1].Text = "download...";
                    }, null);
                    (j as JobControler.Job).ProgressStr = "download...";

                    var retry = 5;
                    while (--retry > 0)
                    {
                        ct.ThrowIfCancellationRequested();
                        try
                        {
                            long length = item.contentProperties.size ?? 0;
                            using (var ret = new AmazonDriveBaseStream(DriveData.Drive, item, autodecrypt: false, parentJob: (j as JobControler.Job)))
                                using (var f = new PositionStream(ret, length))
                                {
                                    f.PosChangeEvent += (src, evnt) =>
                                    {
                                        synchronizationContext.Post(
                                            (o) =>
                                        {
                                            if (ct.IsCancellationRequested)
                                            {
                                                return;
                                            }
                                            var eo       = o as PositionChangeEventArgs;
                                            var listitem = listView1.Items.Find(item.id, false).FirstOrDefault();
                                            listitem.SubItems[1].Text = eo.Log;
                                        }, evnt);
                                        (j as JobControler.Job).ProgressStr = evnt.Log;
                                        (j as JobControler.Job).Progress    = (double)evnt.Position / evnt.Length;
                                    };
                                    f.CopyTo(new NullStream());
                                }
                            synchronizationContext.Post(
                                (o) =>
                            {
                                if (ct.IsCancellationRequested)
                                {
                                    return;
                                }
                                var listitem = listView1.Items.Find(item.id, false).FirstOrDefault();
                                listitem.SubItems[1].Text = "Done";
                            }, null);
                            (j as JobControler.Job).ProgressStr = "Done.";
                            (j as JobControler.Job).Progress    = 1;
                            break;
                        }
                        catch (OperationCanceledException)
                        {
                            throw;
                        }
                        catch (Exception ex)
                        {
                            Config.Log.LogOut("Download : Error " + ex.Message);
                            continue;
                        }
                    }
                    if (retry == 0)
                    {
                        // failed
                        synchronizationContext.Post(
                            (o) =>
                        {
                            if (ct.IsCancellationRequested)
                            {
                                return;
                            }
                            var listitem = listView1.Items.Find(item.id, false).FirstOrDefault();
                            listitem.SubItems[1].Text = "Failed";
                        }, null);
                        (j as JobControler.Job).ProgressStr = "Failed";
                        (j as JobControler.Job).Progress    = double.NaN;
                        (j as JobControler.Job).IsError     = true;
                    }
                });
            });
            JobControler.Run(masterjob, (j) =>
            {
                masterjob.ProgressStr = "running...";
                masterjob.Progress    = -1;
                var ct = (j as JobControler.Job).ct;
                Task.WaitAll(joblist.Select(x => x.WaitTask(ct: ct)).ToArray());
                masterjob.Progress    = 1;
                masterjob.ProgressStr = "done.";
            });
            var afterjob = JobControler.CreateNewJob(JobControler.JobClass.Clean, depends: masterjob);

            afterjob.DisplayName = "clean up";
            afterjob.DoAlways    = true;
            JobControler.Run(afterjob, (j) =>
            {
                afterjob.ProgressStr = "done.";
                afterjob.Progress    = 1;
                runningjob           = null;
                synchronizationContext.Post((o) =>
                {
                    button_Start.Enabled = true;
                }, null);
            });
        }