diff --git a/abstract/UploaderBlock.js b/abstract/UploaderBlock.js index 29f20b528..ba5c29f21 100644 --- a/abstract/UploaderBlock.js +++ b/abstract/UploaderBlock.js @@ -11,7 +11,7 @@ import { serializeCsv } from '../blocks/utils/comma-separated.js'; import { debounce } from '../blocks/utils/debounce.js'; import { customUserAgent } from '../blocks/utils/userAgent.js'; import { buildCollectionFileError, buildOutputFileError } from '../utils/buildOutputError.js'; -import { createCdnUrl, createCdnUrlModifiers } from '../utils/cdn-utils.js'; +import { createCdnUrl, createCdnUrlModifiers, extractUuid, isValidURL } from '../utils/cdn-utils.js'; import { IMAGE_ACCEPT_LIST, fileIsImage, matchExtension, matchMimeType, mergeFileTypes } from '../utils/fileTypes.js'; import { prettyBytes } from '../utils/prettyBytes.js'; import { stringToArray } from '../utils/stringToArray.js'; @@ -21,6 +21,7 @@ import { TypedCollection } from './TypedCollection.js'; import { buildOutputCollectionState } from './buildOutputCollectionState.js'; import { uploadEntrySchema } from './uploadEntrySchema.js'; import { parseCdnUrl } from '../utils/parseCdnUrl.js'; + export class UploaderBlock extends ActivityBlock { couldBeCtxOwner = false; isCtxOwner = false; @@ -822,6 +823,18 @@ export class UploaderBlock extends ActivityBlock { return configValue; } + getDefaultValue() { + /** @type {string[]} */ + // @ts-ignore + const list = this.cfg.defaultValue ?? []; + + for (const key of list) { + const uuid = isValidURL(key) ? extractUuid(key) : key; + + this.addFileFromUuid(uuid); + } + } + /** @returns {import('@uploadcare/upload-client').FileFromOptions} */ getUploadClientOptions() { let options = { diff --git a/blocks/Config/Config.js b/blocks/Config/Config.js index 4ddc6ae95..ff77130d7 100644 --- a/blocks/Config/Config.js +++ b/blocks/Config/Config.js @@ -13,7 +13,7 @@ const allConfigKeys = /** @type {(keyof import('../../types').ConfigType)[]} */ * * @type {(keyof import('../../types').ConfigComplexType)[]} */ -const complexConfigKeys = ['metadata']; +const complexConfigKeys = ['metadata', 'defaultValue']; /** @type {(key: keyof import('../../types').ConfigType) => key is keyof import('../../types').ConfigComplexType} */ const isComplexKey = (key) => complexConfigKeys.includes(key); diff --git a/blocks/Config/initialConfig.js b/blocks/Config/initialConfig.js index f834b7485..7004478a6 100644 --- a/blocks/Config/initialConfig.js +++ b/blocks/Config/initialConfig.js @@ -58,4 +58,5 @@ export const initialConfig = { debug: false, metadata: null, + defaultValue: null, }; diff --git a/blocks/UploadList/UploadList.js b/blocks/UploadList/UploadList.js index 7f5585cf4..c7a08fa19 100644 --- a/blocks/UploadList/UploadList.js +++ b/blocks/UploadList/UploadList.js @@ -142,6 +142,9 @@ export class UploadList extends UploaderBlock { this.subConfigValue('multiple', this._throttledHandleCollectionUpdate); this.subConfigValue('multipleMin', this._throttledHandleCollectionUpdate); this.subConfigValue('multipleMax', this._throttledHandleCollectionUpdate); + this.subConfigValue('defaultValue', () => { + this.getDefaultValue(); + }); this.sub('*currentActivity', (currentActivity) => { if (!this.couldOpenActivity && currentActivity === this.activityType) { diff --git a/types/exported.d.ts b/types/exported.d.ts index d23a3bd48..625fdfa67 100644 --- a/types/exported.d.ts +++ b/types/exported.d.ts @@ -48,8 +48,9 @@ export type ConfigType = { userAgentIntegration: string; debug: boolean; metadata: Metadata | MetadataCallback | null; + defaultValue: string | null; }; -export type ConfigComplexType = Pick; +export type ConfigComplexType = Pick; export type ConfigPlainType = Omit; export type ConfigAttributesType = KebabCaseKeys & LowerCaseKeys; diff --git a/utils/cdn-utils.js b/utils/cdn-utils.js index deebb064a..37ca37c48 100644 --- a/utils/cdn-utils.js +++ b/utils/cdn-utils.js @@ -187,3 +187,12 @@ export const createOriginalUrl = (cdnUrl, uuid) => { url.pathname = uuid + '/'; return url.toString(); }; + +export const isValidURL = (url) => { + try { + new URL(url); + return true; + } catch (error) { + return false; + } +};