Пример #1
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);
        }
Пример #2
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);
        }
Пример #3
0
        public bool LoadFilters()
        {
            // also ensures directory filter exists
            string dataFile = MyFile.EnsureDataFile("Filter", Config.settings.exportExt, "filters");

            IEnumerable <string> files = MyFile.EnumerateFiles("filters", "Filter_*." + Config.settings.exportExt);

            comboBoxFilters.SuspendLayout();

            comboBoxFilters.Items.Clear();
            comboBoxFilters.Items.Add("<_reset_>");
            comboBoxFilters.Items.Add("<filter when closed>");
            comboBoxFilters.Items.Add("<last saved filter>");
            comboBoxFilters.SelectedIndex = 0;
            if (files.Count() > 0)
            {
                Regex regexIgnoreBackups = new Regex(@"\.[0-9]\." + Config.settings.exportExt);
                foreach (string file in files)
                {
                    if (regexIgnoreBackups.IsMatch(file))
                    {
                        continue;
                    }
                    string filterName = file.Replace(@"filters\", "");
                    filterName = filterName.Replace(@"." + Config.settings.exportExt, "");
                    filterName = filterName.Replace(@"Filter_", "");
                    if (filterName == "_reset_" || filterName == "filter when closed" || filterName == "last saved filter")
                    {
                        continue;
                    }
                    comboBoxFilters.Items.Add(filterName);
                }
            }

            comboBoxFilters.ResumeLayout();
            return(true);
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #6
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);
        }