調整 db 並加入 db 升級功能
This commit is contained in:
@@ -6,6 +6,7 @@ using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -75,7 +76,7 @@ namespace EHDownloader
|
||||
|
||||
public int PageCount => Pages.Count;
|
||||
|
||||
public BookStates BookState { get; set; } = BookStates.Wait;
|
||||
public BookStates State { get; set; } = BookStates.Wait;
|
||||
|
||||
|
||||
public Book(string url, string parentFolder)
|
||||
@@ -105,7 +106,7 @@ namespace EHDownloader
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
BookState = BookStates.LoadFailed;
|
||||
State = BookStates.LoadFailed;
|
||||
return FetchBookResults.LoadPageFailed;
|
||||
}
|
||||
|
||||
@@ -115,7 +116,7 @@ namespace EHDownloader
|
||||
}
|
||||
catch
|
||||
{
|
||||
BookState = BookStates.FetchTitleFailed;
|
||||
State = BookStates.FetchTitleFailed;
|
||||
return FetchBookResults.FetchTitleFailed;
|
||||
}
|
||||
|
||||
@@ -125,7 +126,7 @@ namespace EHDownloader
|
||||
var htmlPageSets = doc.DocumentNode.SelectNodes(PageSetXPath);
|
||||
if (htmlPageSets == null)
|
||||
{
|
||||
BookState = BookStates.FetchPageSetFailed;
|
||||
State = BookStates.FetchPageSetFailed;
|
||||
return FetchBookResults.FetchPageSetFailed;
|
||||
}
|
||||
foreach (var htmlPageSet in htmlPageSets)
|
||||
@@ -142,7 +143,7 @@ namespace EHDownloader
|
||||
}
|
||||
catch
|
||||
{
|
||||
BookState = BookStates.FetchPageSetFailed;
|
||||
State = BookStates.FetchPageSetFailed;
|
||||
return FetchBookResults.FetchTitleFailed;
|
||||
}
|
||||
|
||||
@@ -170,7 +171,7 @@ namespace EHDownloader
|
||||
}
|
||||
catch
|
||||
{
|
||||
BookState = BookStates.FetchPageLinkFailed;
|
||||
State = BookStates.FetchPageLinkFailed;
|
||||
return FetchBookResults.FetchPageLinkFailed;
|
||||
}
|
||||
|
||||
@@ -180,7 +181,7 @@ namespace EHDownloader
|
||||
Pages[i].PageNumber = i + 1;
|
||||
}
|
||||
|
||||
BookState = BookStates.FetchCompleted;
|
||||
State = BookStates.FetchCompleted;
|
||||
return FetchBookResults.Completed;
|
||||
}
|
||||
|
||||
@@ -218,15 +219,16 @@ namespace EHDownloader
|
||||
|
||||
if (Pages.All(p => p.Result == DownloadPageResults.Completed))
|
||||
{
|
||||
BookState = BookStates.DownloadCompleted;
|
||||
State = BookStates.DownloadCompleted;
|
||||
return;
|
||||
}
|
||||
}
|
||||
BookState = BookStates.DownloadFinishedButSomePageError;
|
||||
State = BookStates.DownloadFinishedButSomePageError;
|
||||
}
|
||||
|
||||
private string GetFolderName()
|
||||
private string getFolderName()
|
||||
{
|
||||
if (Title == null) return null;
|
||||
string folderName = Title;
|
||||
foreach (var ch in System.IO.Path.GetInvalidFileNameChars())
|
||||
folderName = folderName.Replace(ch, ' ');
|
||||
@@ -251,7 +253,7 @@ namespace EHDownloader
|
||||
public async Task CompressAsync()
|
||||
{
|
||||
string folderPath = getFolderPath();
|
||||
if (BookState != BookStates.DownloadCompleted)
|
||||
if (State != BookStates.DownloadCompleted)
|
||||
return;
|
||||
if (!System.IO.Directory.Exists(ParentFolder))
|
||||
return;
|
||||
@@ -271,12 +273,19 @@ namespace EHDownloader
|
||||
System.IO.Directory.Delete(folderPath, true);
|
||||
}
|
||||
|
||||
BookState = BookStates.Compressed;
|
||||
State = BookStates.Compressed;
|
||||
}
|
||||
|
||||
private string getFolderPath()
|
||||
{
|
||||
return System.IO.Path.Combine(ParentFolder, GetFolderName());
|
||||
string nane = getFolderName();
|
||||
if (nane == null) return null;
|
||||
return System.IO.Path.Combine(ParentFolder, nane);
|
||||
}
|
||||
|
||||
public string Path
|
||||
{
|
||||
get => getFolderPath();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Policy;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -13,23 +14,82 @@ namespace EHDownloader
|
||||
{
|
||||
public DbContext()
|
||||
{
|
||||
if (File.Exists(Settings.GetDbPath())) return;
|
||||
if (File.Exists(Settings.GetDbPath()))
|
||||
{
|
||||
checkDB();
|
||||
}
|
||||
else
|
||||
{
|
||||
createNewDB();
|
||||
}
|
||||
}
|
||||
|
||||
private void checkDB()
|
||||
{
|
||||
int DBVer = getDBSchemaVersion();
|
||||
if(DBVer == 0)
|
||||
{
|
||||
updateDBSchema(DBVer);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateDBSchema(int fromVer)
|
||||
{
|
||||
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
||||
{
|
||||
switch (fromVer)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
// add App table and ver=1
|
||||
cn.Execute(@"
|
||||
CREATE TABLE App (DBSchemaVersion INTEGER NOT NULL);
|
||||
INSERT INTO App (DBSchemaVersion) VALUES (1);
|
||||
ALTER TABLE Book ADD COLUMN State INTEGER NOT NULL DEFAULT 3;
|
||||
ALTER TABLE Book ADD COLUMN Path TEXT;
|
||||
");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void createNewDB()
|
||||
{
|
||||
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
||||
{
|
||||
cn.Execute(@"
|
||||
CREATE TABLE Book (
|
||||
Url VARCHAR(256),
|
||||
Title VARNCHAR(256),
|
||||
PageCount smallint,
|
||||
DownloadDateTime datetime,
|
||||
CONSTRAINT Book_PK PRIMARY KEY (Url)
|
||||
)");
|
||||
|
||||
CREATE TABLE ""Book"" (
|
||||
""Url"" VARCHAR(256),
|
||||
""Title"" VARNCHAR(256),
|
||||
""PageCount"" smallint,
|
||||
""DownloadDateTime"" datetime,
|
||||
""State"" INTEGER NOT NULL,
|
||||
""Path"" TEXT,
|
||||
CONSTRAINT ""Book_PK"" PRIMARY KEY(""Url"")
|
||||
);
|
||||
CREATE TABLE ""App"" (
|
||||
""DBSchemaVersion"" INTEGER NOT NULL
|
||||
);
|
||||
INSERT INTO App (DBSchemaVersion) VALUES (1);
|
||||
");
|
||||
}
|
||||
}
|
||||
|
||||
public bool PageIsExists(string url)
|
||||
private int getDBSchemaVersion()
|
||||
{
|
||||
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
||||
{
|
||||
// check App table is exists
|
||||
var query_table_exists = "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'App'";
|
||||
bool app_table_is_exists = cn.Query(query_table_exists).Any();
|
||||
if (!app_table_is_exists) return 0;
|
||||
var query = "select DBSchemaVersion from App";
|
||||
return cn.Query<int>(query).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
public bool BookIsExists(string url)
|
||||
{
|
||||
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
||||
{
|
||||
@@ -39,14 +99,24 @@ namespace EHDownloader
|
||||
}
|
||||
}
|
||||
|
||||
public void InsertPage(Book book)
|
||||
public void InsertBook(Book book)
|
||||
{
|
||||
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
||||
{
|
||||
var insertScript =
|
||||
"INSERT INTO Book VALUES (@Url,@Title,@PageCount,@DownloadDateTime)";
|
||||
"INSERT INTO Book VALUES (@Url,@Title,@PageCount,@DownloadDateTime,@State,@Path)";
|
||||
cn.Execute(insertScript, book);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateBookStatus(Book book)
|
||||
{
|
||||
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
||||
{
|
||||
var updateScript =
|
||||
"UPDATE Book SET (State=@State) WHERE Url=@Url";
|
||||
cn.Execute(updateScript, book);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,4 +114,10 @@
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\packages\System.Data.SQLite.Core.1.0.111.0\build\net46\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Data.SQLite.Core.1.0.111.0\build\net46\System.Data.SQLite.Core.targets'))" />
|
||||
</Target>
|
||||
<PropertyGroup>
|
||||
<ContentSQLiteInteropFiles>true</ContentSQLiteInteropFiles>
|
||||
<CopySQLiteInteropFiles>false</CopySQLiteInteropFiles>
|
||||
<CleanSQLiteInteropFiles>false</CleanSQLiteInteropFiles>
|
||||
<CollectSQLiteInteropFiles>false</CollectSQLiteInteropFiles>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -106,7 +106,7 @@ namespace EHDownloader
|
||||
{
|
||||
if (!isEhUrl(url)) return;
|
||||
if (lv_Tasks.Items.ContainsKey(url)) return;
|
||||
if (_dbContext.PageIsExists(url)) return;
|
||||
if (_dbContext.BookIsExists(url)) return;
|
||||
ListViewItem lvi = new ListViewItem(url) { Name = url };
|
||||
lvi.SubItems.Add("Wait");
|
||||
|
||||
@@ -135,8 +135,8 @@ namespace EHDownloader
|
||||
while (running)
|
||||
{
|
||||
|
||||
lvi.SubItems[1].Text = book.BookState.GetDescription();
|
||||
switch (book.BookState)
|
||||
lvi.SubItems[1].Text = book.State.GetDescription();
|
||||
switch (book.State)
|
||||
{
|
||||
case BookStates.Wait:
|
||||
await book.FetchAsync();
|
||||
@@ -147,7 +147,7 @@ namespace EHDownloader
|
||||
break;
|
||||
case BookStates.DownloadCompleted:
|
||||
await book.CompressAsync();
|
||||
_dbContext.InsertPage(book);
|
||||
_dbContext.InsertBook(book);
|
||||
goto default;
|
||||
case BookStates.Compressed:
|
||||
case BookStates.LoadFailed:
|
||||
@@ -178,7 +178,7 @@ namespace EHDownloader
|
||||
{
|
||||
var lvi = lv_Tasks.Items[i];
|
||||
var book = (Book)lvi.Tag;
|
||||
switch (book.BookState)
|
||||
switch (book.State)
|
||||
{
|
||||
case BookStates.Wait:
|
||||
case BookStates.FetchCompleted:
|
||||
@@ -190,10 +190,10 @@ namespace EHDownloader
|
||||
case BookStates.FetchTitleFailed:
|
||||
case BookStates.FetchPageSetFailed:
|
||||
case BookStates.FetchPageLinkFailed:
|
||||
book.BookState = BookStates.Wait;
|
||||
book.State = BookStates.Wait;
|
||||
continue;
|
||||
case BookStates.DownloadFinishedButSomePageError:
|
||||
book.BookState = BookStates.FetchCompleted;
|
||||
book.State = BookStates.FetchCompleted;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user