-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathupload-image.ts
More file actions
95 lines (85 loc) · 2.68 KB
/
upload-image.ts
File metadata and controls
95 lines (85 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { generateAltText } from '../../../utils/generate-alt-text';
import { runAbility } from '../../../utils/run-ability';
import { trimText } from '../../../utils/text';
import type {
GeneratedImageData,
ImageImportAbilityInput,
ImageProgressCallback,
UploadedImage,
} from '../types';
const { aiImageGenerationData } = window as any;
/**
* Uploads an image to the media library.
*
* @param {GeneratedImageData} imageData The generated image data (from generateImage).
* @param {Object} options Optional settings.
* @param {Function} options.onProgress Callback invoked with progress messages.
* @param options.altTextEnabled
* @return {Promise<UploadedImage>} A promise that resolves to the uploaded image data.
*/
export async function uploadImage(
{ image, prompt, prompts }: GeneratedImageData,
options?: { onProgress?: ImageProgressCallback; altTextEnabled?: boolean }
): Promise< UploadedImage > {
const onProgress = options?.onProgress;
const promptHistory = prompts?.length ? prompts : [ prompt ];
const params: ImageImportAbilityInput = {
data: image.data,
mime_type: 'image/png',
description: sprintf(
/* translators: 1: Provider name, 2: Model name, 3: Date, 4: Prompt */
__( 'Generated by %1$s using %2$s on %3$s. Prompt: %4$s', 'ai' ),
image.provider_metadata.name,
image.model_metadata.name,
new Date().toLocaleDateString(),
promptHistory.join( ' | ' )
),
meta: [
{
key: 'ai_generated',
value: '1',
},
],
};
// Use the prompt as alt text by default.
params.alt_text = prompt;
// If alt text generation is enabled, try generating alt text.
const isAltTextEnabled =
options?.altTextEnabled ?? aiImageGenerationData?.altTextEnabled;
if ( isAltTextEnabled ) {
try {
onProgress?.( __( 'Generating alt text…', 'ai' ) );
const altResult = await generateAltText(
undefined,
`data:image/png;base64,${ image.data }`
);
params.alt_text = altResult.alt_text;
} catch ( error ) {
params.alt_text = prompt;
}
}
// Set our image title to be a trimmed version of the alt text.
params.title = trimText( params.alt_text ?? '' );
onProgress?.( __( 'Importing image…', 'ai' ) );
return await runAbility( 'ai/image-import', params )
.then( ( response: any ) => {
if (
response &&
typeof response === 'object' &&
'image' in response
) {
return response.image as UploadedImage;
}
throw new Error( 'Invalid response from image import' );
} )
.catch( ( error ) => {
throw new Error( error.message );
} );
}