Skip to content

Commit 84a934f

Browse files
committed
Download image
1 parent bff39cb commit 84a934f

File tree

2 files changed

+114
-5
lines changed

2 files changed

+114
-5
lines changed

src/cli/commands/apply-blueprint.js

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import GLib from 'gi://GLib';
2+
import Gio from 'gi://Gio';
13
import {ConfigWriter} from '../../utils/ConfigWriter.js';
24
import {BlueprintService} from '../../services/BlueprintService.js';
35
import {ColorMapper} from '../utils/color-mapper.js';
6+
import {ensureDirectoryExists} from '../../utils/file-utils.js';
47

58
/**
69
* Command handler for applying a blueprint theme
@@ -11,9 +14,9 @@ export class ApplyBlueprintCommand {
1114
* Executes the apply-blueprint command
1215
*
1316
* @param {string} name - Name of the blueprint to apply
14-
* @returns {boolean} True if successful, false otherwise
17+
* @returns {Promise<boolean>} True if successful, false otherwise
1518
*/
16-
static execute(name) {
19+
static async execute(name) {
1720
if (!name) {
1821
print('Error: Blueprint name is required');
1922
print('Usage: aether --apply-blueprint <name>');
@@ -47,6 +50,16 @@ export class ApplyBlueprintCommand {
4750
}
4851

4952
const palette = foundBlueprint.palette;
53+
54+
// Download wallpaper if wallpaperUrl is present and wallpaper is missing
55+
let wallpaperPath = palette.wallpaper;
56+
if (palette.wallpaperUrl && !wallpaperPath) {
57+
wallpaperPath = await this._downloadWallpaper(palette.wallpaperUrl);
58+
if (!wallpaperPath) {
59+
print('Warning: Failed to download wallpaper, continuing without it');
60+
}
61+
}
62+
5063
const colorRoles = ColorMapper.mapColorsToRoles(palette.colors);
5164

5265
const settings = foundBlueprint.settings || {};
@@ -56,7 +69,7 @@ export class ApplyBlueprintCommand {
5669
const configWriter = new ConfigWriter();
5770
configWriter.applyTheme(
5871
colorRoles,
59-
palette.wallpaper,
72+
wallpaperPath,
6073
settings,
6174
lightMode,
6275
appOverrides,
@@ -71,4 +84,48 @@ export class ApplyBlueprintCommand {
7184
return false;
7285
}
7386
}
87+
88+
/**
89+
* Downloads a wallpaper from a URL
90+
* @param {string} url - URL to download from
91+
* @returns {Promise<string|null>} Path to downloaded wallpaper or null on failure
92+
* @private
93+
*/
94+
static async _downloadWallpaper(url) {
95+
try {
96+
print(`Downloading wallpaper from: ${url}`);
97+
98+
const wallpapersDir = GLib.build_filenamev([
99+
GLib.get_user_data_dir(),
100+
'aether',
101+
'wallpapers',
102+
]);
103+
ensureDirectoryExists(wallpapersDir);
104+
105+
// Extract filename from URL
106+
const urlParts = url.split('/');
107+
const filename = urlParts[urlParts.length - 1] || 'imported-wallpaper.jpg';
108+
const wallpaperPath = GLib.build_filenamev([
109+
wallpapersDir,
110+
filename,
111+
]);
112+
113+
// Check if already downloaded
114+
const file = Gio.File.new_for_path(wallpaperPath);
115+
if (file.query_exists(null)) {
116+
print(`Wallpaper already downloaded: ${wallpaperPath}`);
117+
return wallpaperPath;
118+
}
119+
120+
// Download wallpaper
121+
const {wallhavenService} = await import('../../services/wallhaven-service.js');
122+
await wallhavenService.downloadWallpaper(url, wallpaperPath);
123+
124+
print(`✓ Wallpaper downloaded: ${wallpaperPath}`);
125+
return wallpaperPath;
126+
} catch (error) {
127+
print(`Error downloading wallpaper: ${error.message}`);
128+
return null;
129+
}
130+
}
74131
}

src/components/PaletteEditor.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import GLib from 'gi://GLib';
2+
import Gio from 'gi://Gio';
23
import GObject from 'gi://GObject';
34
import Gtk from 'gi://Gtk?version=4.0';
45
import Adw from 'gi://Adw?version=1';
56

67
import {extractColorsFromWallpaperIM} from '../utils/imagemagick-color-extraction.js';
78
import {adjustColor} from '../utils/color-utils.js';
9+
import {ensureDirectoryExists} from '../utils/file-utils.js';
810
import {WallpaperBrowser} from './WallpaperBrowser.js';
911
import {LocalWallpaperBrowser} from './LocalWallpaperBrowser.js';
1012
import {FavoritesView} from './FavoritesView.js';
@@ -438,14 +440,64 @@ export const PaletteEditor = GObject.registerClass(
438440
this.switchToEditorTab();
439441
}
440442

441-
loadBlueprintPalette(palette) {
443+
async loadBlueprintPalette(palette) {
442444
if (palette.colors) {
443445
this._originalPalette = [...palette.colors];
444446
this.setPalette(palette.colors);
445447
}
446448

447-
if (palette.wallpaper) {
449+
// Download wallpaper if wallpaperUrl is present
450+
if (palette.wallpaperUrl && !palette.wallpaper) {
451+
try {
452+
const wallpapersDir = GLib.build_filenamev([
453+
GLib.get_user_data_dir(),
454+
'aether',
455+
'wallpapers',
456+
]);
457+
ensureDirectoryExists(wallpapersDir);
458+
459+
// Extract filename from URL
460+
const urlParts = palette.wallpaperUrl.split('/');
461+
const filename = urlParts[urlParts.length - 1] || 'imported-wallpaper.jpg';
462+
const wallpaperPath = GLib.build_filenamev([
463+
wallpapersDir,
464+
filename,
465+
]);
466+
467+
// Check if already downloaded
468+
const file = Gio.File.new_for_path(wallpaperPath);
469+
if (!file.query_exists(null)) {
470+
// Download wallpaper
471+
const {wallhavenService} = await import('../services/wallhaven-service.js');
472+
await wallhavenService.downloadWallpaper(
473+
palette.wallpaperUrl,
474+
wallpaperPath
475+
);
476+
console.log(`Downloaded wallpaper from blueprint: ${wallpaperPath}`);
477+
}
478+
479+
// Load the downloaded wallpaper
480+
this.loadWallpaperWithoutExtraction(wallpaperPath);
481+
482+
// Store metadata
483+
this._wallpaperMetadata = {
484+
url: palette.wallpaperUrl,
485+
source: palette.wallpaperSource || 'wallhaven',
486+
};
487+
} catch (error) {
488+
console.error('Failed to download wallpaper from URL:', error);
489+
// Continue without wallpaper
490+
}
491+
} else if (palette.wallpaper) {
448492
this.loadWallpaperWithoutExtraction(palette.wallpaper);
493+
494+
// Restore wallpaper metadata if available
495+
if (palette.wallpaperUrl) {
496+
this._wallpaperMetadata = {
497+
url: palette.wallpaperUrl,
498+
source: palette.wallpaperSource || 'wallhaven',
499+
};
500+
}
449501
}
450502

451503
// Always reset additional images when loading blueprint

0 commit comments

Comments
 (0)