Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: insert/update novel/chapter logic #1019

Merged
merged 5 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/database/queries/ChapterQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const insertChapters = async (
index,
],
(txObj, { insertId }) => {
if (!insertId) {
if (!insertId || insertId < 0) {
tx.executeSql(
`
UPDATE Chapter SET
Expand Down
20 changes: 11 additions & 9 deletions src/database/queries/NovelQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,19 @@ export const insertNovelAndChapters = async (
const promises = [insertChapters(novelId, sourceNovel.chapters)];
if (sourceNovel.cover) {
const novelDir = NovelDownloadFolder + '/' + pluginId + '/' + novelId;
await RNFS.mkdir(novelDir);
const novelCoverUri = 'file://' + novelDir + '/cover.png';
promises.push(
fetchImage(pluginId, sourceNovel.cover).then(base64 => {
if (base64) {
RNFS.mkdir(novelDir)
.then(() => RNFS.writeFile(novelCoverUri, base64, 'base64'))
.then(() => {
db.transaction(tx => {
tx.executeSql('UPDATE Novel SET cover = ? WHERE id = ?', [
novelCoverUri,
novelId,
]);
});
RNFS.writeFile(novelCoverUri, base64, 'base64').then(() => {
db.transaction(tx => {
tx.executeSql('UPDATE Novel SET cover = ? WHERE id = ?', [
novelCoverUri,
novelId,
]);
});
});
}
}),
);
Expand Down Expand Up @@ -274,6 +273,9 @@ export const pickCustomNovelCover = async (novel: NovelInfo) => {
const novelDir =
NovelDownloadFolder + '/' + novel.pluginId + '/' + novel.id;
let novelCoverUri = 'file://' + novelDir + '/cover.png';
if (!(await RNFS.exists(novelDir))) {
await RNFS.mkdir(novelDir);
}
RNFS.copyFile(image.assets[0].uri, novelCoverUri);
novelCoverUri += '?' + Date.now();
db.transaction(tx => {
Expand Down
1 change: 1 addition & 0 deletions src/screens/novel/NovelScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ const Novel = ({ route, navigation }: NovelScreenProps) => {
})
.then(() => getNovel())
.then(() => showToast(getString('novelScreen.updatedToast', { name })))
.catch(error => showToast(error.message))
.finally(() => setUpdating(false));
}
};
Expand Down
23 changes: 11 additions & 12 deletions src/services/epub/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,30 @@ const insertLocalNovel = (
Novel(name, path, pluginId, inLibrary, isLocal)
VALUES(?, ?, 'local', 1, 1)`,
[name, path],
async (txObj, resultSet) => {
if (resultSet.insertId) {
await updateNovelCategoryById(resultSet.insertId, [2]);
const novelDir =
NovelDownloadFolder + '/local/' + resultSet.insertId;
async (txObj, { insertId }) => {
if (insertId && insertId >= 0) {
await updateNovelCategoryById(insertId, [2]);
const novelDir = NovelDownloadFolder + '/local/' + insertId;
await RNFS.mkdir(novelDir);
const newCoverPath =
'file://' + novelDir + '/' + cover?.split(/[\/\\]/).pop();
if (cover && (await RNFS.exists(cover))) {
await RNFS.moveFile(cover, newCoverPath);
}
await updateNovelInfo({
id: resultSet.insertId,
id: insertId,
pluginId: LOCAL_PLUGIN_ID,
author: author,
artist: artist,
summary: summary,
path: NovelDownloadFolder + '/local/' + resultSet.insertId,
path: NovelDownloadFolder + '/local/' + insertId,
cover: newCoverPath,
name: name,
inLibrary: true,
isLocal: true,
totalPages: 0,
});
resolve(resultSet.insertId);
resolve(insertId);
} else {
reject(
new Error(getString('advancedSettingsScreen.novelInsertFailed')),
Expand Down Expand Up @@ -100,8 +99,8 @@ const insertLocalChapter = (
releaseTime,
fakeId,
],
async (txObj, resultSet) => {
if (resultSet.insertId) {
async (txObj, { insertId }) => {
if (insertId && insertId >= 0) {
let chapterText: string = '';
try {
path = decodeURI(path);
Expand All @@ -128,9 +127,9 @@ const insertLocalChapter = (
?.pop()}"`;
},
);
await RNFS.mkdir(novelDir + '/' + resultSet.insertId);
await RNFS.mkdir(novelDir + '/' + insertId);
await TextFile.writeFile(
novelDir + '/' + resultSet.insertId + '/index.html',
novelDir + '/' + insertId + '/index.html',
chapterText,
);
resolve(staticPaths);
Expand Down
42 changes: 30 additions & 12 deletions src/services/updates/LibraryUpdateQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ const updateNovelMetadata = (
});
};

const updateNovelTotalPages = (novelId: number, totalPages: number) => {
return new Promise((resolve, reject) => {
db.transaction(tx => {
tx.executeSql(
'UPDATE SET Novel totalPages = ? WHERE id = ?',
[totalPages, novelId],
() => resolve(null),
(txObj, error) => {
reject(error);
return false;
},
);
});
});
};

const updateNovelChapters = (
pluginId: string,
novelId: number,
Expand All @@ -73,36 +89,39 @@ const updateNovelChapters = (
) => {
return new Promise((resolve, reject) => {
db.transaction(async tx => {
const newChapters: { id: number; path: string }[] = [];
for (let position = 0; position < novel.chapters.length; position++) {
const { name, path, releaseTime, page } = novel.chapters[position];
const { name, path, releaseTime, page, chapterNumber } =
novel.chapters[position];
tx.executeSql(
`
INSERT INTO Chapter (path, name, releaseTime, novelId, updatedTime, page, position)
SELECT ?, ?, ?, ?, datetime('now','localtime'), ?, ?
INSERT INTO Chapter (path, name, releaseTime, novelId, updatedTime, chapterNumber, page, position)
SELECT ?, ?, ?, ?, datetime('now','localtime'), ?, ?, ?
WHERE NOT EXISTS (SELECT id FROM Chapter WHERE path = ? AND novelId = ?);
`,
[
path,
name,
releaseTime || null,
novelId,
chapterNumber || null,
page || '1',
position,
path,
novelId,
],
(txObj, { insertId }) => {
if (insertId) {
if (insertId && insertId >= 0) {
if (downloadNewChapters) {
newChapters.push({ id: insertId, path: path });
downloadChapter(pluginId, novelId, insertId, path).catch(
reject,
);
}
} else {
tx.executeSql(
`
UPDATE Chapter SET
name = ?, releaseTime = ?, updatedTime = datetime('now','localtime'), page = ?, position = ?
WHERE path = ? AND novelId = ? AND (name != ? OR releaseTime != ? OR page != ?);
WHERE path = ? AND novelId = ? AND (name != ? OR releaseTime != ? OR page != ? OR position != ?);
`,
[
name,
Expand All @@ -114,6 +133,7 @@ const updateNovelChapters = (
name,
releaseTime || null,
page || '1',
position,
],
undefined,
(txObj, error) => {
Expand All @@ -129,11 +149,6 @@ const updateNovelChapters = (
},
);
}
if (downloadNewChapters) {
for (const { id, path } of newChapters) {
await downloadChapter(pluginId, novelId, id, path).catch(reject);
}
}
resolve(null);
});
});
Expand All @@ -157,6 +172,9 @@ const updateNovel = async (
const novel = await fetchNovel(pluginId, novelPath);
if (refreshNovelMetadata) {
await updateNovelMetadata(pluginId, novelId, novel);
} else if (novel.totalPages) {
// at least update totalPages,
await updateNovelTotalPages(novelId, novel.totalPages);
}
await updateNovelChapters(pluginId, novelId, novel, downloadNewChapters);
const latestChapterKey = `${NOVEL_LATEST_CHAPTER_PREFIX}_${novelId}`;
Expand Down
Loading