Skip to content

Commit

Permalink
feat: added defaultValue property in lr-config and upload image
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Didenko committed Apr 8, 2024
1 parent d47baf0 commit 047671c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
15 changes: 14 additions & 1 deletion abstract/UploaderBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion blocks/Config/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions blocks/Config/initialConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ export const initialConfig = {
debug: false,

metadata: null,
defaultValue: null,
};
3 changes: 3 additions & 0 deletions blocks/UploadList/UploadList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion types/exported.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ export type ConfigType = {
userAgentIntegration: string;
debug: boolean;
metadata: Metadata | MetadataCallback | null;
defaultValue: string | null;
};
export type ConfigComplexType = Pick<ConfigType, 'metadata'>;
export type ConfigComplexType = Pick<ConfigType, 'metadata' | 'defaultValue'>;
export type ConfigPlainType = Omit<ConfigType, keyof ConfigComplexType>;
export type ConfigAttributesType = KebabCaseKeys<ConfigPlainType> & LowerCaseKeys<ConfigPlainType>;

Expand Down
9 changes: 9 additions & 0 deletions utils/cdn-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

0 comments on commit 047671c

Please sign in to comment.