PluginProbe
Extendify / 3.1.3
Extendify v3.1.3
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / AutoLaunch / fetchers / get-logo.js

get-logo.js in Extendify 3.1.3, at src/AutoLaunch/fetchers/get-logo.js

87 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { getLogoShape } from '@auto-launch/fetchers/shape';
2 import {
3 failWithFallback,
4 fetchWithTimeout,
5 retryTwice,
6 setStatus,
7 } from '@auto-launch/functions/helpers';
8 import { updateOption } from '@auto-launch/functions/wp';
9 import { AI_HOST } from '@constants';
10 import { digest } from '@shared/api/digest';
11 import { reqDataBasics } from '@shared/lib/data';
12 import { __ } from '@wordpress/i18n';
13 import { uploadMedia } from '@wordpress/media-utils';
14
15 const { showAILogo } = window.extSharedData;
16 const fallback = {
17 logoUrl:
18 'https://images.extendify-cdn.com/demo-content/logos/ext-custom-logo-default.webp',
19 };
20 const url = `${AI_HOST}/api/site-profile/generate-logo`;
21 const method = 'POST';
22 const headers = { 'Content-Type': 'application/json' };
23
24 export const handleSiteLogo = async ({ siteProfile }) => {
25 if (!showAILogo) return fallback;
26
27 // translators: this is for a action log UI. Keep it short
28 setStatus(__('Generating a logo', 'extendify-local'));
29
30 const { logoObjectName: objectName } = siteProfile;
31 const body = JSON.stringify({ ...reqDataBasics, objectName });
32 const response = await retryTwice(() =>
33 fetchWithTimeout(url, { method, headers, body }),
34 ).catch((error) => {
35 return { ok: false, statusText: error.message, status: 0 };
36 });
37
38 if (!response?.ok) {
39 digest({
40 error: {
41 message: response.statusText,
42 name: 'FetchError',
43 status: response.status,
44 },
45 details: { source: 'auto-launch', caller: 'handleSiteLogo', objectName },
46 });
47 return fallback;
48 }
49
50 const logoUrl = await failWithFallback(
51 async () => {
52 const { logoUrl } = getLogoShape.parse(await response.json());
53 return logoUrl;
54 },
55 fallback.logoUrl,
56 { caller: 'handleSiteLogo' },
57 );
58
59 // If this errors we just move on.
60 await uploadLogo(logoUrl);
61
62 return getLogoShape.parse({ logoUrl });
63 };
64
65 export const uploadLogo = async (url) => {
66 const blob = await (await fetch(url)).blob();
67 const type = blob.type;
68 const fileExtension = type.replace('image/', '');
69 const logoName = `ext-custom-logo-${Date.now()}`;
70 const image = new File([blob], `${logoName}.${fileExtension}`, { type });
71
72 await uploadMedia({
73 filesList: [image],
74 onFileChange: async ([fileObj]) => {
75 if (!fileObj?.id) return;
76 await updateOption('site_logo', fileObj.id);
77 },
78 onError: (err) => {
79 console.error('Error uploading logo:', err);
80 digest({
81 error: err,
82 details: { source: 'auto-launch', caller: 'uploadLogo' },
83 });
84 },
85 });
86 };
87