Example #1
0
 private bool IsFB2ParserValid()
 {
     try
     {
         fb2Parser = new FB2Parser(fb2FileName);
     }
     catch
     {
         ApplicationLogger.WriteStringToError("File " + fb2FileName + " is corrupted.");
         throw new Exception("File " + fb2FileName + " is corrupted.");
     }
     if (!string.IsNullOrEmpty(fb2Parser.BookText))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #2
0
        /// <summary>
        /// Scan zip file
        /// </summary>
        public void Scan()
        {
            Status = FileScannerStatus.SCANNING;
            ZipFile      zipFile       = null;
            string       entryFileName = string.Empty;
            MemoryStream memStream     = null;

            try
            {
                zipFile = new ZipFile(ZipFileName);

                foreach (ZipEntry entry in zipFile.Entries)
                {
                    if (Status != FileScannerStatus.SCANNING)
                    {
                        break;
                    }

                    if (!string.IsNullOrEmpty(entry.FileName))
                    {
                        entryFileName = entry.FileName;

                        // Process accepted files
                        try
                        {
                            Book book = null;
                            memStream = new MemoryStream();

                            string ext = Path.GetExtension(entry.FileName).ToLower();

                            if (Library.Contains(ZipFileName.Substring(Library.LibraryPath.Length + 1) + "@" + entryFileName))
                            {
                                SkippedFiles++;
                                if (OnFileSkipped != null)
                                {
                                    OnFileSkipped(this, new FileSkippedEventArgs(SkippedFiles));
                                }
                            }
                            else if (ext.Contains(".epub"))
                            {
                                entry.Extract(memStream);
                                book = new ePubParser().Parse(memStream, ZipFileName + "@" + entryFileName);
                            }
                            else if (ext.Contains(".fb2"))
                            {
                                entry.Extract(memStream);
                                book = new FB2Parser().Parse(memStream, ZipFileName + "@" + entryFileName);
                            }

                            if (book != null)
                            {
                                if (book.IsValid && OnBookFound != null)
                                {
                                    OnBookFound(this, new BookFoundEventArgs(book));
                                }
                                else if (!book.IsValid && OnInvalidBook != null)
                                {
                                    OnInvalidBook(this, new InvalidBookEventArgs(ZipFileName + "@" + entryFileName));
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.WriteLine(LogLevel.Error, ".ScanDirectory: exception {0} on file: {1}", e.Message, ZipFileName + "@" + entryFileName);
                            if (OnInvalidBook != null)
                            {
                                OnInvalidBook(this, new InvalidBookEventArgs(ZipFileName + "@" + entryFileName));
                            }
                        }
                        finally
                        {
                            if (memStream != null)
                            {
                                memStream.Dispose();
                                memStream = null;
                            }
                        }
                    }
                }
            }
            finally
            {
                if (zipFile != null)
                {
                    zipFile.Dispose();
                    zipFile = null;
                }
            }
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        public void ScanFile(string fullName)
        {
            Book   book = null;
            string ext  = Path.GetExtension(fullName).ToLower();

            // Process accepted files
            try
            {
                if (LibraryFactory.GetLibrary().Contains(fullName.Substring(LibraryFactory.GetLibrary().LibraryPath.Length + 1)))
                {
                    SkippedFiles++;
                    if (OnFileSkipped != null)
                    {
                        OnFileSkipped(this, new FileSkippedEventArgs(SkippedFiles));
                    }
                }
                else if (ext.Contains(".epub"))
                {
                    book = new ePubParser().Parse(fullName);
                }
                else if (ext.Contains(".fb2"))
                {
                    book = new FB2Parser().Parse(fullName);
                }
                else if (ext.Contains(".zip"))
                {
                    _zipScanner              = new ZipScanner(fullName);
                    _zipScanner.OnBookFound += (object sender, BookFoundEventArgs e) => { if (OnBookFound != null)
                                                                                          {
                                                                                              OnBookFound(sender, e);
                                                                                          }
                    };
                    _zipScanner.OnInvalidBook += (object sender, InvalidBookEventArgs e) => { if (OnInvalidBook != null)
                                                                                              {
                                                                                                  OnInvalidBook(sender, e);
                                                                                              }
                    };
                    _zipScanner.OnFileSkipped += (object sender, FileSkippedEventArgs e) =>
                    {
                        SkippedFiles++;
                        if (OnFileSkipped != null)
                        {
                            OnFileSkipped(sender, new FileSkippedEventArgs(SkippedFiles));
                        }
                    };
                    _zipScanner.Scan();
                }

                // Inform caller
                if (book != null)
                {
                    if (book.IsValid && OnBookFound != null)
                    {
                        OnBookFound(this, new BookFoundEventArgs(book));
                    }
                    else if (!book.IsValid && OnInvalidBook != null)
                    {
                        OnInvalidBook(this, new InvalidBookEventArgs(fullName));
                    }
                }
            }
            catch (Exception e)
            {
                Log.WriteLine(LogLevel.Error, ".ScanFile: exception {0} on file: {1}", e.Message, fullName);
                if (OnInvalidBook != null)
                {
                    OnInvalidBook(this, new InvalidBookEventArgs(fullName));
                }
            }
        }