-
Notifications
You must be signed in to change notification settings - Fork 0
feat: allow to upload file directry from the url query param #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -103,6 +103,8 @@ const progress = ref(0); | |||
| const uploaded = ref(false); | ||||
| const uploadedSize = ref(0); | ||||
|
|
||||
| const downloadFileUrl = ref(''); | ||||
|
|
||||
| watch(() => uploaded, (value) => { | ||||
| emit('update:emptiness', !value); | ||||
| }); | ||||
|
|
@@ -118,9 +120,45 @@ function uploadGeneratedImage(imgBlob) { | |||
| }); | ||||
| } | ||||
|
|
||||
| onMounted(() => { | ||||
| onMounted(async () => { | ||||
| const previewColumnName = `previewUrl_${props.meta.pluginInstanceId}`; | ||||
| if (props.record[previewColumnName]) { | ||||
|
|
||||
| let queryValues; | ||||
| try { | ||||
| queryValues = JSON.parse(atob(route.query.values as string)); | ||||
| } catch (e) { | ||||
| queryValues = {}; | ||||
| } | ||||
|
|
||||
| if (queryValues[props.meta.pathColumnName]) { | ||||
|
|
||||
|
|
||||
yaroslav8765 marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+134
to
+135
|
||||
yaroslav8765 marked this conversation as resolved.
Show resolved
Hide resolved
yaroslav8765 marked this conversation as resolved.
Show resolved
Hide resolved
yaroslav8765 marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import { PluginOptions } from './types.js'; | |
| import { AdminForthPlugin, AdminForthResourceColumn, AdminForthResource, Filters, IAdminForth, IHttpServer, suggestIfTypo } from "adminforth"; | ||
| import { Readable } from "stream"; | ||
| import { RateLimiter } from "adminforth"; | ||
| import { url } from 'inspector/promises'; | ||
yaroslav8765 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const ADMINFORTH_NOT_YET_USED_TAG = 'adminforth-candidate-for-cleanup'; | ||
|
|
||
|
|
@@ -86,6 +87,7 @@ export default class UploadPlugin extends AdminForthPlugin { | |
| minShowWidth: this.options.preview?.minShowWidth, | ||
| generationPrompt: this.options.generation?.generationPrompt, | ||
| recorPkFieldName: this.resourceConfig.columns.find((column: any) => column.primaryKey)?.name, | ||
| pathColumnName: this.options.pathColumnName, | ||
| }; | ||
| // define components which will be imported from other components | ||
| this.componentPath('imageGenerator.vue'); | ||
|
|
@@ -411,6 +413,51 @@ export default class UploadPlugin extends AdminForthPlugin { | |
| }, | ||
| }); | ||
|
|
||
| server.endpoint({ | ||
| method: 'POST', | ||
| path: `/plugin/${this.pluginInstanceId}/get-file-download-url`, | ||
| handler: async ({ body, adminUser }) => { | ||
| const { filePath } = body; | ||
| if (!filePath) { | ||
| return { error: 'Missing filePath' }; | ||
| } | ||
| const url = await this.options.storageAdapter.getDownloadUrl(filePath, 1800); | ||
|
|
||
yaroslav8765 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return { | ||
| url, | ||
| }; | ||
yaroslav8765 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
|
Comment on lines
+418
to
+428
|
||
| }); | ||
|
|
||
| server.endpoint({ | ||
| method: 'POST', | ||
| path: `/plugin/${this.pluginInstanceId}/proxy-download`, | ||
| handler: async ({ body, response }) => { | ||
| const { fileDownloadURL } = body; | ||
|
|
||
| if (!fileDownloadURL) { | ||
| return { error: 'Missing fileDownloadURL' }; | ||
| } | ||
|
|
||
yaroslav8765 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const upstream = await fetch(fileDownloadURL); | ||
| if (!upstream.ok || !upstream.body) { | ||
| return { error: `Failed to download file (status ${upstream.status})` }; | ||
| } | ||
|
|
||
| const contentType = upstream.headers.get('content-type') || 'application/octet-stream'; | ||
| const contentLength = upstream.headers.get('content-length'); | ||
| const contentDisposition = upstream.headers.get('content-disposition'); | ||
|
|
||
| response.setHeader('Content-Type', contentType); | ||
| if (contentLength) response.setHeader('Content-Length', contentLength); | ||
| if (contentDisposition) response.setHeader('Content-Disposition', contentDisposition); | ||
|
|
||
| //@ts-ignore Node 18+: convert Web stream to Node stream and pipe to response | ||
| Readable.fromWeb(upstream.body).pipe(response.blobStream()); | ||
| return null; | ||
yaroslav8765 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }); | ||
|
|
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
watchfunction is incorrectly watching the entireuploadedref object instead of its value. It should bewatch(uploaded, (value) => ...)orwatch(() => uploaded.value, (value) => ...)to properly react to changes in the uploaded state.