fix: enhance URL validation for Z.AI image generation (#5482) * fix: enhance URL validation for Z.AI image generation and handle potential delays Fixes #5480 * fix: retry 404 in a loop * fix: handle Z.AI image and video unavailability with appropriate warnings and responses

b8f2b1cfa66ae9dee462d329b8c114f747bc21f6

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +29 -9Ignore whitespace
src/endpoints/stable-diffusion.js+29 -9
@@ -1906,23 +1906,41 @@ zai.post('/generate', async (request, response) => {
19061906 const data = await generateResponse.json();
19071907 console.debug('Z.AI image response:', data);
19081908
19091909 const urlurlString = String(data?.data?.[0]?.url ?? '');
19101910 if (!urlurlString || !isValidUrl(url) || !new URL(url).hostname.endsWith('.z.ai'urlString)) {
19111911 console.warn('Z.AI returned an invalid image URL.');
19121912 return response.sendStatus(500);
19131913 }
19141914
19151915 const imageResponseurl = awaitnew fetchURL(urlurlString);
1916- if (!imageResponse.ok) {
1916+ if (!url.hostname.endsWith('.z.ai') && !url.hostname.endsWith('.ufileos.com')) {
19171917 console.warn('Z.AI image fetch returned ana error.URL Status:',with imageResponse.status,an imageResponseunrecognized hostname.statusText');
19181918 return response.sendStatus(500);
19191919 }
19201920
1921- const buffer = await imageResponse.arrayBuffer();
1921+ for (let attempt = 0; attempt < 5; attempt++) {
1922- const image = Buffer.from(buffer).toString('base64');
1922+ const imageResponse = await fetch(url);
1923- const format = path.extname(url).substring(1).toLowerCase() || 'png';
1923+ if (!imageResponse.ok) {
1924+ // Sometimes the URL is valid but the image isn't immediately available
1925+ if (imageResponse.status === 404) {
1926+ console.info('Z.AI image not found yet, retrying...', { attempt: attempt + 1 });
1927+ await delay(1000);
1928+ continue;
1929+ }
19241930
1925- return response.send({ image, format });
1931+ console.warn('Z.AI image fetch returned an error. Status:', imageResponse.status, imageResponse.statusText);
1932+ return response.sendStatus(500);
1933+ }
1934+
1935+ const buffer = await imageResponse.arrayBuffer();
1936+ const image = Buffer.from(buffer).toString('base64');
1937+ const format = path.extname(url.pathname).substring(1).toLowerCase() || 'png';
1938+
1939+ return response.send({ image, format });
1940+ }
1941+
1942+ console.warn('Z.AI image was not available after multiple attempts.');
1943+ return response.sendStatus(500);
19261944 } catch (error) {
19271945 console.error(error);
19281946 return response.sendStatus(500);
@@ -2024,6 +2042,8 @@ zai.post('/generate-video', async (request, response) => {
20242042 return response.send({ format: 'mp4', video: Buffer.from(contentBuffer).toString('base64') });
20252043 }
20262044 }
2045+ console.warn('Z.AI video was not available after multiple attempts.');
2046+ return response.sendStatus(500);
20272047 } catch (error) {
20282048 console.error(error);
20292049 return response.sendStatus(500);