Skip to content

Commit f9816e8

Browse files
author
Egor Didenko
committed
feat: added defaultValue property in lr-config and upload image
1 parent d47baf0 commit f9816e8

File tree

6 files changed

+30
-3
lines changed

6 files changed

+30
-3
lines changed

Diff for: abstract/UploaderBlock.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { serializeCsv } from '../blocks/utils/comma-separated.js';
1111
import { debounce } from '../blocks/utils/debounce.js';
1212
import { customUserAgent } from '../blocks/utils/userAgent.js';
1313
import { buildCollectionFileError, buildOutputFileError } from '../utils/buildOutputError.js';
14-
import { createCdnUrl, createCdnUrlModifiers } from '../utils/cdn-utils.js';
14+
import { createCdnUrl, createCdnUrlModifiers, extractUuid, isValidURL } from '../utils/cdn-utils.js';
1515
import { IMAGE_ACCEPT_LIST, fileIsImage, matchExtension, matchMimeType, mergeFileTypes } from '../utils/fileTypes.js';
1616
import { prettyBytes } from '../utils/prettyBytes.js';
1717
import { stringToArray } from '../utils/stringToArray.js';
@@ -21,6 +21,7 @@ import { TypedCollection } from './TypedCollection.js';
2121
import { buildOutputCollectionState } from './buildOutputCollectionState.js';
2222
import { uploadEntrySchema } from './uploadEntrySchema.js';
2323
import { parseCdnUrl } from '../utils/parseCdnUrl.js';
24+
2425
export class UploaderBlock extends ActivityBlock {
2526
couldBeCtxOwner = false;
2627
isCtxOwner = false;
@@ -822,6 +823,18 @@ export class UploaderBlock extends ActivityBlock {
822823
return configValue;
823824
}
824825

826+
getDefaultValue() {
827+
/** @type {string[]} */
828+
// @ts-ignore
829+
const list = this.cfg.defaultValue ?? [];
830+
831+
for (const key of list) {
832+
const uuid = isValidURL(key) ? extractUuid(key) : key;
833+
834+
this.addFileFromUuid(uuid);
835+
}
836+
}
837+
825838
/** @returns {import('@uploadcare/upload-client').FileFromOptions} */
826839
getUploadClientOptions() {
827840
let options = {

Diff for: blocks/Config/Config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const allConfigKeys = /** @type {(keyof import('../../types').ConfigType)[]} */
1313
*
1414
* @type {(keyof import('../../types').ConfigComplexType)[]}
1515
*/
16-
const complexConfigKeys = ['metadata'];
16+
const complexConfigKeys = ['metadata', 'defaultValue'];
1717

1818
/** @type {(key: keyof import('../../types').ConfigType) => key is keyof import('../../types').ConfigComplexType} */
1919
const isComplexKey = (key) => complexConfigKeys.includes(key);

Diff for: blocks/Config/initialConfig.js

+1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ export const initialConfig = {
5858
debug: false,
5959

6060
metadata: null,
61+
defaultValue: null,
6162
};

Diff for: blocks/UploadList/UploadList.js

+3
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ export class UploadList extends UploaderBlock {
142142
this.subConfigValue('multiple', this._throttledHandleCollectionUpdate);
143143
this.subConfigValue('multipleMin', this._throttledHandleCollectionUpdate);
144144
this.subConfigValue('multipleMax', this._throttledHandleCollectionUpdate);
145+
this.subConfigValue('defaultValue', () => {
146+
this.getDefaultValue();
147+
});
145148

146149
this.sub('*currentActivity', (currentActivity) => {
147150
if (!this.couldOpenActivity && currentActivity === this.activityType) {

Diff for: types/exported.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ export type ConfigType = {
4848
userAgentIntegration: string;
4949
debug: boolean;
5050
metadata: Metadata | MetadataCallback | null;
51+
defaultValue: string[] | null;
5152
};
52-
export type ConfigComplexType = Pick<ConfigType, 'metadata'>;
53+
export type ConfigComplexType = Pick<ConfigType, 'metadata' | 'defaultValue'>;
5354
export type ConfigPlainType = Omit<ConfigType, keyof ConfigComplexType>;
5455
export type ConfigAttributesType = KebabCaseKeys<ConfigPlainType> & LowerCaseKeys<ConfigPlainType>;
5556

Diff for: utils/cdn-utils.js

+9
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,12 @@ export const createOriginalUrl = (cdnUrl, uuid) => {
187187
url.pathname = uuid + '/';
188188
return url.toString();
189189
};
190+
191+
export const isValidURL = (url) => {
192+
try {
193+
new URL(url);
194+
return true;
195+
} catch (error) {
196+
return false;
197+
}
198+
};

0 commit comments

Comments
 (0)