Skip to content

Commit d5208bf

Browse files
committed
fix: correct isArray support for create/edit views
1 parent 30f15e6 commit d5208bf

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

custom/uploader.vue

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,24 @@ onMounted(async () => {
169169
170170
const existingValue = (props as any).value;
171171
const existingFilePath =
172-
typeof existingValue === 'string' && existingValue.trim() ? existingValue : null;
173-
172+
typeof existingValue === 'string' && existingValue.trim() ? existingValue : null;
174173
if (!uploaded.value && props.record?.[previewColumnName]) {
175-
imgPreview.value = props.record[previewColumnName];
174+
if (Array.isArray(props.record[previewColumnName]) && props.record[previewColumnName].length > 0) {
175+
const resp = await callAdminForthApi({
176+
path: `/plugin/${props.meta.pluginInstanceId}/get-file-preview-url`,
177+
method: 'POST',
178+
body: { filePath: existingFilePath },
179+
});
180+
if (!resp?.error && resp?.url) {
181+
imgPreview.value = resp.url;
182+
uploaded.value = true;
183+
emit('update:emptiness', false);
184+
return;
185+
}
186+
imgPreview.value = resp.url;
187+
} else {
188+
imgPreview.value = props.record[previewColumnName];
189+
}
176190
uploaded.value = true;
177191
emit('update:emptiness', false);
178192
} else if (!uploaded.value && existingFilePath) {

index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export default class UploadPlugin extends AdminForthPlugin {
4040
}
4141

4242
this.getFileUploadUrl = async ( originalFilename, contentType, size, originalExtension, recordPk ) : Promise<{ uploadUrl: string, tagline?: string, filePath?: string, uploadExtraParams?: Record<string, string>, previewUrl?: string, error?: string } | {error: string}> => {
43-
4443
if (this.options.allowedFileExtensions && !this.options.allowedFileExtensions.includes(originalExtension.toLowerCase())) {
4544
return {
4645
error: `File extension "${originalExtension}" is not allowed, allowed extensions are: ${this.options.allowedFileExtensions.join(', ')}`
@@ -526,6 +525,23 @@ export default class UploadPlugin extends AdminForthPlugin {
526525
},
527526
});
528527

528+
server.endpoint({
529+
method: 'POST',
530+
path: `/plugin/${this.pluginInstanceId}/get-file-preview-url`,
531+
handler: async ({ body, adminUser }) => {
532+
const { filePath } = body;
533+
if (!filePath) {
534+
return { error: 'Missing filePath' };
535+
}
536+
if (this.options.preview?.previewUrl) {
537+
const url = this.options.preview.previewUrl({ filePath });
538+
return { url };
539+
}
540+
return { error: 'failed to generate preview URL' };
541+
},
542+
});
543+
544+
529545
}
530546

531547

0 commit comments

Comments
 (0)