Path: blob/main/extensions/copilot/src/platform/image/node/imageServiceImpl.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { RequestType } from '@vscode/copilot-api';6import { URI } from '../../../util/vs/base/common/uri';7import { ICAPIClientService } from '../../endpoint/common/capiClient';8import { IImageService } from '../common/imageService';910export class ImageServiceImpl implements IImageService {11declare readonly _serviceBrand: undefined;1213constructor(14@ICAPIClientService private readonly capiClient: ICAPIClientService,15) { }1617async uploadChatImageAttachment(binaryData: Uint8Array, name: string, mimeType: string | undefined, token: string | undefined): Promise<URI> {18if (!mimeType || !token) {19throw new Error('Missing required mimeType or token for image upload');20}2122const sanitizedName = name.replace(/[^a-zA-Z0-9._-]/g, '');23let uploadName = sanitizedName;2425// can catch unexpected types like "IMAGE/JPEG", "image/svg+xml", or "image/png; charset=UTF-8"26const subtypeMatch = mimeType.toLowerCase().match(/^[^\/]+\/([^+;]+)/);27const subtype = subtypeMatch?.[1];2829// add the extension if it is missing.30if (subtype && !uploadName.toLowerCase().endsWith(`.${subtype}`)) {31uploadName = `${uploadName}.${subtype}`;32}3334try {35const response = await this.capiClient.makeRequest<Response>({36method: 'POST',37body: binaryData,38headers: {39'Content-Type': 'application/octet-stream',40Authorization: `Bearer ${token}`,41}42}, { type: RequestType.ChatAttachmentUpload, uploadName, mimeType });43if (!response.ok) {44throw new Error(`Image upload failed: ${response.status} ${response.statusText}`);45}46const result = await response.json() as { url: string };47return URI.parse(result.url);48} catch (error) {49throw new Error(`Error uploading image: ${error}`);50}51}5253async resizeImage(data: Uint8Array, mimeType: string): Promise<{ data: Uint8Array; mimeType: string }> {54return { data, mimeType };55}56}575859