Skip to content

Commit 657668d

Browse files
committed
Added download option for image urls
1 parent 0a65fd8 commit 657668d

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/main.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { MarkdownView, Notice, parseYaml, Plugin, stringifyYaml, TFile, TFolder } from 'obsidian';
2+
import { requestUrl, normalizePath } from 'obsidian'; // Add requestUrl import
23
import type { MediaType } from 'src/utils/MediaType';
34
import { APIManager } from './api/APIManager';
45
import { BoardGameGeekAPI } from './api/apis/BoardGameGeekAPI';
@@ -314,6 +315,31 @@ export default class MediaDbPlugin extends Plugin {
314315

315316
options.openNote = this.settings.openNoteInNewTab;
316317

318+
if (mediaTypeModel.image && typeof mediaTypeModel.image === 'string' && mediaTypeModel.image.startsWith('http')) {
319+
if (this.settings.imageDownload) {
320+
try {
321+
const imageurl = mediaTypeModel.image;
322+
const imageext = imageurl.split('.').pop()?.split(/\#|\?/)[0] || 'jpg';
323+
const imagefileName = `${replaceIllegalFileNameCharactersInString(`${mediaTypeModel.type}_${mediaTypeModel.title} (${mediaTypeModel.year})`)}.${imageext}`;
324+
const imagepath = normalizePath(`${this.settings.imageFolder}/${imagefileName}`);
325+
326+
if (!this.app.vault.getAbstractFileByPath(this.settings.imageFolder)) {
327+
await this.app.vault.createFolder(this.settings.imageFolder);
328+
}
329+
330+
const response = await requestUrl({ url: imageurl, method: 'GET' });
331+
await this.app.vault.createBinary(imagepath, response.arrayBuffer);
332+
333+
// Update model to use local image path
334+
mediaTypeModel.image = `[[${imagepath}]]`;
335+
} catch (e) {
336+
console.warn('MDB | Failed to download image:', e);
337+
}
338+
} else {
339+
mediaTypeModel.image = mediaTypeModel.image;
340+
}
341+
}
342+
317343
const fileContent = await this.generateMediaDbNoteContents(mediaTypeModel, options);
318344

319345
if (!options.folder) {

src/models/MediaTypeModel.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export abstract class MediaTypeModel {
99
dataSource: string;
1010
url: string;
1111
id: string;
12+
image?: string;
1213

1314
userData: object;
1415

@@ -21,6 +22,8 @@ export abstract class MediaTypeModel {
2122
this.dataSource = '';
2223
this.url = '';
2324
this.id = '';
25+
this.image = '';
26+
2427
this.userData = {};
2528
}
2629

src/settings/Settings.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export interface MediaDbPluginSettings {
6969
boardgameFolder: string;
7070
bookFolder: string;
7171

72+
imageDownload: boolean;
73+
imageFolder: string;
7274
propertyMappingModels: PropertyMappingModel[];
7375
}
7476

@@ -130,6 +132,8 @@ const DEFAULT_SETTINGS: MediaDbPluginSettings = {
130132
boardgameFolder: 'Media DB/boardgames',
131133
bookFolder: 'Media DB/books',
132134

135+
imageDownload: false,
136+
imageFolder: 'Media DB/images',
133137
propertyMappingModels: [],
134138
};
135139

@@ -298,6 +302,29 @@ export class MediaDbSettingTab extends PluginSettingTab {
298302
});
299303
});
300304

305+
new Setting(containerEl)
306+
.setName('Download images')
307+
.setDesc('Downloads images for new notes in the folder below')
308+
.addToggle(cb => {
309+
cb.setValue(this.plugin.settings.imageDownload).onChange(data => {
310+
this.plugin.settings.imageDownload = data;
311+
void this.plugin.saveSettings();
312+
});
313+
});
314+
315+
new Setting(containerEl)
316+
.setName('Image folder')
317+
.setDesc('Where downloaded images should be stored.')
318+
.addSearch(cb => {
319+
new FolderSuggest(this.app, cb.inputEl);
320+
cb.setPlaceholder(DEFAULT_SETTINGS.imageFolder)
321+
.setValue(this.plugin.settings.imageFolder)
322+
.onChange(data => {
323+
this.plugin.settings.imageFolder = data;
324+
void this.plugin.saveSettings();
325+
});
326+
});
327+
301328
// Create a map to store APIs for each media type
302329
const mediaTypeApiMap = new Map<MediaType, string[]>();
303330

0 commit comments

Comments
 (0)