Ejemplo n.º 1
0
        public static bool Decompress(string source)
        {
            FileInfo sourceFileInfo = MyFile.FileInfo(source);

            if (sourceFileInfo == null)
            {
                return(false);
            }
            try
            {
                using (FileStream originalFileStream = sourceFileInfo.OpenRead())
                {
                    string currentFileName = sourceFileInfo.FullName;
                    string newFileName     = currentFileName.Remove(currentFileName.Length - sourceFileInfo.Extension.Length);

                    using (FileStream decompressedFileStream = File.Create(newFileName))
                    {
                        using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                        {
                            decompressionStream.CopyTo(decompressedFileStream);
                            MyLog.Add(String.Format("Decompressed {0} from {1} to {2}", sourceFileInfo.Name, MyFile.FormatSize(sourceFileInfo.Length), MyFile.FormatSize(decompressionStream.Length)));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MyLog.Add(e.ToString());
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
0
        public static bool Compress(string source, string target, string targetExt)
        {
            FileInfo sourceFileInfo = MyFile.FileInfo(source);

            if (sourceFileInfo == null)
            {
                return(false);
            }
            try
            {
                using (FileStream originalFileStream = sourceFileInfo.OpenRead())
                {
                    using (FileStream compressedFileStream = File.Create(target + targetExt))
                    {
                        using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                        {
                            originalFileStream.CopyTo(compressionStream);
                            MyLog.Add(String.Format("Compressed {0} from {1} to {2}", sourceFileInfo.Name, MyFile.FormatSize(sourceFileInfo.Length), MyFile.FormatSize(compressedFileStream.Length)));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MyLog.Add(e.ToString());
                return(false);
            }
            return(true);
        }
        public FileInfo GetFileInfo()
        {
            string fullName = GetFullName();

            if (fullName == null)
            {
                return(null);
            }
            FileInfo fileInfo = MyFile.FileInfo(fullName);

            return(fileInfo);
        }
Ejemplo n.º 4
0
        public static bool Save()
        {
            if (listVideoInfos == null)
            {
                return(false);
            }

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            MyLog.Add("Saving VideoItems -->");

            foreach (ConfigSettings.Source settingsSource in Config.settings.sources)
            {
                string dataFile = MyFile.EnsureDataFile("videos", Config.settings.exportExt, "data", settingsSource.alias);
                if (dataFile == null)
                {
                    continue;
                }

                List <VideoInfo> saveVideoInfos = new List <VideoInfo>(listVideoInfos.Count());
                saveVideoInfos = listVideoInfos.Where(x => x.sourceAlias == settingsSource.alias && x.videoItem != null && x.videoItem.title != null).OrderBy(x => x.videoItem.title).ToList();


                if (saveVideoInfos == null)
                {
                    continue;
                }
                int nbrVideoInfos = saveVideoInfos.Count();
                if (nbrVideoInfos == 0)
                {
                    continue;
                }

                MyLog.RotateFiles(dataFile);
                MySerialize.ToFile(Config.settings.exportFormat, dataFile, saveVideoInfos);


                FileInfo fileInfo = MyFile.FileInfo(dataFile);
                string   toFile   = "to " + dataFile.Replace(MyFile.exeDirectory, "");
                if (fileInfo != null)
                {
                    toFile += " " + MyFile.FormatSize(fileInfo.Length);
                }
                MyLog.Add("Saved " + nbrVideoInfos + " VideoItems " + toFile);
            }

            stopWatch.Stop();
            MyLog.AddElapsed("<-- in ", stopWatch.Elapsed);

            return(true);
        }
        public static bool Load()
        {
            bool      ret       = false;
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            // MyLog.Add("Loading VideoItemStats");

            string dataFile = MyFile.EnsureDataFile("stats", Config.settings.exportExt, "stats");

            if (dataFile == null)
            {
                return(false);
            }

            // keep a backup of settings
            if (File.Exists(dataFile))
            {
                videoInfoStats = new VideoInfoStats();
                videoInfoStats = (VideoInfoStats)MyDeserialize.FromFile(Config.settings.exportFormat, dataFile, videoInfoStats);

                string   fromFIle = "from " + dataFile.Replace(MyFile.exeDirectory, "");
                FileInfo fileInfo = MyFile.FileInfo(dataFile);
                if (fileInfo != null)
                {
                    fromFIle += " " + MyFile.FormatSize(fileInfo.Length);
                }
                MyLog.Add("Loaded VideoInfoStats " + fromFIle);

                if (videoInfoStats != null && videoInfoStats.year != null && videoInfoStats.year.Count() > 0)
                {
                    ret = true;
                }
            }
            else
            {
                // MyLog.Add("No VideoInfoStats to load ");
            }

            stopWatch.Stop();

            if (ret)
            {
                MyLog.AddElapsed(stopWatch.Elapsed);
            }

            return(ret);
        }
Ejemplo n.º 6
0
        private bool EmptyGalleryCache()
        {
            IEnumerable <string> files = MyFile.EnumerateFiles(@"cache\gallery");

            // MyLog.Add("Clean up cache dir: " + files.Count() + " files");
            foreach (string file in files)
            {
                FileInfo fileInfo = MyFile.FileInfo(file);
                // if (fileInfo != null && fileInfo.LastAccessTime < DateTime.Now.AddMinutes(-5))
                if (fileInfo != null)
                {
                    fileInfo.Delete();
                }
            }

            return(true);
        }
Ejemplo n.º 7
0
        public static bool RotateLogs(string file)
        {
            if (File.Exists(file))
            {
                // rotate log, if bigger than X KB
                int      rotateLogSize = 250 * 1024;
                FileInfo logFileInfo   = MyFile.FileInfo(file);
                if (logFileInfo.Length >= rotateLogSize)
                {
                    RotateFiles(file);

                    MyFile.DeleteFile(file);
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 8
0
        public bool Play(VideoInfo videoInfo, VideoItemFile videoItemFile)
        {
            bool ret = false;
            // if playing video, mark video item as played/watched
            // TODO btr way to normalize if a file is a video file
            string fileExt = videoItemFile.Extension.TrimStart('.');

            switch (fileExt)
            {
            case "avi":
            case "m4v":
            case "mov":
            case "mpg":
            case "mkv":
            case "mp4":
            case "mpeg":
                // run as background so can wait w/o blocking ui
                // after X secs timeout, mark as played/watched
                // .. but requires UseShellEx = false which requires more config/settings to specify video player
                // so for now, meh runs as background thread w/o wait
                VideoItemFileInfo videoItemFileInfo = new VideoItemFileInfo();
                videoItemFileInfo.videoInfo     = videoInfo;
                videoItemFileInfo.videoItemFile = videoItemFile;
                backgroundWorkerOpenFile.Run(videoItemFileInfo);
                break;

            default:
                string   fullName = videoInfo.GetFullName(videoItemFile);
                FileInfo fileInfo = MyFile.FileInfo(fullName);
                if (fileInfo == null)
                {
                    // MessageBox.Show("Error trying to open file\n"+fullName+"\nView log for details");
                    ret = false;
                }
                else
                {
                    ret = MyFile.RunFile(fileInfo);
                }
                break;
            }

            return(ret);
        }
Ejemplo n.º 9
0
        public static long DirectorySize(string directory, string searchPattern = null)
        {
            long size = 0;
            IEnumerable <string> files = MyFile.EnumerateFiles(directory, searchPattern);

            if (files.Count() == 0)
            {
                return(size);
            }

            foreach (string file in files)
            {
                FileInfo fileInfo = MyFile.FileInfo(file);

                if (fileInfo == null)
                {
                    continue;
                }
                size += fileInfo.Length;
            }
            return(size);
        }
Ejemplo n.º 10
0
        private void buttonPlay_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            if (selectedVideoInfo != null && selectedVideoInfo.files != null && selectedVideoInfo.files.video != null)
            {
                button.Enabled = false;
                button.Text    = "Playing..";

                PlayFile playFile = new PlayFile();
                playFile.playFile_Completed += (senderPlay, eventPlay) => PlayFile_Completed(senderPlay, eventPlay, selectedVideoInfo);
                playFile.AddAccessToSubForms(this, subFormProgress);
                string   videoFullName = selectedVideoInfo.GetFullName(selectedVideoInfo.files.video);
                FileInfo fileInfo      = MyFile.FileInfo(videoFullName);
                if (fileInfo == null)
                {
                    MessageBox.Show("Error trying to play video [" + selectedVideoInfo.files.video.Name + "]");
                    return;
                }
                playFile.Play(selectedVideoInfo, selectedVideoInfo.files.video);

                MyFormField.DelayButtonClick(button, "Play");
            }
        }
Ejemplo n.º 11
0
        public static bool Load()
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            MyLog.Add("Loading VideoItems -->");

            int      nbrVideoInfos;
            FileInfo fileInfo;

            CreateList();
            foreach (ConfigSettings.Source source in Config.settings.sources)
            {
                string dataFile = MyFile.EnsureDataFile("videos", Config.settings.exportExt, "data", source.alias);
                if (dataFile == null)
                {
                    continue;
                }

                // keep a backup of settings
                if (File.Exists(dataFile))
                {
                    fileInfo = MyFile.FileInfo(dataFile);
                    List <VideoInfo> loadVideoInfos = (List <VideoInfo>)MyDeserialize.FromFile(Config.settings.exportFormat, dataFile, listVideoInfos);
                    if (loadVideoInfos == null)
                    {
                        MyLog.Add("Unable to load VideoInfos from " + dataFile);
                        continue;
                    }
                    AddItems(loadVideoInfos);

                    string fromFile = "from " + dataFile.Replace(MyFile.exeDirectory, "");
                    if (fileInfo != null)
                    {
                        fromFile += " " + MyFile.FormatSize(fileInfo.Length);
                    }

                    if (loadVideoInfos == null)
                    {
                        nbrVideoInfos = 0;
                    }
                    else
                    {
                        nbrVideoInfos = loadVideoInfos.Count();
                    }
                    MyLog.Add("Loaded " + nbrVideoInfos + " VideoItems " + fromFile);
                }
            }

            EnsureVideoInfos();

            stopWatch.Stop();


            if (listVideoInfos == null)
            {
                nbrVideoInfos = 0;
            }
            else
            {
                nbrVideoInfos = listVideoInfos.Count();
            }

            MyLog.AddElapsed("<-- in ", stopWatch.Elapsed);

            bool loaded = nbrVideoInfos > 0 ? true : false;

            return(loaded);
        }
Ejemplo n.º 12
0
        public bool SyncUpVideoSource(FileInfo sourceFileInfo)
        {
            // clean up old uploads
            IEnumerable <string> files = MyFile.EnumerateFiles(@"sync", "*.gz");

            foreach (string file in files)
            {
                MyFile.DeleteFile(file);
            }

            // compress file, video source
            string compressedFile = @"sync\" + MyFile.SafeFileName(sourceFileInfo.Name);

            if (!MyFile.Compress(sourceFileInfo.FullName, compressedFile))
            {
                return(false);
            }

            // rename file so has 'rand' key/iv
            Random random     = new Random();
            int    rand       = random.Next(0, MyEncrypt.sharedKeys.Length - 1);
            string uploadFile = compressedFile.Replace("." + Config.settings.exportExt, "-" + rand + "." + Config.settings.exportExt);

            File.Move(compressedFile + ".gz", uploadFile + ".gz");



            string key       = MyEncrypt.sharedKeys[rand];
            string iv        = MyEncrypt.GenerateIV();
            string delimiter = "#";

            // now encrypt compressed file contents
            string fileContents = MyFile.ReadAllBinaryToString(uploadFile + ".gz");

            string contentsHeader = "{";

            contentsHeader += "\"apiKey\":\"" + this.apiKey + "\", ";
            contentsHeader += "\"iv\":\"" + iv + "\", ";
            contentsHeader += "\"sync\":" + MySerialize.ToJSON(Sync.syncSettings);
            contentsHeader += "}";

            contentsHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes(contentsHeader));



            fileContents = MyEncrypt.EncryptRJ256(key, iv, fileContents);
            if (fileContents == null)
            {
                return(false);
            }

            contentsHeader = MyEncrypt.EncryptRJ256(key, iv, contentsHeader);

            string contentsToEncode = contentsHeader + delimiter + fileContents;

            // write base64 encoded file
            File.WriteAllText(uploadFile + ".enc", contentsToEncode);


            // log it
            FileInfo encodedFileInfo = MyFile.FileInfo(uploadFile + ".enc");

            if (encodedFileInfo == null)
            {
                return(false);
            }
            MyLog.Add(String.Format("Encrypted {0} to {1}", encodedFileInfo.Name, MyFile.FormatSize(encodedFileInfo.Length)));


            // test decrypt

            fileContents = MyFile.ReadAllText(uploadFile + ".enc");

            string[] fileParts = fileContents.Split(new string[] { delimiter }, StringSplitOptions.None);

            fileContents = MyEncrypt.DecryptRJ256(key, iv, fileParts[1]);



            MyFile.DeleteFile(uploadFile + ".gz");
            MyFile.WriteAllBinaryFromString(uploadFile + ".gz", fileContents);



            // post encoded file to website

            string url = apiURL;
            List <KeyValuePair <string, string> > headers = new List <KeyValuePair <string, string> > {
            };

            headers.Add(new KeyValuePair <string, string>("api-key", apiKey));
            headers.Add(new KeyValuePair <string, string>("access-token", iv));
            Upload(url, uploadFile + ".enc", headers);



            return(true);
        }
Ejemplo n.º 13
0
        public bool SetList()
        {
            listViewSyncUp.SuspendLayout();

            listViewSyncUp.Items.Clear();

            // string syncFile;
            string       fileSize;
            FileInfo     fileInfo;
            ListViewItem listViewItem;

            /*
             * // get sync info
             * syncFile = @"sync\sync." + Config.settings.exportExt;
             * fileInfo = MyFile.FileInfo(syncFile);
             * if (fileInfo == null)
             * {
             *  Sync.Save();
             *  fileInfo = MyFile.FileInfo(syncFile);
             *  if (fileInfo == null)
             *  {
             *      MessageBox.Show("Unable to create [" + syncFile + "]");
             *      MyLog.Add("Unable to create [" + syncFile + "]");
             *      return false;
             *  }
             * }
             * listViewItem = new ListViewItem("Sync");
             * listViewItem.SubItems.Add("-");
             * fileSize = Convert.ToString(MyFile.FormatSize(fileInfo.Length));
             * listViewItem.SubItems.Add(fileSize);
             * listViewItem.Tag = fileInfo;
             *
             * listViewSyncUp.Items.Add(listViewItem);
             */

            // get all sources
            IEnumerable <string> files = MyFile.EnumerateFiles("data", "videos_*." + Config.settings.exportExt);
            Regex regexIgnoreBackups   = new Regex(@"\.[0-9]\." + Config.settings.exportExt);

            foreach (string file in files)
            {
                if (regexIgnoreBackups.IsMatch(file))
                {
                    continue;
                }
                fileInfo = MyFile.FileInfo(file);
                if (fileInfo == null)
                {
                    continue;
                }
                string source = file.Replace(@"data\videos_", "").Replace("." + Config.settings.exportExt, "");
                int    qty    = CalcStatsForSource(source);
                if (qty == 0)
                {
                    MyLog.Add("Source [" + source + "] seems to have 0 videos, skipping");
                    continue;
                }
                listViewItem = new ListViewItem(source);
                listViewItem.SubItems.Add(qty.ToString());
                fileSize = Convert.ToString(MyFile.FormatSize(fileInfo.Length));
                listViewItem.SubItems.Add(fileSize);
                listViewItem.Tag = fileInfo;

                listViewSyncUp.Items.Add(listViewItem);
            }


            ResizeColumns();


            listViewSyncUp.ResumeLayout();

            return(true);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// read the video directory and try to parse video info
        /// such as XBMC, MB
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public VideoInfo ReadDirectory(string directory)
        {
            bool      parsedFile = false;
            VideoInfo videoInfo  = new VideoInfo();

            videoInfo.Initialize();
            videoInfo.videoDirectory = directory;
            VideoInfo parsedVideoInfo = new VideoInfo();

            parsedVideoInfo.Initialize();
            parsedVideoInfo.videoDirectory = directory;

            IEnumerable <string> files = MyFile.EnumerateFiles(directory);

            if (files.Count() == 0)
            {
                // empty folder .. maybe a category folder, or movie placeholder
                return(videoInfo);
            }


            // MyLog.Add("ReadDirectory: " + files.Count() + " files");
            foreach (string file in files)
            {
                FileInfo fileInfo = MyFile.FileInfo(file);

                if (fileInfo == null)
                {
                    continue;
                }
                VideoItemFile videoItemFile = new VideoItemFile();
                videoItemFile.Set(directory, fileInfo);

                // parse nfo/xml/file to get file info

                videoInfo.files.qty += 1;

                string fileExt = fileInfo.Extension.TrimStart('.');
                switch (fileExt)
                {
                case "avi":
                case "m4v":
                case "mov":
                case "mpg":
                case "mkv":
                case "mp4":
                case "mpeg":
                    videoInfo.files.video = videoItemFile;

                    //if (videoInfo.videoItem.encoding == null)
                    //{
                    string encoding = RunFFprobe(videoInfo.GetFullName(videoInfo.files.video));
                    if (encoding != null)
                    {
                        videoInfo.videoItem.encoding = ParseFFprobe(encoding);
                    }
                    //}
                    break;

                case "jpg":
                case "jpeg":
                case "png":
                    if (fileInfo.Name == "poster.jpg")
                    {
                        videoInfo.files.poster = videoItemFile;
                    }
                    else if (fileInfo.Name == "fanart.jpg")
                    {
                        videoInfo.files.fanart = videoItemFile;
                    }
                    videoInfo.files.images.Add(videoItemFile);
                    break;

                case "mve":
                case "nfo":
                case "xml":
                    // TODO btr way to ensure which type parsing

                    string fileContents = MyFile.ReadAllText(fileInfo.FullName);
                    fileContents = fileContents.Trim();
                    if (String.IsNullOrEmpty(fileContents))
                    {
                        continue;
                    }
                    if (fileContents.StartsWith("<?xml"))
                    {
                        XmlDocument doc = MyXMLDoc.LoadXml(fileContents);
                        if (doc == null)
                        {
                            continue;
                        }



                        if (fileContents.Contains("<VideoInfo>"))
                        {
                            //if (parsedFile == false)
                            {
                                parsedVideoInfo = ParseMVE(fileInfo.FullName);
                                videoInfo.MergeVideoItemWith(parsedVideoInfo);
                                // videoInfo.videoItem = parsedVideoInfo.videoItem;
                            }
                            videoInfo.files.mve = videoItemFile;

                            parsedFile = true;
                        }
                        if (fileContents.Contains("<Title>"))
                        {
                            //if (parsedFile == false)
                            {
                                parsedVideoInfo = ParseMB(doc);
                                videoInfo.MergeVideoItemWith(parsedVideoInfo);
                                //videoInfo.videoItem = parsedVideoInfo.videoItem;
                            }
                            videoInfo.files.mb = videoItemFile;

                            parsedFile = true;
                        }
                        if (fileContents.Contains("<movie>"))
                        {
                            //if (parsedFile == false)
                            {
                                parsedVideoInfo = ParseXBMC(doc);
                                videoInfo.MergeVideoItemWith(parsedVideoInfo);
                                //videoInfo.videoItem = parsedVideoInfo.videoItem;
                            }
                            videoInfo.files.xbmc = videoItemFile;

                            parsedFile = true;
                        }
                    }
                    else if (fileContents.StartsWith("{"))
                    {
                        //if (parsedFile == false)
                        {
                            parsedVideoInfo = ParseMVE(fileInfo.FullName);
                            videoInfo.MergeVideoItemWith(parsedVideoInfo);
                            // videoInfo.videoItem = parsedVideoInfo.videoItem;
                        }
                        videoInfo.files.mve = videoItemFile;

                        parsedFile = true;
                    }

                    break;

                case "txt":
                    if (videoItemFile.Name.StartsWith("handbrake"))
                    {
                        parsedVideoInfo = ParseOtherHandrake(videoInfo.GetFullName(videoItemFile), videoItemFile);
                        videoInfo.MergeVideoItemWith(parsedVideoInfo);
                    }
                    else
                    {
                        parsedVideoInfo = ParseOtherFileName(videoInfo.GetFullName(videoItemFile), videoItemFile);
                        videoInfo.MergeVideoItemWith(parsedVideoInfo);
                    }

                    videoInfo.files.others.Add(videoItemFile);
                    break;

                default:
                    videoInfo.files.others.Add(videoItemFile);
                    break;
                } // end switch ext
            }     // end foreach file


            // couldnt parse nfo/xml and have video file in dir, so use video file for some basic info
            if (!parsedFile && videoInfo.files != null && videoInfo.files.video != null)
            {
                parsedVideoInfo = ParseVideoFile(videoInfo.files.video);
                videoInfo.MergeVideoItemWith(parsedVideoInfo);

                parsedFile = true;
            }

            /* .. no video file .. so skip
             * // couldnt parse nfo/xml and have files in dir, so use dir for some basic info
             * else if (!parsedFile && files.Count() > 0)
             * {
             *  DirectoryInfo directoryInfo = new DirectoryInfo(directory);
             *  parsedVideoInfo = ParseDirectory(directoryInfo);
             *  videoInfo.MergeVideoItemWith(parsedVideoInfo);
             *
             *  parsedFile = true;
             * }
             */

            if (videoInfo.videoItem.title == null || videoInfo.files == null || videoInfo.files.video == null)
            {
                videoInfo = null;
            }
            else
            {
                // sanitize some data

                // remove (year) from title .. bad prior parse mb, xbmc
                videoInfo.videoItem.title = Regex.Replace(videoInfo.videoItem.title, @"\([0-9]{4}\)$", "");
            }

            return(videoInfo);
        }