private static void AddToDatabase(RmtFile file, SettingObj setting, DateTime startTime) { try { if (!setting.DataBase.Equals("mysql", StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidOperationException("Database is wrone or not in use."); } using (var database = new Database(setting.DataBase_Addr, setting.DataBase_User, setting.DataBase_Pw)) { database.AddDataItem(file.FullFilePath.Substring(file.FullFilePath.IndexOf('\\') + 1), new System.IO.FileInfo(file.FullFilePath).Name, MirakurunWarpper.MirakurunService.GetUNIXTimeStamp(startTime), file.IsFatherUpdate ? file.OldFatherName : null); } } catch (Exception ex) { ex.Message.ErrorLognConsole(); Upload(file); } }
/// <summary> /// Upload method *unstable* under test. /// </summary> /// <param name="file"><see cref="RmtFile"/> object.</param> /// <param name="counter">fail counter.</param> private static void Upload(RmtFile file, int counter = -1) { if (counter >= 3) { "Failed to startup or connact to upload instance!".ErrorLognConsole(); } var client = new RVMCore.PipeClient <RmtFile>(); for (int i = 0; i <= 3; i++) { // try up to 4 times. try { client.Send(file, "RVMCoreUploader"); return; // if success return. } catch (Exception ex) { System.Threading.Tasks.Task.Delay(1000); ex.Message.ErrorLognConsole(); } } // if it come to here. then there should be not a upload instance present. so strat one. int pid = 0; try { // this ProcessExtensions should help startup a instance to a user... ummmm..untested. ProcessExtensions.StartProcessAsCurrentUser( System.Reflection.Assembly.GetEntryAssembly().Location, out pid); "Uploader process has been lanchued, PID[{0}]".InfoLognConsole(pid); } catch (Exception ex) { "Failed to start upload process [{0}]".ErrorLognConsole(ex.Message); } System.Threading.Tasks.Task.Delay(2000); counter += 1; //Pass fail to counter. then run from first line. Upload(file, counter); }
public IEnumerable <RmtFile> LoadData(bool loadAll = false) { var mCon = new MySqlConnection($"Server={serverAddr};Uid={uid};Pwd={pwd};Port={port};Database={DATABASENAME};SslMode=Preferred;"); var cmd = new MySqlCommand($"SELECT `id`, `time`, `name`, `path`, `isuploaded`, `showonuploader`, `uploadid`, `upprogress`, `initialfoldername` " + $"FROM `{TABLE_RECORDED}` " + (loadAll ? "" : "WHERE `showonuploader` = '1' AND `isuploaded` = '0'") + $"ORDER BY `time` ASC LIMIT 1000", mCon); MySqlDataReader result = null; try { mCon.Open(); result = cmd.ExecuteReader(); } catch (MySqlException e) { "Database Error:{0}".ErrorLognConsole(e.Message); yield break; } catch (Exception e) { "Unknow error occured when reading database : {0}".ErrorLognConsole(e.Message); yield break; } string root = SettingObj.Read().StorageFolder; while (result.Read()) { var tmp = result["initialfoldername"]; var item = new RmtFile(System.IO.Path.Combine(root, (string)result["path"]), !(tmp == DBNull.Value), (tmp == DBNull.Value ? string.Empty:tmp.ToString()), true); item.ID = (string)result["id"]; yield return(item); } cmd.Dispose(); mCon.Close(); }
private static bool MoveFile(StreamFile para, SettingObj mySetting, EPGAccess epgAccess = null) { if (!System.IO.File.Exists(para.FilePath)) { return(false); // Ops! file is not there. } string fileName = System.IO.Path.GetFileName(para.FilePath); string[] FolderList = System.IO.Directory.GetDirectories(mySetting.StorageFolder); //Get local preset base folder's child folers. string Targetfolder = mySetting.StorageFolder; RmtFile mFile = new RmtFile(); // Object for Upload process if (!string.IsNullOrWhiteSpace(mySetting.GetFolderTag(para.Genre))) { try //try get full path of genre folder. { Targetfolder = FolderList.First(x => Strings.InStr(x, mySetting.GetFolderTag(para.Genre)) > 0); } catch (Exception e)// In case the preset genre folder is not there. { Targetfolder = System.IO.Path.Combine(mySetting.StorageFolder, mySetting.GetFolderTag(para.Genre)); Console.WriteLine(e.Message); } } if (!System.IO.Directory.Exists(Targetfolder)) //if genre folder is not there make one. { System.IO.Directory.CreateDirectory(Targetfolder); } if (para.Genre.HasFlag(ProgramGenre.Anime) || para.Genre.HasFlag(ProgramGenre.Drama) || para.Genre.HasFlag(ProgramGenre.Variety)) { // those programs has genre of anime drama or variety could be in series. if that is the case make a folder to hold them. string programName = MasterHelper.FindTitle(para.Title); //find title. if (!programName.Equals(para.Title)) // if the title find by program doesn't match it's full name means it's in a series. { FolderList = System.IO.Directory.GetDirectories(Targetfolder); try // from here on is basicly copy plast the folder exist thing. { Targetfolder = FolderList.First(x => { // find a folder matchs series name var str = x.Substring(x.LastIndexOf(@"\") + 1); str = str.Substring(str.IndexOf("]") + 1); return(Strings.InStr(x, programName) > 0 || Strings.InStr(x, Strings.StrConv(programName, VbStrConv.Wide)) > 0 || Strings.InStr(programName, str) > 0 || Strings.InStr(Strings.StrConv(programName, VbStrConv.Wide), str) > 0); }); } catch (Exception e) { //no match so far so make a one. Targetfolder = System.IO.Path.Combine(Targetfolder, MasterHelper.GetTimeSpan(para.StartTime, para.StartTime) + programName); Console.WriteLine("Try find folder : " + e.Message); } mFile.FullFilePath = System.IO.Path.Combine(Targetfolder, fileName); //this will make sure the date period at the head of folder name is correct. if (MasterHelper.RenameDirUpToDate(ref Targetfolder, para.EndTime)) {//this is for upload process . notify that the folder name has been changed. mFile.OldFatherName = System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(mFile.FullFilePath)); mFile.IsFatherUpdate = true; mFile.FullFilePath = System.IO.Path.Combine(Targetfolder, fileName); } Console.WriteLine("Target folder is : " + Targetfolder); if (!System.IO.Directory.Exists(Targetfolder)) { System.IO.Directory.CreateDirectory(Targetfolder); "Create folder: {0}".InfoLognConsole(Targetfolder); } } //Move file to where it belongs. FileMovier(para.FilePath, System.IO.Path.Combine(Targetfolder, fileName)); para.FilePath = System.IO.Path.Combine(Targetfolder, fileName); if (epgAccess == null) {// if the epgAccess is null means this process is called by tvrock //no need to get extra information from server. //XML file is standerd meta data format for tvrock for now. para.ToXml(System.IO.Path.Combine(Targetfolder, fileName)); } else {// this process is called by epgstation or else. //information is from server, it will be stored in a //*.meta file, it could also include a station logo and a thumbnail of video. var tmp = para.EPGStation.WtiteFile(System.IO.Path.Combine(Targetfolder, fileName)); if (tmp) { epgAccess.DeleteRecordByID(para.EPGStation.Meta.id); } } //comit upload AddToDatabase(mFile, mySetting, para.StartTime); OKBeep(mySetting); // beep } else { mFile.FullFilePath = System.IO.Path.Combine(Targetfolder, fileName); FileMovier(para.FilePath, System.IO.Path.Combine(Targetfolder, fileName)); para.FilePath = System.IO.Path.Combine(Targetfolder, fileName); if (epgAccess == null) { para.ToXml(System.IO.Path.Combine(Targetfolder, fileName)); } else { var tmp = para.EPGStation.WtiteFile(System.IO.Path.Combine(Targetfolder, fileName)); if (tmp) { epgAccess.DeleteRecordByID(para.EPGStation.Meta.id); } } //Do upload same here.. AddToDatabase(mFile, mySetting, para.StartTime); OKBeep(mySetting); } return(true); }