Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added uuid property in lr-config and upload image #640

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
};
Loading