generated from tpl/obsidian-sample-plugin
Small fixes
This commit is contained in:
parent
76f180aa58
commit
09d1b08f7d
21
src/main.ts
21
src/main.ts
|
@ -1,4 +1,4 @@
|
|||
import { Notice, Plugin, requestUrl } from "obsidian";
|
||||
import { Notice, Plugin, requestUrl, TFile } from "obsidian";
|
||||
import {
|
||||
type BookTrackerPluginSettings,
|
||||
DEFAULT_SETTINGS,
|
||||
|
@ -99,8 +99,9 @@ export default class BookTrackerPlugin extends Plugin {
|
|||
|
||||
async downloadCoverImage(
|
||||
coverImageUrl: string,
|
||||
fileName: string
|
||||
): Promise<string> {
|
||||
fileName: string,
|
||||
overwrite?: boolean
|
||||
): Promise<TFile> {
|
||||
const response = await requestUrl(coverImageUrl);
|
||||
const contentType = response.headers["content-type"];
|
||||
const extension = CONTENT_TYPE_EXTENSIONS[contentType || ""] || "";
|
||||
|
@ -121,20 +122,21 @@ export default class BookTrackerPlugin extends Plugin {
|
|||
|
||||
const existingFile = this.app.vault.getFileByPath(filePath);
|
||||
if (existingFile) {
|
||||
if (this.settings.overwriteExistingCovers) {
|
||||
if (this.settings.overwriteExistingCovers || overwrite) {
|
||||
await this.app.vault.modifyBinary(
|
||||
existingFile,
|
||||
response.arrayBuffer
|
||||
);
|
||||
} else {
|
||||
new Notice("Cover image already exists: " + filePath);
|
||||
return filePath;
|
||||
}
|
||||
return existingFile;
|
||||
}
|
||||
|
||||
await this.app.vault.createBinary(filePath, response.arrayBuffer);
|
||||
|
||||
return filePath;
|
||||
return await this.app.vault.createBinary(
|
||||
filePath,
|
||||
response.arrayBuffer
|
||||
);
|
||||
}
|
||||
|
||||
async createEntry(book: Book): Promise<void> {
|
||||
|
@ -149,10 +151,11 @@ export default class BookTrackerPlugin extends Plugin {
|
|||
const data: Record<string, unknown> = { book };
|
||||
|
||||
if (this.settings.downloadCovers && book.coverImageUrl) {
|
||||
data.coverImagePath = await this.downloadCoverImage(
|
||||
const coverImageFile = await this.downloadCoverImage(
|
||||
book.coverImageUrl,
|
||||
fileName
|
||||
);
|
||||
data.coverImagePath = coverImageFile.path;
|
||||
}
|
||||
|
||||
const renderedContent = await this.templater.renderTemplateFile(
|
||||
|
|
Loading…
Reference in New Issue