public void DecryptCourse(List <ListViewItem> list)
        {
            if (string.IsNullOrWhiteSpace(txtCoursePath.Text))
            {
                MessageBox.Show("Please select course path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (string.IsNullOrWhiteSpace(txtDBPath.Text))
            {
                MessageBox.Show("Please select database path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (string.IsNullOrWhiteSpace(txtOutputPath.Text))
            {
                MessageBox.Show("Please select output path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            foreach (ListViewItem item in list)
            {
                CourseItem courseItem = listCourse.Where(r => r.Course.Name == item.Name).Select(r => r).FirstOrDefault();

                if (chkDecrypt.Checked)
                {
                    bgwDecrypt.ReportProgress(1, new Log {
                        Text = $"Start to decrypt course \"{courseItem.Course.Title}\"", TextColor = Color.Magenta, NewLine = true, IsError = true
                    });

                    //Create new course path with the output path
                    var newCoursePath = Path.Combine(txtOutputPath.Text, this.CleanName(courseItem.Course.Title));

                    DirectoryInfo courseInfo = Directory.Exists(newCoursePath)
                        ? new DirectoryInfo(newCoursePath)
                        : Directory.CreateDirectory(newCoursePath);

                    if (chkCopyImage.Checked && File.Exists($"{courseItem.CoursePath}\\image.jpg"))
                    {
                        File.Copy($"{courseItem.CoursePath}\\image.jpg", $"{newCoursePath}\\image.jpg", true);
                    }


                    //Get list all modules in current course
                    List <Module> listModules = courseItem.Course.Modules;

                    if (listModules.Count > 0)
                    {
                        // integer to add 1 if index should start at 1
                        int startAt1 = Convert.ToInt16(chkStartModuleIndexAt1.Checked);
                        //Get each module
                        foreach (Module module in listModules)
                        {
                            //Generate module hash name
                            string moduleHash = this.ModuleHash(module.Name, module.AuthorHandle);
                            //Generate module path
                            string moduleHashPath = Path.Combine(courseItem.CoursePath, moduleHash);
                            //Create new module path with decryption name
                            string newModulePath = Path.Combine(courseInfo.FullName, $"{(startAt1 + module.Index):00}. {module.Title}");

                            if (Directory.Exists(moduleHashPath))
                            {
                                DirectoryInfo moduleInfo = Directory.Exists(newModulePath)
                                    ? new DirectoryInfo(newModulePath)
                                    : Directory.CreateDirectory(newModulePath);
                                //Decrypt all videos in current module folder
                                this.DecryptAllVideos(moduleHashPath, module, moduleInfo.FullName);
                            }
                            else
                            {
                                bgwDecrypt.ReportProgress(1, new Log {
                                    Text = $"Folder {moduleHash} not found in the current course path", TextColor = Color.Red, NewLine = true, IsError = true
                                });
                            }
                        }
                    }
                    bgwDecrypt.ReportProgress(1, new Log {
                        Text = $"Decrypt \"{courseItem.Course.Title}\" complete!", TextColor = Color.Magenta, NewLine = true, IsError = true
                    });
                }

                if (chkDelete.Checked)
                {
                    try
                    {
                        Directory.Delete(courseItem.CoursePath, true);
                    }
                    catch (Exception ex)
                    {
                        bgwDecrypt.ReportProgress(1, new Log {
                            Text = $"Delete folder course {courseItem.Course.Title} fail\n{ex.Message}", TextColor = Color.Gray, NewLine = true, IsError = true
                        });
                    }

                    try
                    {
                        RemoveCourseInDb(courseItem.CoursePath);
                    }
                    catch (Exception ex)
                    {
                        bgwDecrypt.ReportProgress(1, new Log {
                            Text = $"Delete course {courseItem.Course.Title} from db fail\n{ex.Message}", TextColor = Color.Gray, NewLine = true, IsError = true
                        });
                    }


                    bgwDecrypt.ReportProgress(1, new Log {
                        Text = $"Delete course {courseItem.Course.Title} success!", TextColor = Color.Magenta, NewLine = true
                    });
                }
            }

            bgwDecrypt.ReportProgress(100);
        }
Example #2
0
        public void DecryptCourse(List <ListViewItem> list)
        {
            if (string.IsNullOrWhiteSpace(txtCoursePath.Text))
            {
                MessageBox.Show("Please select courses path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (string.IsNullOrWhiteSpace(txtDBPath.Text))
            {
                MessageBox.Show("Please select database path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (string.IsNullOrWhiteSpace(txtOutputPath.Text))
            {
                MessageBox.Show("Please select output path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            foreach (ListViewItem item in list)
            {
                CourseItem courseItem = listCourse.Where(r => r.course.CourseName == item.Name).Select(r => r).FirstOrDefault();

                bgwDecrypt.ReportProgress(1, new { Text = "Starting decrcypt course " + courseItem.course.CourseTitle, Color = Color.Magenta, newLine = true });

                //Create new course path with the output path
                var newCoursePath = Path.Combine(txtOutputPath.Text, this.CleanName(courseItem.course.CourseTitle));

                DirectoryInfo courseInfo = Directory.Exists(newCoursePath)
                    ? new DirectoryInfo(newCoursePath)
                    : Directory.CreateDirectory(newCoursePath);


                //Get list all modules in current course
                List <Module> listModules = courseItem.course.Modules;

                if (listModules.Count > 0)
                {
                    //Get each module
                    foreach (Module module in listModules)
                    {
                        //Generate module hash name
                        string moduleHash = this.ModuleHash(module.ModuleName, module.AuthorHandle);
                        //Generate module path
                        string moduleHashPath = Path.Combine(courseItem.coursePath, moduleHash);
                        //Create new module path with decryption name
                        string newModulePath = Path.Combine(courseInfo.FullName,
                                                            module.ModuleIndex + ". " + module.ModuleTitle);
                        //If length too long, rename it
                        if (newModulePath.Length > 240)
                        {
                            newModulePath = Path.Combine(courseInfo.FullName,
                                                         module.ModuleIndex + "");
                        }

                        if (Directory.Exists(moduleHashPath))
                        {
                            DirectoryInfo moduleInfo = Directory.Exists(newModulePath)
                                ? new DirectoryInfo(newModulePath)
                                : Directory.CreateDirectory(newModulePath);
                            //Decrypt all videos in current module folder
                            this.DecryptAllVideos(moduleHashPath, module, moduleInfo.FullName);
                        }
                        else
                        {
                            //this.AddText("Folder " + moduleHash + " cannot be found in the current course path.", Color.Red, true);
                            bgwDecrypt.ReportProgress(1, new { Text = "Folder " + moduleHash + " cannot be found in the current course path.", Color = Color.Red, newLine = true });
                        }
                    }
                }
                //this.AddText("Decryption " + courseItem.course.CourseTitle + " has been completed!", Color.Magenta, true);
                bgwDecrypt.ReportProgress(1, new { Text = "Decryption " + courseItem.course.CourseTitle + " has been completed!", Color = Color.Magenta, newLine = true });

                if (chkDelete.Checked)
                {
                    Directory.Delete(courseItem.coursePath, true);
                    RemoveCourseInDb(courseItem.coursePath);

                    bgwDecrypt.ReportProgress(1, new { Text = "Delete course " + courseItem.course.CourseTitle + " success!", Color = Color.Magenta, newLine = true });
                }
            }

            bgwDecrypt.ReportProgress(100);
        }
        public void DecryptCourse(List <ListViewItem> list)
        {
            if (string.IsNullOrWhiteSpace(txtCoursePath.Text))
            {
                MessageBox.Show("Please select course path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (string.IsNullOrWhiteSpace(txtDBPath.Text))
            {
                MessageBox.Show("Please select database path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (string.IsNullOrWhiteSpace(txtOutputPath.Text))
            {
                MessageBox.Show("Please select output path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            foreach (ListViewItem item in list)
            {
                CourseItem courseItem = listCourse.Where(r => r.Course.CourseName == item.Name).Select(r => r).FirstOrDefault();

                if (chkDecrypt.Checked)
                {
                    bgwDecrypt.ReportProgress(1, new { Text = "Start to decrypt course \"" + courseItem.Course.CourseTitle + "\"", Color = Color.Magenta, newLine = true });

                    //Create new course path with the output path
                    var newCoursePath = Path.Combine(txtOutputPath.Text, this.CleanName(courseItem.Course.CourseTitle));

                    DirectoryInfo courseInfo = Directory.Exists(newCoursePath)
                        ? new DirectoryInfo(newCoursePath)
                        : Directory.CreateDirectory(newCoursePath);


                    //Get list all modules in current course
                    List <Module> listModules = courseItem.Course.Modules;

                    if (listModules.Count > 0)
                    {
                        // integer to add 1 if index should start at 1
                        int startAt1 = Convert.ToInt16(chkStartModuleIndexAt1.Checked);
                        //Get each module
                        foreach (Module module in listModules)
                        {
                            //Generate module hash name
                            string moduleHash = this.ModuleHash(module.ModuleName, module.AuthorHandle);
                            //Generate module path
                            string moduleHashPath = Path.Combine(courseItem.CoursePath, moduleHash);
                            //Create new module path with decryption name
                            string newModulePath = Path.Combine(courseInfo.FullName,
                                                                (startAt1 + module.ModuleIndex < 10 ? "0" : "") + (startAt1 + module.ModuleIndex) + ". " + module.ModuleTitle);

                            if (Directory.Exists(moduleHashPath))
                            {
                                DirectoryInfo moduleInfo = Directory.Exists(newModulePath)
                                    ? new DirectoryInfo(newModulePath)
                                    : Directory.CreateDirectory(newModulePath);
                                //Decrypt all videos in current module folder
                                this.DecryptAllVideos(moduleHashPath, module, moduleInfo.FullName);
                            }
                            else
                            {
                                bgwDecrypt.ReportProgress(1, new { Text = "Folder " + moduleHash + " cannot be found in the current course path.", Color = Color.Red, newLine = true });
                            }
                        }
                    }
                    bgwDecrypt.ReportProgress(1, new { Text = "\"" + courseItem.Course.CourseTitle + "\" complete!", Color = Color.Magenta, newLine = true });
                }

                if (chkDelete.Checked)
                {
                    try
                    {
                        Directory.Delete(courseItem.CoursePath, true);
                    }
                    catch (Exception ex)
                    {
                        bgwDecrypt.ReportProgress(1, new { Text = "Delete folder course " + courseItem.Course.CourseTitle + " fail!" + Environment.NewLine + ex.Message, Color = Color.Gray, newLine = true });
                    }

                    try
                    {
                        RemoveCourseInDb(courseItem.CoursePath);
                    }
                    catch (Exception ex)
                    {
                        bgwDecrypt.ReportProgress(1, new { Text = "Delete course " + courseItem.Course.CourseTitle + " from Db fail!" + Environment.NewLine + ex.Message, Color = Color.Gray, newLine = true });
                    }


                    bgwDecrypt.ReportProgress(1, new { Text = "Delete course " + courseItem.Course.CourseTitle + " success!", Color = Color.Magenta, newLine = true });
                }
            }

            bgwDecrypt.ReportProgress(100);
        }