Add uploads to a media connector
Add these methods to a working media connector so end users can upload images through an image variable in GraFx Studio.
Implement uploads
Import Connector and Media from @chili-publish/studio-connectors. Add Media.MediaConnectorUpload to your class's implements list to type-check upload(). In getCapabilities(), keep your existing capabilities and add upload: true.
This example uses an illustrative asset API that accepts one file per multipart request and returns JSON with the created asset's id. Replace the URL, request fields, and response mapping with those of your service.
Both detail() and download() must resolve asset IDs from query() and upload(). If you started from the simple media connector tutorial, update query(), detail(), and download() to use your asset service so each asset keeps the same ID across methods.
getCapabilities(): Media.MediaConnectorCapabilities {
return {
query: true,
detail: true,
filtering: false,
metadata: false,
upload: true,
};
}
async upload(
files: Connector.FilePointer[],
_context: Connector.Dictionary
): Promise<Media.Media[]> {
const results = await Promise.allSettled(
files.map(async (file): Promise<Media.Media> => {
const formData = new StudioFormData();
formData.append('file', file);
const response = await this.runtime.fetch('https://assets.example.com/uploads', {
method: 'POST',
body: formData.toJSON(),
});
if (!response.ok) {
throw new ConnectorHttpError(response.status, `Upload failed: ${response.statusText}`);
}
const asset = JSON.parse(response.text) as { id: string };
return {
id: asset.id,
name: file.name,
relativePath: '/',
type: 0,
metaData: {},
};
})
);
return results.map((result) => {
if (result.status === 'rejected') {
throw result.reason;
}
return result.value;
});
}
StudioFormData and ConnectorHttpError are provided by the connector runtime. The example returns one media item per file, with type: 0 for a file and relativePath: '/' for a flat asset library.
The example waits for all requests to settle before reporting a failure. Some files may already be stored. If your service supports idempotency keys, reuse the same per-file key on retry. For uncertain outcomes such as timeouts, check which files the service stored before retrying.
File types and size limits
- File types. Studio supports JPEG (JPG), PNG, TIFF, and PDF for image-variable uploads.
- File size. The default limit is about 1 GB per file. Your destination service may apply a lower limit.
PDF availability
Studio Run mode offers PDF only in environments where PDF uploads are enabled.
Add optional settings
If your service needs settings from the template designer, declare them in getConfigurationOptions(). Use context: ['upload'] for Upload Options, context: ['query'] for Browse Options, or context: ['query', 'upload'] to show the option in both.
getConfigurationOptions(): Connector.ConnectorConfigValue[] {
return [
{ name: 'assetLabel', displayName: 'Asset Label', type: 'text', context: ['upload'] },
{ name: 'searchFilter', displayName: 'Search Filter', type: 'text', context: ['query'] },
{ name: 'collectionId', displayName: 'Collection ID', type: 'text', context: ['query', 'upload'] },
];
}
In upload(), rename _context to context and read settings by their name. For example, if your API accepts the collection ID as a form field, add this after appending the file.
const collectionId = context['collectionId'];
if (typeof collectionId === 'string' && collectionId.trim()) {
formData.append('collectionId', collectionId.trim());
}
Verify the upload in GraFx Studio
- Build and republish your existing connector in the test environment using the Connector CLI. Pass its ID with
--connectorIdand include the hostnames your connector calls in--proxyOption.allowedDomains. - Select your connector for an image variable and enable Allow Upload. Fill in any options your connector exposes.
- Upload an image in the end-user view. Verify that your service stores the file and Studio displays it in the linked image frame.
GraFx Media reference
The GraFx Media example reads an uploadFolder setting and defaults to /Upload. It posts each file to the GraFx Environment API's /media endpoint with the folder path and file name. GraFx Media upload method ยท Folder handling