public void DescomprimirArquivo(string ArquivosZip, string caminho)
        {
            MemoryStream stream = new MemoryStream(Convert.FromBase64String(ArquivosZip));

            Ionic.Zip.ZipInputStream zipInputStream = new Ionic.Zip.ZipInputStream(stream);
            IList <ArquivoPdf>       list           = new List <ArquivoPdf>();

            Ionic.Zip.ZipEntry nextEntry;
            while ((nextEntry = zipInputStream.GetNextEntry()) != null)
            {
                MemoryStream memoryStream = new MemoryStream();
                long         num          = nextEntry.UncompressedSize;
                byte[]       array        = new byte[4096];
                while (true)
                {
                    num = (long)zipInputStream.Read(array, 0, array.Length);
                    if (num <= 0L)
                    {
                        break;
                    }
                    memoryStream.Write(array, 0, (int)num);
                }
                File.WriteAllBytes(caminho, memoryStream.ToArray());
                memoryStream.Close();
            }
        }
Exemple #2
0
        public static byte[] UnIZip(this byte[] data)
        {
            if (data == null)
            {
                return(null);
            }
            if (data.Length == 0)
            {
                return(new byte[0]);
            }

            using (MemoryStream output = new MemoryStream())
            {
                using (MemoryStream input = new MemoryStream(data))
                {
                    using (Ionic.Zip.ZipInputStream stream = new Ionic.Zip.ZipInputStream(input))
                    {
                        var Entry = stream.GetNextEntry();
                        stream.CopyTo(output);
                        return(output.ToArray());
                    }
                }
            }
        }
        //Allows the user to upload new Answers along with their respective
        //Practice Problem and the answer to that Practice Problem
        public ActionResult Upload(UploadModel model, string returnUrl)
        {
            if (!Request.IsAuthenticated) { return RedirectToAction("ResourceUnavailable", "Home"); }
            if (!(User.Identity.Name.Equals("mcaskilladmin") || User.Identity.Name.Equals("perkinsadmin") || User.Identity.Name.Equals("uploadadmin") || User.Identity.Name.Equals("administrator")))
            { return RedirectToAction("ResourceUnavailable", "Home"); }

            List<ViewDataUploadFilesResult> r = new List<ViewDataUploadFilesResult>();

            HttpPostedFileBase hpf = null;// = Request.Files[file] as HttpPostedFileBase;
            String FileName = null;

            foreach (string file in Request.Files)
            {
                hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf.ContentLength == 0)
                    continue;
                //string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images\\" + Path.GetFileName(hpf.FileName));
                string savedFileName = Path.Combine(Path.GetFileName(hpf.FileName));
                //hpf.SaveAs(savedFileName);//Replace this with database insertion
                FileName = Path.GetFileName(hpf.FileName);// hpf.FileName;

                if (FileName.EndsWith(".zip"))
                {
                    var zip = new Ionic.Zip.ZipInputStream(hpf.InputStream);
                    Ionic.Zip.ZipEntry zipEntry;

                    while ((zipEntry = zip.GetNextEntry()) != null)
                    {
                        // Read the entire file
                        var data = new byte[zipEntry.UncompressedSize];
                        zip.Read(data, 0, (int)zipEntry.UncompressedSize);

                        ViewData["Info"] += "<p>" + zipEntry.Info + "File Name: " + zipEntry.FileName + "</p>";

                        // Create new file
                        //var f = new file();
                        //f.site_id = m_Site.site_id;
                        //f.data = data;
                        //f.filename = dest_filename;
                        //f.folder = dest_folder;

                        // Save it
                        //jabDB.Save(f);

                        //ObjectToFile(data, "../../../Content/" + zipEntry.FileName);
                        if (zipEntry.FileName.EndsWith(".jpg") || zipEntry.FileName.EndsWith(".gif"))
                        {
                            ViewData["Info"] += "File Upload: " + AddImageFromZip(data, zipEntry.FileName);
                        }
                        else
                        {
                            ViewData["Info"] += "File Upload: " + AddFromZip(data, zipEntry.FileName);
                        }
                    }
                return View("Upload", r);
                }//New*/
                else if (FileName.EndsWith(".jpg") || FileName.EndsWith(".gif"))
                {
                    AddImage(hpf, FileName, model);
                }
                else
                {
                    AddSolution(hpf, FileName, model);
                }
                /*r.Add(new ViewDataUploadFilesResult()
                {
                    Name = savedFileName,
                    Length = hpf.ContentLength
                });*/

            }
            ViewData["FileName"] = FileName;
            if(User.Identity.Name.Equals("administrator")){}
            return View("Upload", r);
        }
        //Allows the user to upload new Answers along with their respective
        //Practice Problem and the answer to that Practice Problem
        public ActionResult Upload(UploadModel model, string returnUrl)
        {
            List<ViewDataUploadFilesResult> r = new List<ViewDataUploadFilesResult>();

            HttpPostedFileBase hpf = null;// = Request.Files[file] as HttpPostedFileBase;
            String FileName = null;

            foreach (string file in Request.Files)
            {
                hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf.ContentLength == 0)
                    continue;
                //string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images\\" + Path.GetFileName(hpf.FileName));
                string savedFileName = Path.Combine(Path.GetFileName(hpf.FileName));
                //hpf.SaveAs(savedFileName);//Replace this with database insertion
                FileName = Path.GetFileName(hpf.FileName);// hpf.FileName;

                var zip = new Ionic.Zip.ZipInputStream(hpf.InputStream);//NEW
                Ionic.Zip.ZipEntry zipEntry;
                while ((zipEntry = zip.GetNextEntry()) != null)
                {
                    // Read the entire file
                    var data = new byte[zipEntry.UncompressedSize];
                    zip.Read(data, 0, (int)zipEntry.UncompressedSize);

                    // Create new file
                    /*var f = new file();
                    f.site_id = m_Site.site_id;
                    f.data = data;
                    f.filename = dest_filename;
                    f.folder = dest_folder;

                    // Save it
                    jabDB.Save(f);*/
                }//New

                r.Add(new ViewDataUploadFilesResult()
                {
                    Name = savedFileName,
                    Length = hpf.ContentLength
                });

                AnswerApp.Models.AnswerAppDataContext db = new AnswerApp.Models.AnswerAppDataContext();

                //Disect the file name for it's file properties
                String[] properties = FileName.Split(new char[1] { '_' });
                String Textbook_Title = properties[0];
                String Unit_Title = properties[1];
                String Chapter_Title = properties[2];
                String Section_Title = properties[3];
                String Page_Number = properties[4];
                String Question_Number = properties[5].Split(new char[1] { '.' })[0];//Truncate ".pdf" from the end of the file name
                String Practice_Problem = null;
                if (properties.Length > 6) { Practice_Problem = properties[6]; }//An 7th argument indicates a Practice Problem
                if (Practice_Problem != null) { Practice_Problem = properties[6].Split(new char[1] { '.' })[0]; }//Truncate ".pdf" from the end of the file name

                //Search teh database for this Textbook
                IQueryable<Textbook> RetrievedTextbooks = from theTextbooks in db.Textbooks
                                                          where theTextbooks.Title.Equals(Textbook_Title)
                                                          select theTextbooks;
                Textbook[] TextbookResults = RetrievedTextbooks.ToArray<Textbook>();
                if (TextbookResults.Length == 0)//The Textbook does not yet exists
                {
                    //Create a new Textbook
                    AnswerApp.Models.Textbook theTextbook = new AnswerApp.Models.Textbook();

                    //Populate the Textbook with the properties extracted from the file name
                    theTextbook.Title = Textbook_Title;

                    db.Textbooks.InsertOnSubmit(theTextbook);
                    db.SubmitChanges();
                }

                //Search teh database for this Unit
                IQueryable<Unit> RetrievedUnits = from theUnits in db.Units
                                                  where theUnits.Textbook_Title.Equals(Textbook_Title)
                                                  && theUnits.Unit_Title.Equals(Unit_Title)
                                                  select theUnits;
                Unit[] UnitResults = RetrievedUnits.ToArray<Unit>();
                if (UnitResults.Length == 0)//The Unit does not yet exists
                {
                    //Create a new Unit
                    AnswerApp.Models.Unit theUnit = new AnswerApp.Models.Unit();

                    //Populate the Unit with the properties extracted from the file name
                    theUnit.Textbook_Title = Textbook_Title;
                    theUnit.Unit_Title = Unit_Title;
                    //Populate the relational Id's based on previous hierarchical entries
                    theUnit.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;

                    db.Units.InsertOnSubmit(theUnit);
                    db.SubmitChanges();
                }

                //Search the database for this Chapter
                IQueryable<Chapter> RetrievedChapters = from theChapters in db.Chapters
                                                        where theChapters.Textbook_Title.Equals(Textbook_Title)
                                                        && theChapters.Unit_Title.Equals(Unit_Title)
                                                        && theChapters.Chapter_Title.Equals(Chapter_Title)
                                                        select theChapters;
                Chapter[] ChapterResults = RetrievedChapters.ToArray<Chapter>();
                if (ChapterResults.Length == 0)//The Chapter does not yet exists
                {
                    //Create a new Chapter
                    AnswerApp.Models.Chapter theChapter = new AnswerApp.Models.Chapter();

                    //Populate the Chapter with the properties extracted from the file name
                    theChapter.Textbook_Title = Textbook_Title;
                    theChapter.Unit_Title = Unit_Title;
                    theChapter.Chapter_Title = Chapter_Title;
                    //Populate the relational Id's based on previous hierarchical entries
                    theChapter.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                    theChapter.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;

                    db.Chapters.InsertOnSubmit(theChapter);
                    db.SubmitChanges();
                }

                //Search teh database for this Section
                IQueryable<Section> RetrievedSections = from theSections in db.Sections
                                                        where theSections.Textbook_Title.Equals(Textbook_Title)
                                                        && theSections.Unit_Title.Equals(Unit_Title)
                                                        && theSections.Chapter_Title.Equals(Chapter_Title)
                                                        && theSections.Section_Title.Equals(Section_Title)
                                                        select theSections;
                Section[] SectionResults = RetrievedSections.ToArray<Section>();
                if (SectionResults.Length == 0)//The Section does not yet exists
                {
                    //Create a new Section
                    AnswerApp.Models.Section theSection = new AnswerApp.Models.Section();

                    //Populate the Section with the properties extracted from the file name
                    theSection.Textbook_Title = Textbook_Title;
                    theSection.Unit_Title = Unit_Title;
                    theSection.Chapter_Title = Chapter_Title;
                    theSection.Section_Title = Section_Title;
                    //Populate the relational Id's based on previous hierarchical entries
                    theSection.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                    theSection.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                    theSection.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;

                    db.Sections.InsertOnSubmit(theSection);
                    db.SubmitChanges();
                }

                //Search teh database for this Page
                IQueryable<Page> RetrievedPages = from thePages in db.Pages
                                                  where thePages.Textbook_Title.Equals(Textbook_Title)
                                                  && thePages.Unit_Title.Equals(Unit_Title)
                                                  && thePages.Chapter_Title.Equals(Chapter_Title)
                                                  && thePages.Section_Title.Equals(Section_Title)
                                                  && thePages.Page_Number.Equals(Page_Number)
                                                  select thePages;
                Page[] PageResults = RetrievedPages.ToArray<Page>();
                if (PageResults.Length == 0)//The Page does not yet exists
                {
                    //Create a new Page
                    AnswerApp.Models.Page thePage = new AnswerApp.Models.Page();

                    //Populate the Page with the properties extracted from the file name
                    thePage.Textbook_Title = Textbook_Title;
                    thePage.Unit_Title = Unit_Title;
                    thePage.Chapter_Title = Chapter_Title;
                    thePage.Section_Title = Section_Title;
                    thePage.Page_Number = Page_Number;
                    //Populate the relational Id's based on previous hierarchical entries
                    thePage.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                    thePage.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                    thePage.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;
                    thePage.Section_Id = db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id;

                    db.Pages.InsertOnSubmit(thePage);
                }

                //Search teh database for this Question
                IQueryable<Question> retrieved = from theAnswers in db.Questions
                                                    where theAnswers.Textbook_Title.Equals(Textbook_Title)
                                                    && theAnswers.Unit_Title.Equals(Unit_Title)
                                                    && theAnswers.Chapter_Title.Equals(Chapter_Title)
                                                    && theAnswers.Section_Title.Equals(Section_Title)
                                                    && theAnswers.Page_Number.Equals(Page_Number)
                                                    && theAnswers.Question_Number.Equals(Question_Number)
                                                    select theAnswers;
                Question[] results = retrieved.ToArray<Question>();
                if (results.Length != 0)//The Answer already exists
                {
                    //Use the existing Question
                    AnswerApp.Models.Question theQuestion = results.First();

                    if (Practice_Problem != null)//This is a Practice Problem
                    {
                        theQuestion.Practice_Problem = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                        theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                    }
                    else//(Practice_Problem == null) This is an Answer
                    {
                        theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                        theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                    }
                }
                else//(results.Length == 0) This is a new Answer
                {
                    //Create a new Question
                    AnswerApp.Models.Question theQuestion = new AnswerApp.Models.Question();

                    //Populate the Question with the properties extracted from the file name
                    theQuestion.Textbook_Title = Textbook_Title;
                    theQuestion.Unit_Title = Unit_Title;
                    theQuestion.Chapter_Title = Chapter_Title;
                    theQuestion.Section_Title = Section_Title;
                    theQuestion.Page_Number = Page_Number;
                    theQuestion.Question_Number = Question_Number;
                    //Populate the relational Id's based on previous hierarchical entries
                    theQuestion.Textbook_Id = db.Textbooks.Single(d => d.Title.Equals(Textbook_Title)).Unique_Id;
                    theQuestion.Unit_Id = db.Units.Single(d => d.Unit_Title.Equals(Unit_Title)).Unit_Id;
                    theQuestion.Chapter_Id = db.Chapters.Single(d => d.Chapter_Title.Equals(Chapter_Title)).Chapter_Id;
                    theQuestion.Section_Id = db.Sections.Single(d => d.Section_Title.Equals(Section_Title)).Section_Id;
                    theQuestion.Page_Id = db.Pages.Single(d => d.Page_Number.Equals(Page_Number)).Page_Id;

                    if (Practice_Problem != null)//This is a Practice Problem
                    {
                        theQuestion.Practice_Problem = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                        theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                    }
                    else//(Practice_Problem == null) This is an Answer
                    {
                        theQuestion.Answer = new BinaryReader(hpf.InputStream).ReadBytes((int)hpf.InputStream.Length);
                        theQuestion.Practice_Problem_Answer = model.PracticeProblemAnswer;
                    }

                    //Insert the new Question into the database
                    db.Questions.InsertOnSubmit(theQuestion);
                }

                db.SubmitChanges();//Commit the changes to the database.
            }
            if(User.Identity.Name.Equals("administrator")){}
            return View("Upload", r);
        }