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 abf7ee3

Browse files
committed
feat: use fetch for the file download instead of using proxy download
1 parent 2e1ee72 commit abf7ee3

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

custom/uploader.vue

Lines changed: 48 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,53 @@ 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'),
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+
if (!file) {
162+
return;
163+
}
164+
onFileChange({
165+
target: {
166+
files: [file],
167+
},
168+
});
169+
} else if (props.record[previewColumnName]) {
124170
imgPreview.value = props.record[previewColumnName];
125171
uploaded.value = true;
126172
emit('update:emptiness', false);

index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export default class UploadPlugin extends AdminForthPlugin {
8686
minShowWidth: this.options.preview?.minShowWidth,
8787
generationPrompt: this.options.generation?.generationPrompt,
8888
recorPkFieldName: this.resourceConfig.columns.find((column: any) => column.primaryKey)?.name,
89+
pathColumnName: this.options.pathColumnName,
8990
};
9091
// define components which will be imported from other components
9192
this.componentPath('imageGenerator.vue');
@@ -427,6 +428,22 @@ export default class UploadPlugin extends AdminForthPlugin {
427428
},
428429
});
429430

431+
server.endpoint({
432+
method: 'POST',
433+
path: `/plugin/${this.pluginInstanceId}/get-file-download-url`,
434+
handler: async ({ body, adminUser }) => {
435+
const { filePath } = body;
436+
if (!filePath) {
437+
return { error: 'Missing filePath' };
438+
}
439+
const url = await this.options.storageAdapter.getDownloadUrl(filePath, 1800);
440+
441+
return {
442+
url,
443+
};
444+
},
445+
});
446+
430447
}
431448

432449
}

0 commit comments

Comments
 (0)