@@ -132,7 +132,6 @@ import type {
132132} from ' ~/types/uploads' ;
133133import { useTokenInfo } from ' ~/composables/useTokenInfo' ;
134134import { useMetadata } from ' ~/composables/useMetadata' ;
135- import { useMetadataParser } from ' ~/composables/useMetadataParser' ;
136135import { validateSlot } from ' ~/utils/validation' ;
137136import { useTusUpload } from ' ~/composables/useTusUpload' ;
138137import { useUploadSlots } from ' ~/composables/useUploadSlots' ;
@@ -145,8 +144,7 @@ const token = ref<string>((route.params.token as string) || '');
145144
146145const { tokenInfo, notFound, tokenError, isExpired, isDisabled, shareLinkText, fetchTokenInfo } =
147146 useTokenInfo (token );
148- const { metadataSchema, fetchMetadata } = useMetadata ();
149- const { applyParsedMeta } = useMetadataParser ();
147+ const { metadataSchema, fetchMetadata, extractMetadata } = useMetadata ();
150148const { startTusUpload, pauseUpload, resumeUpload } = useTusUpload ();
151149const { slots, seedSlots, addSlot, unintiatedSlots } = useUploadSlots (metadataSchema );
152150const { pollUploadStatus, stopPolling, stopAllPolling } = useUploadPolling ();
@@ -211,11 +209,35 @@ async function refreshAll() {
211209 seedSlots (tokenInfo .value );
212210}
213211
214- function onFile(slot : Slot , e : Event ) {
212+ function buildDefaultMetadataValues() {
213+ return Object .fromEntries (metadataSchema .value .map ((field ) => [field .key , field .default ?? ' ' ]));
214+ }
215+
216+ async function onFile(slot : Slot , e : Event ) {
215217 const target = e .target as HTMLInputElement ;
216- slot .file = target .files ?.[0 ] || null ;
217- metadataSchema .value .forEach ((f ) => (slot .values [f .key ] = f .default ?? ' ' ));
218- if (slot .file ) applyParsedMeta (slot , slot .file .name , metadataSchema .value );
218+ const selectedFile = target .files ?.[0 ] || null ;
219+ const defaultValues = buildDefaultMetadataValues ();
220+
221+ slot .file = selectedFile ;
222+ slot .values = defaultValues ;
223+ slot .errors = validateSlot (slot , metadataSchema .value , tokenInfo .value );
224+
225+ if (! selectedFile ) {
226+ return ;
227+ }
228+
229+ const extractedValues = await extractMetadata (selectedFile .name );
230+ if (slot .file !== selectedFile ) {
231+ return ;
232+ }
233+
234+ const nextValues = { ... slot .values };
235+ Object .entries (extractedValues ).forEach (([key , value ]) => {
236+ if (slot .values [key ] === defaultValues [key ]) {
237+ nextValues [key ] = value ;
238+ }
239+ });
240+ slot .values = nextValues ;
219241 slot .errors = validateSlot (slot , metadataSchema .value , tokenInfo .value );
220242}
221243
0 commit comments