Skip to content

Commit 0dbdaaa

Browse files
Handle resource URIs
1 parent 03e69f0 commit 0dbdaaa

File tree

3 files changed

+125
-41
lines changed

3 files changed

+125
-41
lines changed

src/handlers/nasa/epic.ts

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type EpicParams = z.infer<typeof epicParamsSchema>;
2222
* @param collection The collection type (natural or enhanced)
2323
* @returns Formatted results with summary and image data
2424
*/
25-
function processEpicResults(epicData: any[], collection: string) {
25+
async function processEpicResults(epicData: any[], collection: string) {
2626
if (!Array.isArray(epicData) || epicData.length === 0) {
2727
return {
2828
summary: "No EPIC data available for the specified parameters.",
@@ -39,37 +39,79 @@ function processEpicResults(epicData: any[], collection: string) {
3939
const [year, month, day] = dateStr.split('-');
4040

4141
// Format each image and register it as a resource
42-
const images = epicData.map(img => {
42+
const images = [];
43+
44+
for (const img of epicData) {
4345
// Construct the image URL according to NASA's format
4446
const imageUrl = `${EPIC_IMAGE_BASE_URL}/${collection}/${year}/${month}/${day}/png/${img.image}.png`;
4547

4648
// Create a unique resource URI for this image
4749
const resourceUri = `nasa://epic/image/${collection}/${img.identifier}`;
4850

49-
// Register this image as a resource
50-
addResource(resourceUri, {
51-
name: `NASA EPIC Earth Image - ${img.identifier}`,
52-
mimeType: "image/png",
53-
text: JSON.stringify({
54-
id: img.identifier,
55-
date: img.date,
51+
try {
52+
// Fetch the actual image data
53+
const imageResponse = await axios({
54+
url: imageUrl,
55+
responseType: 'arraybuffer',
56+
timeout: 30000
57+
});
58+
59+
// Register this image as a resource with binary data
60+
addResource(resourceUri, {
61+
name: `NASA EPIC Earth Image - ${img.identifier}`,
62+
mimeType: "image/png",
63+
// Store metadata as text
64+
text: JSON.stringify({
65+
id: img.identifier,
66+
date: img.date,
67+
caption: img.caption || "Earth view from DSCOVR satellite",
68+
imageUrl: imageUrl,
69+
centroid_coordinates: img.centroid_coordinates,
70+
dscovr_j2000_position: img.dscovr_j2000_position,
71+
lunar_j2000_position: img.lunar_j2000_position,
72+
sun_j2000_position: img.sun_j2000_position,
73+
attitude_quaternions: img.attitude_quaternions
74+
}),
75+
// Store actual image data as blob
76+
blob: Buffer.from(imageResponse.data)
77+
});
78+
79+
images.push({
80+
identifier: img.identifier,
5681
caption: img.caption || "Earth view from DSCOVR satellite",
5782
imageUrl: imageUrl,
58-
centroid_coordinates: img.centroid_coordinates,
59-
dscovr_j2000_position: img.dscovr_j2000_position,
60-
lunar_j2000_position: img.lunar_j2000_position,
61-
sun_j2000_position: img.sun_j2000_position,
62-
attitude_quaternions: img.attitude_quaternions
63-
})
64-
});
65-
66-
return {
67-
identifier: img.identifier,
68-
caption: img.caption || "Earth view from DSCOVR satellite",
69-
imageUrl: imageUrl,
70-
resourceUri: resourceUri
71-
};
72-
});
83+
resourceUri: resourceUri
84+
});
85+
} catch (error) {
86+
console.error(`Error fetching EPIC image ${img.identifier}:`, error);
87+
88+
// If fetch fails, register with just the metadata
89+
addResource(resourceUri, {
90+
name: `NASA EPIC Earth Image - ${img.identifier}`,
91+
mimeType: "image/png",
92+
text: JSON.stringify({
93+
id: img.identifier,
94+
date: img.date,
95+
caption: img.caption || "Earth view from DSCOVR satellite",
96+
imageUrl: imageUrl,
97+
centroid_coordinates: img.centroid_coordinates,
98+
dscovr_j2000_position: img.dscovr_j2000_position,
99+
lunar_j2000_position: img.lunar_j2000_position,
100+
sun_j2000_position: img.sun_j2000_position,
101+
attitude_quaternions: img.attitude_quaternions,
102+
fetch_error: (error as Error).message
103+
})
104+
});
105+
106+
images.push({
107+
identifier: img.identifier,
108+
caption: img.caption || "Earth view from DSCOVR satellite",
109+
imageUrl: imageUrl,
110+
resourceUri: resourceUri,
111+
error: "Failed to fetch image data"
112+
});
113+
}
114+
}
73115

74116
return {
75117
summary: `EPIC Earth imagery from ${date} - Collection: ${collection} - ${images.length} images available`,
@@ -100,7 +142,7 @@ export async function nasaEpicHandler(params: EpicParams) {
100142

101143
// Process the results
102144
if (epicData && epicData.length > 0) {
103-
const results = processEpicResults(epicData, collection);
145+
const results = await processEpicResults(epicData, collection);
104146

105147
return {
106148
content: [

src/handlers/nasa/gibs.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,16 @@ export async function nasaGibsHandler(params: GibsParams) {
6161
addResource(resourceUri, {
6262
name: `NASA GIBS: ${layer} (${formattedDate})`,
6363
mimeType: `image/${format}`,
64-
text: imageBase64
64+
// Store metadata as text (optional)
65+
text: JSON.stringify({
66+
layer: layer,
67+
date: formattedDate,
68+
bbox: bboxParam,
69+
width: 720,
70+
height: 360
71+
}),
72+
// Store the actual image data as a blob
73+
blob: Buffer.from(response.data)
6574
});
6675

6776
// Return metadata and image data

src/handlers/nasa/mars_rover.ts

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z } from 'zod';
2+
import axios from 'axios';
23
import { nasaApiRequest } from '../../utils/api-client';
34
import { MarsRoverParams } from '../setup';
45
import { addResource } from '../../index';
@@ -50,7 +51,7 @@ export async function nasaMarsRoverHandler(params: MarsRoverParams) {
5051
/**
5152
* Process the Mars Rover API results, register resources, and format the response
5253
*/
53-
function processRoverResults(data: any, rover: string) {
54+
async function processRoverResults(data: any, rover: string) {
5455
const photos = data.photos || [];
5556
const resources = [];
5657

@@ -69,24 +70,56 @@ function processRoverResults(data: any, rover: string) {
6970
const photoId = photo.id.toString();
7071
const resourceUri = `nasa://mars_rover/photo?rover=${rover}&id=${photoId}`;
7172

72-
// Register the resource
73-
addResource(resourceUri, {
74-
name: `Mars Rover Photo ${photoId}`,
75-
mimeType: "image/jpeg",
76-
text: JSON.stringify({
77-
photo_id: photoId,
78-
rover: rover,
79-
camera: photo.camera?.name || 'Unknown',
80-
img_src: photo.img_src,
81-
earth_date: photo.earth_date,
82-
sol: photo.sol
83-
})
84-
});
73+
try {
74+
// Fetch the actual image data
75+
const imageResponse = await axios({
76+
url: photo.img_src,
77+
responseType: 'arraybuffer',
78+
timeout: 30000
79+
});
80+
81+
// Convert image data to Base64
82+
const imageBase64 = Buffer.from(imageResponse.data).toString('base64');
83+
84+
// Register the resource with binary data in the blob field
85+
addResource(resourceUri, {
86+
name: `Mars Rover Photo ${photoId}`,
87+
mimeType: "image/jpeg",
88+
// Store metadata as text for reference
89+
text: JSON.stringify({
90+
photo_id: photoId,
91+
rover: rover,
92+
camera: photo.camera?.name || 'Unknown',
93+
earth_date: photo.earth_date,
94+
sol: photo.sol,
95+
img_src: photo.img_src
96+
}),
97+
// Store the actual image data as a blob
98+
blob: Buffer.from(imageResponse.data)
99+
});
100+
} catch (error) {
101+
console.error(`Error fetching image for rover photo ${photoId}:`, error);
102+
103+
// If fetching fails, register with just the metadata and URL
104+
addResource(resourceUri, {
105+
name: `Mars Rover Photo ${photoId}`,
106+
mimeType: "image/jpeg",
107+
text: JSON.stringify({
108+
photo_id: photoId,
109+
rover: rover,
110+
camera: photo.camera?.name || 'Unknown',
111+
img_src: photo.img_src,
112+
earth_date: photo.earth_date,
113+
sol: photo.sol,
114+
fetch_error: (error as Error).message
115+
})
116+
});
117+
}
85118

86119
resources.push({
87120
title: `Mars Rover Photo ${photoId}`,
88121
description: `Photo taken by ${rover} rover on Mars`,
89-
resource_uri: `nasa://mars_rover/photo?rover=${rover}&id=${photo.id}`
122+
resource_uri: resourceUri
90123
});
91124
}
92125

0 commit comments

Comments
 (0)