import { Directive, forwardRef, inject, input, OnInit, output, signal, } from…
- User
import { Directive, forwardRef, inject, input, OnInit, output, signal, } from '@angular/core'; import { ApiService, FileManagerService, OnChangeType, OnTouchedType, UploadFileResponse, } from '@simorgh/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { UploadComponent } from '../upload.component'; import { CustomHttpUploader } from '../plugins/xhr-uploader.plugin'; import { firstValueFrom } from 'rxjs';
@Directive({ selector: '[smgUploader]', providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UploaderDirective), multi: true, }, ], }) export class UploaderDirective implements OnInit, ControlValueAccessor { pathField = input(); plus = input(true); fileId = signal(''); policyToken = signal(''); resolved = output();
private onChange: OnChangeType = () => {}; private onTouched: OnTouchedType = () => {};
private readonly fileManagerService = inject(FileManagerService); private uploadComponent = inject(UploadComponent, { host: true }); private readonly http = inject(ApiService);
ngOnInit(): void { if (this.pathField()) { this.loadFilePolicy(); } if (!this.plus) { this.setupUploaderPlugin(); } }
registerOnChange(fn: OnChangeType): void { this.onChange = fn; }
registerOnTouched(fn: OnTouchedType): void { this.onTouched = fn; }
writeValue(fileId: string): void { if (!fileId) { this.fileId.set(''); return; }
this.fileId.set(fileId); this.loadExistingFile(fileId);}
private loadFilePolicy(): void { this.getFilePolicy(this.pathField()!).subscribe({ next: ({ extensions, mimetypes, maxSize, token }) => { this.policyToken.set(token); this.setUploadRestrictions(mimetypes, maxSize); this.setupUploaderPlugin(); }, error: () => console.log('common.file.policy.fetchError'), }); }
private getFilePolicy(pathField: string) { return this.fileManagerService.getToken({ pathField }); }
private setUploadRestrictions(mimetypes: string[], maxSize: number): void { const maxFileSize = maxSize * 1024 * 1024; this.uploadComponent.uppy.setOptions({ restrictions: { allowedFileTypes: mimetypes ?? undefined, maxFileSize, }, }); }
private setupUploaderPlugin(): void { const { path, pathPlus, uploadFilePath } = this.fileManagerService.config(); const basePath = this.plus() ? pathPlus : path; const endpoint =
${basePath}${uploadFilePath}; this.uploadComponent.uppy.use(CustomHttpUploader, { endpoint, token: this.policyToken() ?? '', http: this.http, onUploadSuccess: (response) => this.handleUploadSuccess(response), }); }private handleUploadSuccess(response: any): void { const { fileId } = response.body; this.fileId.set(fileId); this.onChange(fileId); }
private loadExistingFile(fileId: string): void { this.fileManagerService.getFilesInfo(fileId).subscribe({ next: (files) => { console.log(files[0]); const fileInfo = files[0]; if (fileInfo && this.uploadComponent.uppy) { this.addExistingFileToUppy(fileInfo, fileId); } }, error: (err) => console.error('Error loading file info for preview', err), }); }
Assistant
To handle multiple files in the
loadExistingFilemethod, modify the code to iterate over all files in the response and add each to Uppy. Here's the step-by-step solution:- Update the
loadExistingFilemethod to loop through each file in the response array. - Pass each file's individual ID to ensure the correct file is downloaded.
- Fix hardcoded name and type in to use actual file info.
- Update the
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.