Small fixes

This commit is contained in:
Evan Fiordeliso 2025-07-01 10:36:26 -04:00
parent 76f180aa58
commit 09d1b08f7d
1 changed files with 12 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import { Notice, Plugin, requestUrl } from "obsidian"; import { Notice, Plugin, requestUrl, TFile } from "obsidian";
import { import {
type BookTrackerPluginSettings, type BookTrackerPluginSettings,
DEFAULT_SETTINGS, DEFAULT_SETTINGS,
@ -99,8 +99,9 @@ export default class BookTrackerPlugin extends Plugin {
async downloadCoverImage( async downloadCoverImage(
coverImageUrl: string, coverImageUrl: string,
fileName: string fileName: string,
): Promise<string> { overwrite?: boolean
): Promise<TFile> {
const response = await requestUrl(coverImageUrl); const response = await requestUrl(coverImageUrl);
const contentType = response.headers["content-type"]; const contentType = response.headers["content-type"];
const extension = CONTENT_TYPE_EXTENSIONS[contentType || ""] || ""; const extension = CONTENT_TYPE_EXTENSIONS[contentType || ""] || "";
@ -121,20 +122,21 @@ export default class BookTrackerPlugin extends Plugin {
const existingFile = this.app.vault.getFileByPath(filePath); const existingFile = this.app.vault.getFileByPath(filePath);
if (existingFile) { if (existingFile) {
if (this.settings.overwriteExistingCovers) { if (this.settings.overwriteExistingCovers || overwrite) {
await this.app.vault.modifyBinary( await this.app.vault.modifyBinary(
existingFile, existingFile,
response.arrayBuffer response.arrayBuffer
); );
} else { } else {
new Notice("Cover image already exists: " + filePath); new Notice("Cover image already exists: " + filePath);
return filePath;
} }
return existingFile;
} }
await this.app.vault.createBinary(filePath, response.arrayBuffer); return await this.app.vault.createBinary(
filePath,
return filePath; response.arrayBuffer
);
} }
async createEntry(book: Book): Promise<void> { async createEntry(book: Book): Promise<void> {
@ -149,10 +151,11 @@ export default class BookTrackerPlugin extends Plugin {
const data: Record<string, unknown> = { book }; const data: Record<string, unknown> = { book };
if (this.settings.downloadCovers && book.coverImageUrl) { if (this.settings.downloadCovers && book.coverImageUrl) {
data.coverImagePath = await this.downloadCoverImage( const coverImageFile = await this.downloadCoverImage(
book.coverImageUrl, book.coverImageUrl,
fileName fileName
); );
data.coverImagePath = coverImageFile.path;
} }
const renderedContent = await this.templater.renderTemplateFile( const renderedContent = await this.templater.renderTemplateFile(