Skip to content

Commit

Permalink
Merge pull request #547 from uploadcare/feat/metadata-callback-argument
Browse files Browse the repository at this point in the history
Feat/metadata-callback-argument
  • Loading branch information
nd0ut authored Oct 27, 2023
2 parents 542dd9c + 9a79bf6 commit 8f448c5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
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

0 comments on commit 8f448c5

Please sign in to comment.