mirror of
https://github.com/fantasy-ke/obsidian-cf-imgbed.git
synced 2026-07-22 06:43:08 +00:00
refactor: Update settings tab and upload service for improved type safety and functionality
- Change the plugin type in settings tab to use CFImageBedPlugin for better clarity. - Enhance upload service by adding TFile and Vault imports for improved type handling. - Simplify error messages in upload service for better debugging. - Ensure proper type checking for existing files in the vault during backup operations.
This commit is contained in:
parent
499294742d
commit
39ff979c1b
2 changed files with 8 additions and 8 deletions
|
|
@ -1,10 +1,10 @@
|
|||
import { App, Plugin, PluginSettingTab, Setting } from 'obsidian';
|
||||
import { CFImageBedSettings } from '../types';
|
||||
import { App, PluginSettingTab, Setting } from 'obsidian';
|
||||
import CFImageBedPlugin from '../../main';
|
||||
|
||||
export class CFImageBedSettingTab extends PluginSettingTab {
|
||||
plugin: Plugin;
|
||||
plugin: CFImageBedPlugin;
|
||||
|
||||
constructor(app: App, plugin: Plugin) {
|
||||
constructor(app: App, plugin: CFImageBedPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { Notice, normalizePath, requestUrl } from 'obsidian';
|
||||
import { Notice, normalizePath, requestUrl, TFile, Vault } from 'obsidian';
|
||||
import { CFImageBedSettings } from '../types';
|
||||
import { ClientCompressor } from '../utils/clientCompressor';
|
||||
import { ClientWatermark } from '../utils/clientWatermark';
|
||||
|
|
@ -119,7 +119,7 @@ export class UploadService {
|
|||
});
|
||||
|
||||
if (response.status !== 200) {
|
||||
throw new Error(`Upload failed: ${response.status} ${response.statusText}`);
|
||||
throw new Error(`Upload failed: ${response.status}`);
|
||||
}
|
||||
|
||||
const result = response.json;
|
||||
|
|
@ -156,7 +156,7 @@ export class UploadService {
|
|||
|
||||
private async saveLocalBackup(file: File, backupPath: string): Promise<void> {
|
||||
// Obsidian 的 app 对象在此不可直接访问;通过 window.app 使用
|
||||
const app = (window as { app?: { vault?: { createFolder: (path: string) => Promise<void>; getAbstractFileByPath: (path: string) => { modifyBinary: (file: { path: string }, data: ArrayBuffer) => Promise<void> } | null; createBinary: (path: string, data: ArrayBuffer) => Promise<void> } } }).app;
|
||||
const app = (window as { app?: { vault?: Vault } }).app;
|
||||
if (!app?.vault) throw new Error('Cannot access Obsidian vault');
|
||||
const normalized = normalizePath(backupPath);
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
|
|
@ -169,7 +169,7 @@ export class UploadService {
|
|||
const targetFilePath = normalizePath(`${normalized}/${file.name}`);
|
||||
// 如果存在则覆盖
|
||||
const existing = app.vault.getAbstractFileByPath(targetFilePath);
|
||||
if (existing) {
|
||||
if (existing && existing instanceof TFile) {
|
||||
await app.vault.modifyBinary(existing, arrayBuffer);
|
||||
} else {
|
||||
await app.vault.createBinary(targetFilePath, arrayBuffer);
|
||||
|
|
|
|||
Loading…
Reference in a new issue