調整 db 並加入 db 升級功能

This commit is contained in:
2024-10-01 15:27:54 +08:00
parent e79f9c140d
commit c899f5591e
4 changed files with 121 additions and 36 deletions

View File

@@ -6,6 +6,7 @@ using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -75,7 +76,7 @@ namespace EHDownloader
public int PageCount => Pages.Count; 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) public Book(string url, string parentFolder)
@@ -105,7 +106,7 @@ namespace EHDownloader
} }
catch (Exception ex) catch (Exception ex)
{ {
BookState = BookStates.LoadFailed; State = BookStates.LoadFailed;
return FetchBookResults.LoadPageFailed; return FetchBookResults.LoadPageFailed;
} }
@@ -115,7 +116,7 @@ namespace EHDownloader
} }
catch catch
{ {
BookState = BookStates.FetchTitleFailed; State = BookStates.FetchTitleFailed;
return FetchBookResults.FetchTitleFailed; return FetchBookResults.FetchTitleFailed;
} }
@@ -125,7 +126,7 @@ namespace EHDownloader
var htmlPageSets = doc.DocumentNode.SelectNodes(PageSetXPath); var htmlPageSets = doc.DocumentNode.SelectNodes(PageSetXPath);
if (htmlPageSets == null) if (htmlPageSets == null)
{ {
BookState = BookStates.FetchPageSetFailed; State = BookStates.FetchPageSetFailed;
return FetchBookResults.FetchPageSetFailed; return FetchBookResults.FetchPageSetFailed;
} }
foreach (var htmlPageSet in htmlPageSets) foreach (var htmlPageSet in htmlPageSets)
@@ -142,7 +143,7 @@ namespace EHDownloader
} }
catch catch
{ {
BookState = BookStates.FetchPageSetFailed; State = BookStates.FetchPageSetFailed;
return FetchBookResults.FetchTitleFailed; return FetchBookResults.FetchTitleFailed;
} }
@@ -170,7 +171,7 @@ namespace EHDownloader
} }
catch catch
{ {
BookState = BookStates.FetchPageLinkFailed; State = BookStates.FetchPageLinkFailed;
return FetchBookResults.FetchPageLinkFailed; return FetchBookResults.FetchPageLinkFailed;
} }
@@ -180,7 +181,7 @@ namespace EHDownloader
Pages[i].PageNumber = i + 1; Pages[i].PageNumber = i + 1;
} }
BookState = BookStates.FetchCompleted; State = BookStates.FetchCompleted;
return FetchBookResults.Completed; return FetchBookResults.Completed;
} }
@@ -218,15 +219,16 @@ namespace EHDownloader
if (Pages.All(p => p.Result == DownloadPageResults.Completed)) if (Pages.All(p => p.Result == DownloadPageResults.Completed))
{ {
BookState = BookStates.DownloadCompleted; State = BookStates.DownloadCompleted;
return; return;
} }
} }
BookState = BookStates.DownloadFinishedButSomePageError; State = BookStates.DownloadFinishedButSomePageError;
} }
private string GetFolderName() private string getFolderName()
{ {
if (Title == null) return null;
string folderName = Title; string folderName = Title;
foreach (var ch in System.IO.Path.GetInvalidFileNameChars()) foreach (var ch in System.IO.Path.GetInvalidFileNameChars())
folderName = folderName.Replace(ch, ' '); folderName = folderName.Replace(ch, ' ');
@@ -251,7 +253,7 @@ namespace EHDownloader
public async Task CompressAsync() public async Task CompressAsync()
{ {
string folderPath = getFolderPath(); string folderPath = getFolderPath();
if (BookState != BookStates.DownloadCompleted) if (State != BookStates.DownloadCompleted)
return; return;
if (!System.IO.Directory.Exists(ParentFolder)) if (!System.IO.Directory.Exists(ParentFolder))
return; return;
@@ -271,12 +273,19 @@ namespace EHDownloader
System.IO.Directory.Delete(folderPath, true); System.IO.Directory.Delete(folderPath, true);
} }
BookState = BookStates.Compressed; State = BookStates.Compressed;
} }
private string getFolderPath() 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();
} }
} }

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Data.SQLite; using System.Data.SQLite;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Security.Policy;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -13,23 +14,82 @@ namespace EHDownloader
{ {
public DbContext() public DbContext()
{ {
if (File.Exists(Settings.GetDbPath())) return; if (File.Exists(Settings.GetDbPath()))
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
{ {
cn.Execute(@" checkDB();
CREATE TABLE Book ( }
Url VARCHAR(256), else
Title VARNCHAR(256), {
PageCount smallint, createNewDB();
DownloadDateTime datetime,
CONSTRAINT Book_PK PRIMARY KEY (Url)
)");
} }
} }
public bool PageIsExists(string url) 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,
""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);
");
}
}
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())) 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())) using (var cn = new SQLiteConnection(Settings.GetConnectString()))
{ {
var insertScript = var insertScript =
"INSERT INTO Book VALUES (@Url,@Title,@PageCount,@DownloadDateTime)"; "INSERT INTO Book VALUES (@Url,@Title,@PageCount,@DownloadDateTime,@State,@Path)";
cn.Execute(insertScript, book); 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);
}
}
} }
} }

View File

@@ -114,4 +114,10 @@
</PropertyGroup> </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'))" /> <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> </Target>
<PropertyGroup>
<ContentSQLiteInteropFiles>true</ContentSQLiteInteropFiles>
<CopySQLiteInteropFiles>false</CopySQLiteInteropFiles>
<CleanSQLiteInteropFiles>false</CleanSQLiteInteropFiles>
<CollectSQLiteInteropFiles>false</CollectSQLiteInteropFiles>
</PropertyGroup>
</Project> </Project>

View File

@@ -106,7 +106,7 @@ namespace EHDownloader
{ {
if (!isEhUrl(url)) return; if (!isEhUrl(url)) return;
if (lv_Tasks.Items.ContainsKey(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 }; ListViewItem lvi = new ListViewItem(url) { Name = url };
lvi.SubItems.Add("Wait"); lvi.SubItems.Add("Wait");
@@ -135,8 +135,8 @@ namespace EHDownloader
while (running) while (running)
{ {
lvi.SubItems[1].Text = book.BookState.GetDescription(); lvi.SubItems[1].Text = book.State.GetDescription();
switch (book.BookState) switch (book.State)
{ {
case BookStates.Wait: case BookStates.Wait:
await book.FetchAsync(); await book.FetchAsync();
@@ -147,7 +147,7 @@ namespace EHDownloader
break; break;
case BookStates.DownloadCompleted: case BookStates.DownloadCompleted:
await book.CompressAsync(); await book.CompressAsync();
_dbContext.InsertPage(book); _dbContext.InsertBook(book);
goto default; goto default;
case BookStates.Compressed: case BookStates.Compressed:
case BookStates.LoadFailed: case BookStates.LoadFailed:
@@ -178,7 +178,7 @@ namespace EHDownloader
{ {
var lvi = lv_Tasks.Items[i]; var lvi = lv_Tasks.Items[i];
var book = (Book)lvi.Tag; var book = (Book)lvi.Tag;
switch (book.BookState) switch (book.State)
{ {
case BookStates.Wait: case BookStates.Wait:
case BookStates.FetchCompleted: case BookStates.FetchCompleted:
@@ -190,10 +190,10 @@ namespace EHDownloader
case BookStates.FetchTitleFailed: case BookStates.FetchTitleFailed:
case BookStates.FetchPageSetFailed: case BookStates.FetchPageSetFailed:
case BookStates.FetchPageLinkFailed: case BookStates.FetchPageLinkFailed:
book.BookState = BookStates.Wait; book.State = BookStates.Wait;
continue; continue;
case BookStates.DownloadFinishedButSomePageError: case BookStates.DownloadFinishedButSomePageError:
book.BookState = BookStates.FetchCompleted; book.State = BookStates.FetchCompleted;
continue; continue;
} }
} }