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); }
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); }