Harden wallpaper downloads

This commit is contained in:
Scott Idem
2026-05-13 18:43:58 -04:00
parent bb51771dc7
commit c8fdb8b1e7
3 changed files with 42 additions and 5 deletions

View File

@@ -132,12 +132,25 @@ function registerSystemHandlers() {
headers['x-account-id'] = account_id;
try {
const response = await (0, axios_1.default)({ method: 'get', url: image_url, responseType: 'stream', headers });
const content_type = (response.headers['content-type'] ?? '');
if (!content_type.startsWith('image/')) {
response.data.destroy();
return { success: false, error: `URL did not return an image (Content-Type: ${content_type})` };
}
const writer = fs.createWriteStream(dest_path);
response.data.pipe(writer);
await new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
const file_size = fs.statSync(dest_path).size;
if (file_size === 0) {
try {
fs.unlinkSync(dest_path);
}
catch { }
return { success: false, error: 'Wallpaper download incomplete (0 bytes)' };
}
return { success: true, path: dest_path };
}
catch (e) {
@@ -185,6 +198,12 @@ function registerSystemHandlers() {
return { success: false, error: result.error };
primary_path = result.path;
}
if (!primary_path && url_external && display === 'external') {
const ext_result = await download_wallpaper_image(url_external, 'wallpaper_external');
if (!ext_result.success || !ext_result.path)
return { success: false, error: ext_result.error };
return await apply_mac_wallpaper(ext_result.path, 'external');
}
if (!primary_path)
return { success: false, error: 'No image source provided' };
if (url_external) {
@@ -193,8 +212,9 @@ function registerSystemHandlers() {
if (!primary_result.success)
return primary_result;
const ext_result = await download_wallpaper_image(url_external, 'wallpaper_external');
const ext_path = ext_result.success && ext_result.path ? ext_result.path : primary_path;
return await apply_mac_wallpaper(ext_path, 'external');
if (!ext_result.success || !ext_result.path)
return { success: false, error: ext_result.error };
return await apply_mac_wallpaper(ext_result.path, 'external');
}
else {
return await apply_mac_wallpaper(primary_path, display);

File diff suppressed because one or more lines are too long

View File

@@ -96,12 +96,23 @@ export function registerSystemHandlers() {
try {
const response = await axios({ method: 'get', url: image_url, responseType: 'stream', headers });
const content_type = (response.headers['content-type'] ?? '') as string;
if (!content_type.startsWith('image/')) {
response.data.destroy();
return { success: false, error: `URL did not return an image (Content-Type: ${content_type})` };
}
const writer = fs.createWriteStream(dest_path);
response.data.pipe(writer);
await new Promise<void>((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
const file_size = fs.statSync(dest_path).size;
if (file_size === 0) {
try { fs.unlinkSync(dest_path); } catch {}
return { success: false, error: 'Wallpaper download incomplete (0 bytes)' };
}
return { success: true, path: dest_path };
} catch (e: any) {
return { success: false, error: `Wallpaper download failed: ${e.message}` };
@@ -144,6 +155,12 @@ export function registerSystemHandlers() {
primary_path = result.path;
}
if (!primary_path && url_external && display === 'external') {
const ext_result = await download_wallpaper_image(url_external, 'wallpaper_external');
if (!ext_result.success || !ext_result.path) return { success: false, error: ext_result.error };
return await apply_mac_wallpaper(ext_result.path, 'external');
}
if (!primary_path) return { success: false, error: 'No image source provided' };
if (url_external) {
@@ -152,8 +169,8 @@ export function registerSystemHandlers() {
if (!primary_result.success) return primary_result;
const ext_result = await download_wallpaper_image(url_external, 'wallpaper_external');
const ext_path = ext_result.success && ext_result.path ? ext_result.path : primary_path;
return await apply_mac_wallpaper(ext_path, 'external');
if (!ext_result.success || !ext_result.path) return { success: false, error: ext_result.error };
return await apply_mac_wallpaper(ext_result.path, 'external');
} else {
return await apply_mac_wallpaper(primary_path, display);
}