From b0479446646470f82e755722239d18a76fd0b125 Mon Sep 17 00:00:00 2001 From: nd0ut Date: Thu, 26 Oct 2023 12:28:52 +0300 Subject: [PATCH 1/3] chore: remove console log with upload-client options --- abstract/UploaderBlock.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/abstract/UploaderBlock.js b/abstract/UploaderBlock.js index e3ce66066..e0e2a320b 100644 --- a/abstract/UploaderBlock.js +++ b/abstract/UploaderBlock.js @@ -638,8 +638,6 @@ export class UploaderBlock extends ActivityBlock { metadata: await this.getMetadata(), }; - console.log('Upload client options:', options); - return options; } From 9e4707fef14df91d29c5172585d23ac912115c71 Mon Sep 17 00:00:00 2001 From: nd0ut Date: Thu, 26 Oct 2023 12:29:50 +0300 Subject: [PATCH 2/3] feat: add property `fullPath` to the output entry --- abstract/UploaderBlock.js | 59 +++++++++++++++++++++------------------ types/exported.d.ts | 1 + 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/abstract/UploaderBlock.js b/abstract/UploaderBlock.js index e0e2a320b..4b8aa5dc2 100644 --- a/abstract/UploaderBlock.js +++ b/abstract/UploaderBlock.js @@ -641,38 +641,43 @@ export class UploaderBlock extends ActivityBlock { 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, + }; + 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, - }; - let 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, - }; - 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; } } diff --git a/types/exported.d.ts b/types/exported.d.ts index a2ea1f724..da970fdd6 100644 --- a/types/exported.d.ts +++ b/types/exported.d.ts @@ -68,6 +68,7 @@ export type OutputFileEntry = Pick & externalUrl: string | null; isValid: boolean; isUploaded: boolean; + fullPath: string | null }; export {}; From 915382619280fdadedac474ee4167cd52982685b Mon Sep 17 00:00:00 2001 From: nd0ut Date: Thu, 26 Oct 2023 12:32:24 +0300 Subject: [PATCH 3/3] feat: pass output entry to the metadata callback --- abstract/UploaderBlock.js | 15 +++++++++------ blocks/DataOutput/DataOutput.js | 2 +- blocks/FileItem/FileItem.js | 3 ++- types/exported.d.ts | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/abstract/UploaderBlock.js b/abstract/UploaderBlock.js index 4b8aa5dc2..7800c8ae0 100644 --- a/abstract/UploaderBlock.js +++ b/abstract/UploaderBlock.js @@ -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} */ - async getUploadClientOptions() { + /** @returns {import('@uploadcare/upload-client').FileFromOptions} */ + getUploadClientOptions() { let options = { store: this.cfg.store, publicKey: this.cfg.pubkey, @@ -635,7 +639,6 @@ export class UploaderBlock extends ActivityBlock { multipartMaxAttempts: this.cfg.multipartMaxAttempts, checkForUrlDuplicates: !!this.cfg.checkForUrlDuplicates, saveUrlForRecurrentUploads: !!this.cfg.saveUrlForRecurrentUploads, - metadata: await this.getMetadata(), }; return options; diff --git a/blocks/DataOutput/DataOutput.js b/blocks/DataOutput/DataOutput.js index b00691ef5..1f8ccccd5 100644 --- a/blocks/DataOutput/DataOutput.js +++ b/blocks/DataOutput/DataOutput.js @@ -126,7 +126,7 @@ export class DataOutput extends UploaderBlock { }; return; } - const uploadClientOptions = await this.getUploadClientOptions(); + const uploadClientOptions = this.getUploadClientOptions(); const resp = await uploadFileGroup(uuidList, uploadClientOptions); this.$.output = { groupData: resp, diff --git a/blocks/FileItem/FileItem.js b/blocks/FileItem/FileItem.js index b9061cfc2..85bf40f89 100644 --- a/blocks/FileItem/FileItem.js +++ b/blocks/FileItem/FileItem.js @@ -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'), @@ -405,6 +405,7 @@ export class FileItem extends UploaderBlock { this.$.progressUnknown = !progress.isComputable; }, signal: abortController.signal, + metadata: await this.getMetadataFor(entry.uid), }); }; diff --git a/types/exported.d.ts b/types/exported.d.ts index da970fdd6..4fe1ae5db 100644 --- a/types/exported.d.ts +++ b/types/exported.d.ts @@ -1,7 +1,7 @@ import { UploadcareFile } from '@uploadcare/upload-client'; export type Metadata = import('@uploadcare/upload-client').Metadata; -export type MetadataCallback = () => Promise; +export type MetadataCallback = (fileEntry: OutputFileEntry) => Promise | Metadata; export type ConfigType = { pubkey: string; multiple: boolean;