Ejemplo n.º 1
0
        public static StreamFile ReadXMLCrypto(string filePath, string SecretKey)
        {
            byte[]          file;
            byte[]          PublicKey = System.Text.Encoding.Unicode.GetBytes(SecretKey);
            RijndaelManaged RMCrypto  = new RijndaelManaged();

            RMCrypto.Key = PublicKey;
            RMCrypto.IV  = PublicKey;
            using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open))
            {
                using (CryptoStream cs = new CryptoStream(fs, RMCrypto.CreateEncryptor(), CryptoStreamMode.Read))
                {
                    file = new byte[cs.Length];
                    cs.Read(file, 0, (int)cs.Length);
                }
            }
            if (file == null)
            {
                throw new Exception("Unable to serialize Object!");
            }
            System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(StreamFile));
            StreamFile result = new StreamFile();

            using (System.IO.MemoryStream ssr = new System.IO.MemoryStream(file))
            {
                result = (StreamFile)sr.Deserialize(ssr);
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read <see cref="StreamFile"/> object from a XML file.
        /// </summary>
        /// <param name="filePath">the full path of the xml file including file name.</param>
        /// <exception cref="InvalidOperationException">if it f***s up, don't be mad.</exception>
        public static StreamFile FromXml(string filePath)
        {
            System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(StreamFile));
            StreamFile result = new StreamFile();

            using (System.IO.StreamReader ssr = new System.IO.StreamReader(filePath))
            {
                result = (StreamFile)sr.Deserialize(ssr);
            }
            return(result);
        }
Ejemplo n.º 3
0
 private static void PrintInfomation(StreamFile paras)//private static void PrintInfomation(StreamFile paras)
 {
     if (paras == null)
     {
         return;
     }
     Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-= File Info =-=-=-=-=-=-=-=-=-=-=-");
     Console.WriteLine("File:         {0}", paras.FilePath);
     Console.WriteLine("Genre:        {0}", paras.Genre);
     Console.WriteLine("Title:        {0}", paras.Title);
     Console.WriteLine("recTitle:     {0}", paras.recTitle);
     Console.WriteLine("Subtitle:     {0}", paras.recSubTitle);
     Console.WriteLine("Channel:      {0}", paras.ChannelName);
     Console.WriteLine("Content:      {0}", paras.Content);
     Console.WriteLine("Infomation:   {0}", paras.Infomation);
     Console.WriteLine("StartTime:    {0}", paras.StartTime);
     Console.WriteLine("EndTime:      {0}", paras.EndTime);
     Console.WriteLine("SerachKeyword:{0}", paras.recKeyWord);
     Console.WriteLine("KeywordInfo:  {0}", paras.recKeywordInfo);
     Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
 }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        private static StreamFile GetPara(string[] paras)//private static StreamFile GetPara(string[] paras)
        {
            // Start process the parameters, group them to their own entries.
            List <string> clPara = new List <string>();

            if (paras.Count() <= 1)
            {
                return(null /* TODO Change to default(_) if this is not a reference type */);
            }
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (var p in paras)
            {
                if (p.StartsWith("-"))
                {
                    var tmp = sb.ToString();
                    if (tmp.Length > 1 && tmp.StartsWith("-"))
                    {
                        clPara.Add(tmp);
                    }
                    sb.Clear(); // Clear the stringbuilder every time it hits the char "-"
                    sb.Append(p);
                }
                else
                {
                    sb.AppendFormat(" {0}", p);
                }
            }
            clPara.Add(sb.ToString()); // Lack of the last run in the loop, so add the last parameter manuly.

            Console.WriteLine();
            StreamFile mPara = new StreamFile();

            foreach (var p in clPara)
            {
                var i = p.Substring(1, 1);
                switch (i.ToLower())
                {
                case "i":
                {
                    string m = p.Substring(p.IndexOf(" ") + 1);
                    if (System.IO.File.Exists(m))
                    {
                        mPara.FilePath = m;
                    }
                    else
                    {
                        Console.WriteLine("Input file could not found."); return(null /* TODO Change to default(_) if this is not a reference type */);
                    }
                    break;
                }

                case "g":
                {
                    string m = p.Substring(p.IndexOf(" ") + 1);
                    mPara.Genre = GetGenre(m);
                    break;
                }

                case "d":
                {
                    if (p.Length > 3)
                    {
                        mPara.Title = p.Substring(p.IndexOf(" ") + 1);
                    }
                    break;
                }

                case "c":
                {
                    if (p.Length > 3)
                    {
                        mPara.ChannelName = p.Substring(p.IndexOf(" ") + 1);
                    }
                    break;
                }

                case "e":
                {
                    if (p.Length > 3)
                    {
                        mPara.Content = p.Substring(p.IndexOf(" ") + 1);
                    }
                    break;
                }

                case "f":
                {
                    if (p.Length > 3)
                    {
                        mPara.Infomation = p.Substring(p.IndexOf(" ") + 1);
                    }
                    break;
                }

                case "k":
                {
                    if (p.Length > 3)
                    {
                        string m = p.Substring(p.IndexOf(" ") + 1);
                        mPara.StartTime = DateTime.Parse(m);
                    }
                    break;
                }

                case "l":
                {
                    if (p.Length > 3)
                    {
                        string m = p.Substring(p.IndexOf(" ") + 1);
                        mPara.EndTime = DateTime.Parse(m);
                    }
                    break;
                }

                case "9":
                {
                    if (p.Length > 3)
                    {
                        mPara.recSubTitle = p.Substring(p.IndexOf(" ") + 1);
                    }
                    break;
                }

                case "8":
                {
                    if (p.Length > 3)
                    {
                        mPara.recTitle = p.Substring(p.IndexOf(" ") + 1);
                    }
                    break;
                }

                case "a":
                {
                    if (p.Length > 3)
                    {
                        mPara.recKeyWord = p.Substring(p.IndexOf(" ") + 1);
                    }
                    break;
                }

                case "b":
                {
                    if (p.Length > 3)
                    {
                        mPara.recKeywordInfo = p.Substring(p.IndexOf(" ") + 1);
                    }
                    break;
                }

                default:
                {
                    Console.WriteLine("Unknow paramater");
                    break;
                }
                }
            }
            return(mPara);
        }
Ejemplo n.º 6
0
        public static bool SortFile(string[] margs)
        {
            "App started.".InfoLognConsole();
            SettingObj mySetting = null;
            bool       a         = false;

            while (!a)
            {
                Console.WriteLine("Reading settings.");

                try
                {
                    mySetting = SettingObj.Read();
                    //mySetting = ((dynamic)Activator.CreateInstance(SettingObj)).Read();
                    a = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error : {0}", ex.Message);
                    "Fail to read settings [{0}]".InfoLognConsole(ex.Message);
                    Console.WriteLine("Sleep 10 sec...");
                    System.Threading.Tasks.Task.Delay(10000);
                }
            }
            EPGAccess  mAccess = null;
            StreamFile mpars   = null;

            if (margs.Any(x => x.Equals("-main", StringComparison.OrdinalIgnoreCase)))
            {
                var wpfwindow = new MasterView.MasterViewControl();
                ElementHost.EnableModelessKeyboardInterop(wpfwindow);
                if (wpfwindow.ShowDialog() == true)
                {
                    return(true);
                }
            }
            if (margs.Any(x => x.Equals("-mirakurun", StringComparison.OrdinalIgnoreCase)))
            {
                var wpfwindow = new MirakurunWarpper.MirakurunViewer();
                ElementHost.EnableModelessKeyboardInterop(wpfwindow);
                if (wpfwindow.ShowDialog() == true)
                {
                    return(true);
                }
            }
            if (margs.Any(x => x.Equals("-setup", StringComparison.OrdinalIgnoreCase)))
            {
                var wpfwindow = new MasterView.Setting();
                ElementHost.EnableModelessKeyboardInterop(wpfwindow);
                if (wpfwindow.ShowDialog() == true)
                {
                    return(true);
                }
            }
            if (margs.Any(x => x.Equals("-rcdbview", StringComparison.OrdinalIgnoreCase)))
            {
                var wpfwindow = new MasterView.RecordedListView();
                ElementHost.EnableModelessKeyboardInterop(wpfwindow);
                if (wpfwindow.ShowDialog() == true)
                {
                    return(true);
                }
            }
            if (margs.Any(x => x.Equals("-cloud", StringComparison.OrdinalIgnoreCase)))
            {
                var wpfwindow = new MasterView.CloudViewer();
                ElementHost.EnableModelessKeyboardInterop(wpfwindow);
                if (wpfwindow.ShowDialog() == true)
                {
                    return(true);
                }
            }
            if (margs.Any(x => x.Equals("-upload", StringComparison.OrdinalIgnoreCase)))
            {
                var wpfwindow = new MasterView.Uploader();
                ElementHost.EnableModelessKeyboardInterop(wpfwindow);
                if (wpfwindow.ShowDialog() == true)
                {
                    return(true);
                }
            }
            if (margs.Any(x => x.Equals("-epgstation", StringComparison.OrdinalIgnoreCase)))
            {
                Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=EPGstation=-=-=-=-=-=-=-=-=-=-=-");
                // Start process the parameters, group them to their own entries.
                List <string> clPara = new List <string>();
                if (margs.Count() <= 1)
                {
                    return(false);/* TODO Change to default(_) if this is not a reference type */
                }
                System.Text.StringBuilder sbb = new System.Text.StringBuilder();
                foreach (var p in margs)
                {
                    if (p.StartsWith("-"))
                    {
                        var tmp = sbb.ToString();
                        if (tmp.Length > 1 && tmp.StartsWith("-"))
                        {
                            clPara.Add(tmp);
                        }
                        sbb.Clear(); // Clear the stringbuilder every time it hits the char "-"
                        sbb.Append(p);
                    }
                    else
                    {
                        sbb.AppendFormat(" {0}", p);
                    }
                }
                "App start with parameter\"{0}\"".InfoLognConsole(sbb.ToString());
                clPara.Add(sbb.ToString()); // Lack of the last run in the loop, so add the last parameter manuly.
                int  id      = -1;
                bool t_check = false;
                try
                {//first see para -id
                    t_check = int.TryParse(clPara.First(x => x.StartsWith("-id")).Substring(4), out id);
                }
                catch
                {
                    t_check = false;
                }
                if (!t_check)
                {//if -id is not working, try get enviroment variable. maybe not working.
                    "parameter -id not found or not a number.".ErrorLognConsole();
                    var e_id = Environment.GetEnvironmentVariable("RECORDEDID");
                    if (!e_id.IsNullOrEmptyOrWhiltSpace() && !int.TryParse(e_id, out id))
                    {
                        "Failed to find \"RECORDEDID\" from Enviorment variable".ErrorLognConsole();
                        "App catch error. exiting...".InfoLognConsole();
                        return(false);
                    }
                    "Environment Variable \"RECORDEDID={0}\"".InfoLognConsole(id.ToString());
                }
                Console.WriteLine("Preparing for access Epgstation server.");
                mAccess = new EPGAccess(mySetting);
                mpars   = mAccess.GetStreamFileObj(id);
                if (mpars == null)
                {
                    "Remote file is missing or \"ID:{0}\" does not exsits.".ErrorLognConsole(id.ToString());
                    "App catch error. exiting...".InfoLognConsole();
                    return(false);
                }
                if (mpars.ChannelName.IsNullOrEmptyOrWhiltSpace())
                {
                    string channel = "";
                    try
                    {
                        channel = clPara.First(x => x.StartsWith("-cn")).Substring(4);
                    }
                    catch
                    {
                        channel = "UNKNOW";
                    }
                    finally
                    {
                        mpars.ChannelName = channel;
                    }
                }
            }
            else
            {
                mpars = GetPara(margs);
            }
            if (mpars == null)
            {
                return(false);
            }
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (var p in margs)
            {
                sb.Append(p + " ");
            }
            PrintInfomation(mpars);
            if (!MoveFile(mpars, mySetting, mAccess))
            {
                "Local file is missing : \"File:{0}\" does not exsit.".ErrorLognConsole(System.IO.Path.GetFileName(mpars.FilePath));
                "App catch error. exiting...".InfoLognConsole();
                return(false);
            }
            ;
            "App has completed job. exiting...".InfoLognConsole();
            return(true);
        }