Exemplo n.º 1
0
 static public void Run()
 {
     if (ConsoleFunc.IsOutputRedirected)
     {
         return;
     }
     t = new System.Threading.Timer((o) =>
     {
         Console.Clear();
         var height  = Console.WindowHeight;
         var width   = Console.WindowWidth;
         var joblist = JobControler.JobList();
         var i       = 0;
         foreach (var j in joblist.Where(x => !x.IsHidden).OrderBy(x => x.index))
         {
             Console.SetCursorPosition(0, i++);
             Console.Write(j.DisplayName);
             if (i > height - 1)
             {
                 break;
             }
             Console.SetCursorPosition(4, i++);
             Console.Write(j.ProgressStr);
             if (i > height - 1)
             {
                 break;
             }
         }
     }, null, 0, 1000);
 }
Exemplo n.º 2
0
 async protected static void CtrlC_Handler(object sender, ConsoleCancelEventArgs args)
 {
     Console.Error.WriteLine("");
     Console.Error.WriteLine("Cancel...");
     JobControler.CancelAll();
     args.Cancel = true;
     await Task.Run(() =>
     {
         while (!JobControler.IsEmpty)
         {
             Thread.Sleep(100);
         }
     }).ConfigureAwait(false);
 }
Exemplo n.º 3
0
        private static JobControler.Job Login()
        {
            var job = JobControler.CreateNewJob();

            job.DisplayName = "login";
            var ct = job.ct;

            JobControler.Run(job, (j) =>
            {
                var initialized = false;
                Console.Error.WriteLine("Login Start.");
                Drive.Login(ct).ContinueWith((task) =>
                {
                    if (!task.Result)
                    {
                        initialized = false;
                        return;
                    }
                    Drive.GetEndpoint(ct).ContinueWith((task2) =>
                    {
                        if (task.Result)
                        {
                            initialized = true;
                            return;
                        }
                    }, ct).Wait(ct);
                }, ct).Wait(ct);
                if (initialized)
                {
                    Console.Error.WriteLine("Login done.");
                }
                else
                {
                    Console.Error.WriteLine("Login failed.");
                    throw new ApplicationException("Login failed.");
                }
            });
            return(job);
        }
Exemplo n.º 4
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);
            });
        }
Exemplo n.º 5
0
        static int Upload(string[] targetArgs, string[] paramArgs, bool watchflag = false)
        {
            var masterjob = JobControler.CreateNewJob(JobControler.JobClass.ControlMaster);

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

            JobControler.Run(masterjob, (j) =>
            {
                string remotepath    = null;
                string localpath     = null;
                string watchdir      = null;
                string localbasepath = null;
                string target_id     = null;

                bool createdir = false;
                bool hashflag  = false;
                foreach (var p in paramArgs)
                {
                    switch (p)
                    {
                    case "md5":
                        Console.Error.WriteLine("(--md5: hash check mode)");
                        hashflag = true;
                        break;

                    case "createpath":
                        Console.Error.WriteLine("(--createpath: make upload target folder mode)");
                        createdir = true;
                        break;

                    case "crypt1":
                        Console.Error.WriteLine("(--crypt1: crypt upload mode(CTR mode))");
                        Config.UseEncryption         = true;
                        Config.UseFilenameEncryption = false;
                        Config.CryptMethod           = CryptMethods.Method1_CTR;
                        break;

                    case "nocrypt":
                        Console.Error.WriteLine("(--nocrypt: nomal upload mode)");
                        Config.UseEncryption         = false;
                        Config.UseFilenameEncryption = false;
                        break;

                    case "crypt1name":
                        Console.Error.WriteLine("(--crypt1name: crypt filename(CTR mode))");
                        Config.UseFilenameEncryption = true;
                        Config.UseEncryption         = true;
                        Config.CryptMethod           = CryptMethods.Method1_CTR;
                        break;

                    case "crypt2":
                        Console.Error.WriteLine("(--crypt2: crypt upload mode(CBC mode CarrotDAV))");
                        Config.UseFilenameEncryption = true;
                        Config.UseEncryption         = true;
                        Config.CryptMethod           = CryptMethods.Method2_CBC_CarotDAV;
                        break;
                    }
                }

                AmazonDriveControl.checkMD5         = hashflag;
                AmazonDriveControl.overrideConflict = true;
                AmazonDriveControl.upskip_check     = true;

                if (watchflag)
                {
                    if (targetArgs.Length > 3)
                    {
                        remotepath = targetArgs[3];
                        remotepath = remotepath.Replace('\\', '/');
                    }
                    if (targetArgs.Length > 2)
                    {
                        localbasepath = targetArgs[2];
                        if (Path.GetFileName(localbasepath) == "")
                        {
                            localbasepath = localbasepath.Substring(0, localbasepath.Length - 1);
                        }
                        localbasepath = Path.GetFullPath(localbasepath);
                    }
                    if (targetArgs.Length > 1)
                    {
                        watchdir = targetArgs[1];
                    }

                    if (string.IsNullOrEmpty(remotepath) || string.IsNullOrEmpty(watchdir) || string.IsNullOrEmpty(localbasepath))
                    {
                        masterjob.Result = 0;
                        return;
                    }
                }
                else
                {
                    if (targetArgs.Length > 2)
                    {
                        remotepath = targetArgs[2];
                        remotepath = remotepath.Replace('\\', '/');
                    }
                    if (targetArgs.Length > 1)
                    {
                        localpath = targetArgs[1];
                    }

                    if (string.IsNullOrEmpty(remotepath) || string.IsNullOrEmpty(localpath))
                    {
                        masterjob.Result = 0;
                        return;
                    }
                }
                try
                {
                    var loginjob = Login();
                    var initjob  = AmazonDriveControl.InitAlltree(loginjob);
                    initjob.Wait(ct: ct);
                    target_id = FindItemsID(remotepath?.Split('/'));

                    if (string.IsNullOrEmpty(target_id) && !createdir)
                    {
                        masterjob.Result = 2;
                        return;
                    }
                }
                catch (OperationCanceledException)
                {
                    masterjob.Result = -1;
                    return;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("error: " + ex.ToString());
                    masterjob.Result = 1;
                    return;
                }

                try
                {
                    if (string.IsNullOrEmpty(target_id) && createdir)
                    {
                        Console.Error.WriteLine("create path:" + remotepath);
                        target_id = AmazonDriveControl.CreateDirectory(remotepath, DriveData.AmazonDriveRootID, ct: ct);
                    }
                    if (string.IsNullOrEmpty(target_id))
                    {
                        Console.Error.WriteLine("error: createFolder");
                        masterjob.Result = -1;
                        return;
                    }
                }
                catch (OperationCanceledException)
                {
                    masterjob.Result = -1;
                    return;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("error: " + ex.ToString());
                    masterjob.Result = 1;
                    return;
                }

                ConsoleJobDisp.Run();

                try
                {
                    if (watchdir == null)
                    {
                        Console.Error.WriteLine("remote:" + remotepath);
                        Console.Error.WriteLine("local:" + localpath);

                        var jobs = DoUpload(localpath, target_id, masterjob);
                        Task.WaitAll(jobs.Select(x => x.WaitTask(ct: ct)).ToArray());
                        masterjob.Result = jobs.Select(x => x.Result as int?).Max();
                    }
                    else
                    {
                        Console.Error.WriteLine("remote:" + remotepath);
                        Console.Error.WriteLine("watch:" + watchdir);

                        UploadParam.target_id     = target_id;
                        UploadParam.watchdir      = watchdir;
                        UploadParam.localbasepath = localbasepath;
                        UploadParam.master        = masterjob;

                        // Create a new FileSystemWatcher and set its properties.
                        FileSystemWatcher watcher = new FileSystemWatcher();
                        watcher.Path = watchdir;

                        // Add event handlers.
                        watcher.Created += new FileSystemEventHandler(OnChanged);

                        // Begin watching.
                        watcher.EnableRaisingEvents = true;

                        try
                        {
                            Task.Delay(-1, ct).Wait(ct);
                        }
                        catch
                        {
                            watcher.EnableRaisingEvents = false;
                            throw;
                        }
                        masterjob.Result = 0;
                        return;
                    }
                }
                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);
        }
Exemplo n.º 6
0
        // Define the event handlers.
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            var target = Path.GetFileName(e.FullPath);

            target = Path.Combine(UploadParam.watchdir, target);
            string uploadpath = "";

            while (true)
            {
                try
                {
                    using (var f = new FileStream(e.FullPath, FileMode.Open))
                        using (var sr = new StreamReader(f))
                        {
                            uploadpath = sr.ReadLine();
                            break;
                        }
                }
                catch
                {
                    Thread.Sleep(1000);
                }
            }
            lock (changes_list)
            {
                if (changes_list.Contains(uploadpath))
                {
                    Console.Error.WriteLine("already added." + uploadpath);
                    return;
                }
                changes_list.Add(uploadpath);
            }

            Task.Delay(TimeSpan.FromSeconds(10), UploadParam.master.ct).Wait(UploadParam.master.ct);

            var prevJob   = UploadParam.master;
            var target_id = UploadParam.target_id;

            if (uploadpath.StartsWith(UploadParam.localbasepath))
            {
                var targetpath = Path.GetDirectoryName(uploadpath);
                targetpath = targetpath.Substring(UploadParam.localbasepath.Length);
                if (!string.IsNullOrEmpty(targetpath))
                {
                    target_id = AmazonDriveControl.CreateDirectory(targetpath, target_id, ct: UploadParam.master.ct);
                    if (target_id == null)
                    {
                        Console.Error.WriteLine("CreateFolder failed.");
                        return;
                    }
                }
            }
            var upjob    = AmazonDriveControl.DoFileUpload(new string[] { uploadpath }, target_id, WeekDepend: true, parentJob: prevJob);
            var cleanjob = JobControler.CreateNewJob(JobControler.JobClass.Clean, depends: upjob);

            cleanjob.DisplayName = "clean file";
            cleanjob.DoAlways    = true;
            JobControler.Run(cleanjob, (j) =>
            {
                if (upjob.First().IsCanceled)
                {
                    return;
                }

                while (File.Exists(e.FullPath))
                {
                    try
                    {
                        File.Delete(e.FullPath);
                    }
                    catch
                    {
                        Task.Delay(1000, cleanjob.ct).Wait(cleanjob.ct);
                    }
                }
                if ((upjob.First().Result as int? ?? -1) < 0)
                {
                    while (true)
                    {
                        try
                        {
                            using (var f = new FileStream(uploadpath + ".err.log", FileMode.Create))
                                using (var sw = new StreamWriter(f))
                                {
                                    sw.WriteLine(upjob.First().DisplayName);
                                    sw.WriteLine(upjob.First().ProgressStr);
                                    break;
                                }
                        }
                        catch
                        {
                            Thread.Sleep(1000);
                        }
                    }
                }
                else
                {
                    while (File.Exists(uploadpath))
                    {
                        try
                        {
                            File.Delete(uploadpath);
                        }
                        catch
                        {
                            Task.Delay(1000, cleanjob.ct).Wait(cleanjob.ct);
                        }
                    }
                }
                lock (changes_list)
                {
                    changes_list.Remove(uploadpath);
                }
            });
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        static int ListItems(string[] targetArgs, string[] paramArgs)
        {
            var job = JobControler.CreateNewJob(JobControler.JobClass.ControlMaster);

            job.DisplayName = "ListItem";
            JobControler.Run(job, (j) =>
            {
                string remotepath          = null;
                FileMetadata_Info[] target = null;

                if (targetArgs.Length > 1)
                {
                    remotepath = targetArgs[1];
                    remotepath = remotepath.Replace('\\', '/');
                }

                bool recursive = false;
                bool showmd5   = false;
                bool nodecrypt = false;
                foreach (var p in paramArgs)
                {
                    switch (p)
                    {
                    case "recursive":
                        Console.Error.WriteLine("(--recursive: recursive mode)");
                        recursive = true;
                        break;

                    case "md5":
                        Console.Error.WriteLine("(--md5: show MD5 hash)");
                        showmd5 = true;
                        break;

                    case "nodecrypt":
                        Console.Error.WriteLine("(--nodecrypt: disable auto decrypt)");
                        nodecrypt = true;
                        break;
                    }
                }

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

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

                    Console.Error.WriteLine("Found : " + target.Length);
                    foreach (var item in target)
                    {
                        string detail = "";
                        if (showmd5)
                        {
                            detail = "\t" + item.contentProperties?.md5;
                        }

                        if (recursive)
                        {
                            Console.WriteLine(DriveData.GetFullPathfromId(item.id, nodecrypt) + detail);
                        }
                        else
                        {
                            Console.WriteLine(((nodecrypt) ? item.name : DriveData.AmazonDriveTree[item.id].DisplayName) + ((item.kind == "FOLDER") ? "/" : "") + detail);
                        }
                    }

                    job.Result = 0;
                }
                catch (OperationCanceledException)
                {
                    job.Result = -1;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("error: " + ex.ToString());
                    job.Result = 1;
                }
            });
            try
            {
                job.Wait(ct: job.ct);
            }
            catch (OperationCanceledException)
            {
            }
            Config.IsClosing = true;
            Console.Out.Flush();
            return((job.Result as int?) ?? -1);
        }
Exemplo n.º 9
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            tic++;
            if (tic > 10000)
            {
                tic = 0;
            }
            listView1.Invalidate();
            if (tic % 10 != 0)
            {
                return;
            }

            var joblist = JobControler.JobList();

            if (joblist == null)
            {
                return;
            }
            var now = DateTime.Now;

            internalJobList = joblist.Where(x => !x.IsHidden)
                              .OrderBy(x => (x.IsInfo)? 0: (x.IsDone) ? 1 : (x.IsRunning) ? 2 : 3)
                              .ThenBy(x => x.index)
                              .ThenBy(x => (x.StartTime == default(DateTime)) ? now : x.StartTime)
                              .ThenBy(x => x.QueueTime).ToArray();
            listView1.VirtualListSize = internalJobList.Length;
            if (listView1.VirtualListSize > 0)
            {
                var str = new StringBuilder();
                str.AppendFormat("All:{0} ", internalJobList.Length);
                int d = JobControler.JobTypeCount(JobControler.JobClass.Download);
                int u = JobControler.JobTypeCount(JobControler.JobClass.Upload);
                int p = JobControler.JobTypeCount(JobControler.JobClass.PlayDownload);
                int o = internalJobList.Length - (d + u + p);
                if (d > 0)
                {
                    str.AppendFormat("Download:{0} ", d);
                }
                if (u > 0)
                {
                    str.AppendFormat("Upload:{0} ", u);
                }
                if (p > 0)
                {
                    str.AppendFormat("Play:{0} ", p);
                }
                if (o > 0)
                {
                    str.AppendFormat("Other:{0} ", o);
                }
                label1.Text = str.ToString();
            }
            else
            {
                label1.Text = "";
            }
            if (internalJobList.Length == 0)
            {
                Stop();
            }
        }
Exemplo n.º 10
0
 private void button1_Click(object sender, EventArgs e)
 {
     JobControler.CancelAllforUser();
     TSviewACD.FormClosing.Instance.Show();
 }
Exemplo n.º 11
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;
            }
        }
Exemplo n.º 12
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);
            });
        }