Пример #1
0
        public void DecryptCourse(List <ListViewItem> list)
        {
            if (string.IsNullOrWhiteSpace(coursePathTextBox.Text))
            {
                MessageBox.Show("Please select course path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrWhiteSpace(dbPathTextBox.Text))
            {
                MessageBox.Show("Please select database path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrWhiteSpace(outputPathTextBox.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();

                bgwDecrypt.ReportProgress(1,
                                          new Log
                {
                    Text      = $"Decrypting course \"{courseItem.Course.Title}\"\n",
                    TextColor = Color.Magenta,
                });

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

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

                if (copyImageCheckbox.Checked && File.Exists(Path.Combine(courseItem.CoursePath, "image.jpg")))
                {
                    File.Copy(Path.Combine(courseItem.CoursePath, "image.jpg"), Path.Combine(newCoursePath, "image.jpg"), true);
                }


                //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 = 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,
                                                            $"{module.Index + 1: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
                            DecryptAllVideos(moduleHashPath, module, moduleInfo.FullName,
                                             courseItem.Course.HasTranscript);
                        }
                        else
                        {
                            bgwDecrypt.ReportProgress(1,
                                                      new Log
                            {
                                Text      = $"Folder {moduleHash} not found in the current course path\n",
                                TextColor = Color.Red,
                            });
                        }
                    }
                }

                bgwDecrypt.ReportProgress(1,
                                          new Log
                {
                    Text      = $"\"{courseItem.Course.Title}\" done!\n",
                    TextColor = Color.Magenta,
                });

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

                    if (!RemoveCourseInDb(courseItem.CoursePath))
                    {
                        bgwDecrypt.ReportProgress(1,
                                                  new Log
                        {
                            Text      = $"Cannot delete course \"{courseItem.Course.Title}\" from db.\n",
                            TextColor = Color.Gray,
                        });
                    }

                    bgwDecrypt.ReportProgress(1,
                                              new Log
                    {
                        Text      = $"Course \"{courseItem.Course.Title}\" deleted successfully.\n",
                        TextColor = Color.Magenta
                    });
                }
            }

            bgwDecrypt.ReportProgress(100);
        }
Пример #2
0
        public void DecryptCourse(List <ListViewItem> list)
        {
            if (string.IsNullOrWhiteSpace(txtCoursePath.Text))
            {
                MessageBox.Show("Please select course path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrWhiteSpace(txtDBPath.Text))
            {
                MessageBox.Show("Please select database path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            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, courseItem.Course.HasTranscript);
                            }
                            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
                        });
                    }

                    if (!RemoveCourseInDb(courseItem.CoursePath))
                    {
                        bgwDecrypt.ReportProgress(1, new Log {
                            Text = $"Delete course {courseItem.Course.Title} from db fail", 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);
        }