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/metadata-callback-argument #547

Merged
merged 4 commits into from
Oct 27, 2023
Merged
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
79 changes: 42 additions & 37 deletions abstract/UploaderBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,18 +607,22 @@ export class UploaderBlock extends ActivityBlock {
}
}

/** @private */
async getMetadata() {
/**
* @param {string} entryId
* @protected
*/
async getMetadataFor(entryId) {
const configValue = this.cfg.metadata ?? /** @type {import('../types').Metadata} */ (this.$['*uploadMetadata']);
if (typeof configValue === 'function') {
const metadata = await configValue();
const outputFileEntry = this.getOutputItem(entryId);
const metadata = await configValue(outputFileEntry);
return metadata;
}
return configValue;
}

/** @returns {Promise<import('@uploadcare/upload-client').FileFromOptions>} */
async getUploadClientOptions() {
/** @returns {import('@uploadcare/upload-client').FileFromOptions} */
getUploadClientOptions() {
let options = {
store: this.cfg.store,
publicKey: this.cfg.pubkey,
Expand All @@ -635,48 +639,49 @@ export class UploaderBlock extends ActivityBlock {
multipartMaxAttempts: this.cfg.multipartMaxAttempts,
checkForUrlDuplicates: !!this.cfg.checkForUrlDuplicates,
saveUrlForRecurrentUploads: !!this.cfg.saveUrlForRecurrentUploads,
metadata: await this.getMetadata(),
};

console.log('Upload client options:', options);

return options;
}

/**
* @param {string} entryId
* @returns {import('../types/exported.js').OutputFileEntry}
*/
getOutputItem(entryId) {
const uploadEntryData = Data.getCtx(entryId).store;
/** @type {import('@uploadcare/upload-client').UploadcareFile} */
const fileInfo = uploadEntryData.fileInfo || {
name: uploadEntryData.fileName,
originalFilename: uploadEntryData.fileName,
size: uploadEntryData.fileSize,
isImage: uploadEntryData.isImage,
mimeType: uploadEntryData.mimeType,
};
/** @type {import('../types/exported.js').OutputFileEntry} */
const outputItem = {
...fileInfo,
file: uploadEntryData.file,
externalUrl: uploadEntryData.externalUrl,
cdnUrlModifiers: uploadEntryData.cdnUrlModifiers,
cdnUrl: uploadEntryData.cdnUrl ?? fileInfo.cdnUrl ?? null,
validationErrorMessage: uploadEntryData.validationErrorMsg,
uploadError: uploadEntryData.uploadError,
isUploaded: !!uploadEntryData.uuid && !!uploadEntryData.fileInfo,
isValid: !uploadEntryData.validationErrorMsg && !uploadEntryData.uploadError,
fullPath: uploadEntryData.fullPath,
uploadProgress: uploadEntryData.uploadProgress,
};
return outputItem;
}

/**
* @param {(item: import('./TypedData.js').TypedData) => Boolean} [checkFn]
* @returns {import('../types/exported.js').OutputFileEntry[]}
*/
getOutputData(checkFn) {
// @ts-ignore TODO: fix this
let data = [];
let items = checkFn ? this.uploadCollection.findItems(checkFn) : this.uploadCollection.items();
items.forEach((itemId) => {
let uploadEntryData = Data.getCtx(itemId).store;
/** @type {import('@uploadcare/upload-client').UploadcareFile} */
let fileInfo = uploadEntryData.fileInfo || {
name: uploadEntryData.fileName,
originalFilename: uploadEntryData.fileName,
size: uploadEntryData.fileSize,
isImage: uploadEntryData.isImage,
mimeType: uploadEntryData.mimeType,
};
/** @type {import('../types/exported.js').OutputFileEntry} */
let outputItem = {
...fileInfo,
file: uploadEntryData.file,
externalUrl: uploadEntryData.externalUrl,
cdnUrlModifiers: uploadEntryData.cdnUrlModifiers,
cdnUrl: uploadEntryData.cdnUrl ?? fileInfo.cdnUrl ?? null,
uploadProgress: uploadEntryData.uploadProgress,
validationErrorMessage: uploadEntryData.validationErrorMsg,
uploadError: uploadEntryData.uploadError,
isUploaded: !!uploadEntryData.uuid && !!uploadEntryData.fileInfo && !uploadEntryData.uploadError,
isValid: !uploadEntryData.validationErrorMsg && !uploadEntryData.uploadError,
};
data.push(outputItem);
});
// @ts-ignore TODO: fix this
const entriesIds = checkFn ? this.uploadCollection.findItems(checkFn) : this.uploadCollection.items();
const data = entriesIds.map((itemId) => this.getOutputItem(itemId));
return data;
}
}
Expand Down
2 changes: 1 addition & 1 deletion blocks/DataOutput/DataOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class DataOutput extends UploaderBlock {
return;
}

const uploadClientOptions = await this.getUploadClientOptions();
const uploadClientOptions = this.getUploadClientOptions();
const uuidList = data.map((fileDesc) => {
return fileDesc.uuid + (fileDesc.cdnUrlModifiers ? `/${fileDesc.cdnUrlModifiers}` : '');
});
Expand Down
3 changes: 2 additions & 1 deletion blocks/FileItem/FileItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ export class FileItem extends UploaderBlock {
entry.setValue('abortController', abortController);

const uploadTask = async () => {
const uploadClientOptions = await this.getUploadClientOptions();
const uploadClientOptions = this.getUploadClientOptions();
return uploadFile(entry.getValue('file') || entry.getValue('externalUrl') || entry.getValue('uuid'), {
...uploadClientOptions,
fileName: entry.getValue('fileName'),
Expand All @@ -405,6 +405,7 @@ export class FileItem extends UploaderBlock {
this.$.progressUnknown = !progress.isComputable;
},
signal: abortController.signal,
metadata: await this.getMetadataFor(entry.uid),
});
};

Expand Down
3 changes: 2 additions & 1 deletion types/exported.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UploadcareFile } from '@uploadcare/upload-client';

export type Metadata = import('@uploadcare/upload-client').Metadata;
export type MetadataCallback = () => Promise<Metadata>;
export type MetadataCallback = (fileEntry: OutputFileEntry) => Promise<Metadata> | Metadata;
export type ConfigType = {
pubkey: string;
multiple: boolean;
Expand Down Expand Up @@ -68,6 +68,7 @@ export type OutputFileEntry = Pick<UploadcareFile, requiredFileInfoFields> &
externalUrl: string | null;
isValid: boolean;
isUploaded: boolean;
fullPath: string | null;
uploadProgress: number;
};

Expand Down
Loading