WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit e6b2bf4

Browse files
authored
Merge pull request #18 from devforth/feature/AdminForth/1007/remove-virtual-column-from-the
feat: allow to upload file directry from the url query param
2 parents 004fb37 + 2a65682 commit e6b2bf4

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

custom/uploader.vue

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ const progress = ref(0);
103103
const uploaded = ref(false);
104104
const uploadedSize = ref(0);
105105
106+
const downloadFileUrl = ref('');
107+
106108
watch(() => uploaded, (value) => {
107109
emit('update:emptiness', !value);
108110
});
@@ -118,9 +120,50 @@ function uploadGeneratedImage(imgBlob) {
118120
});
119121
}
120122
121-
onMounted(() => {
123+
onMounted(async () => {
122124
const previewColumnName = `previewUrl_${props.meta.pluginInstanceId}`;
123-
if (props.record[previewColumnName]) {
125+
let queryValues;
126+
try {
127+
queryValues = JSON.parse(atob(route.query.values as string));
128+
} catch (e) {
129+
queryValues = {};
130+
}
131+
132+
if (queryValues[props.meta.pathColumnName]) {
133+
downloadFileUrl.value = queryValues[props.meta.pathColumnName];
134+
135+
const resp = await callAdminForthApi({
136+
path: `/plugin/${props.meta.pluginInstanceId}/get-file-download-url`,
137+
method: 'POST',
138+
body: {
139+
filePath: queryValues[props.meta.pathColumnName]
140+
},
141+
});
142+
if (resp.error) {
143+
adminforth.alert({
144+
message: t('Error getting file url for field {field}:', { field: props.meta.pathColumnName }),
145+
variant: 'danger'
146+
});
147+
return;
148+
}
149+
const filename = resp.url.split('/').pop()?.split('?')[0] || `file`;
150+
const filenameParts = filename.split('.');
151+
const extension = filenameParts.length > 1 ? filenameParts.pop() : '';
152+
const nameWithoutExt = filenameParts.join('.');
153+
const newFileName = extension
154+
? `${nameWithoutExt}_copy_${Date.now()}.${extension}`
155+
: `${filename}_copy_${Date.now()}`;
156+
157+
158+
const res = await fetch(resp.url);
159+
const fileBlob = await res.blob();
160+
const file = new File([fileBlob], newFileName, { type: fileBlob.type });
161+
onFileChange({
162+
target: {
163+
files: [file],
164+
},
165+
});
166+
} else if (props.record[previewColumnName]) {
124167
imgPreview.value = props.record[previewColumnName];
125168
uploaded.value = true;
126169
emit('update:emptiness', false);

index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { PluginOptions } from './types.js';
33
import { AdminForthPlugin, AdminForthResourceColumn, AdminForthResource, Filters, IAdminForth, IHttpServer, suggestIfTypo } from "adminforth";
44
import { Readable } from "stream";
55
import { RateLimiter } from "adminforth";
6+
import { interpretResource } from 'adminforth';
7+
import { ActionCheckSource } from 'adminforth';
68

79
const ADMINFORTH_NOT_YET_USED_TAG = 'adminforth-candidate-for-cleanup';
810

@@ -86,6 +88,7 @@ export default class UploadPlugin extends AdminForthPlugin {
8688
minShowWidth: this.options.preview?.minShowWidth,
8789
generationPrompt: this.options.generation?.generationPrompt,
8890
recorPkFieldName: this.resourceConfig.columns.find((column: any) => column.primaryKey)?.name,
91+
pathColumnName: this.options.pathColumnName,
8992
};
9093
// define components which will be imported from other components
9194
this.componentPath('imageGenerator.vue');
@@ -427,6 +430,26 @@ export default class UploadPlugin extends AdminForthPlugin {
427430
},
428431
});
429432

433+
server.endpoint({
434+
method: 'POST',
435+
path: `/plugin/${this.pluginInstanceId}/get-file-download-url`,
436+
handler: async ({ body, adminUser }) => {
437+
const { filePath } = body;
438+
if (!filePath) {
439+
return { error: 'Missing filePath' };
440+
}
441+
const allowedActions = await interpretResource( adminUser, this.resourceConfig, '', ActionCheckSource.CustomActionRequest, this.adminforth )
442+
if (allowedActions.allowedActions.create === true || allowedActions.allowedActions.edit === true) {
443+
const url = await this.options.storageAdapter.getDownloadUrl(filePath, 1800);
444+
445+
return {
446+
url,
447+
};
448+
}
449+
return { error: 'You do not have permission to download this file' };
450+
},
451+
});
452+
430453
}
431454

432455
}

0 commit comments

Comments
 (0)