From bf98497711bb521816916eaad3b788ad7c67ed91 Mon Sep 17 00:00:00 2001 From: Julien Nahum Date: Sat, 13 Jan 2024 18:17:24 +0100 Subject: [PATCH] Fix local file upload --- .../Content/FileUploadController.php | 1 + app/Providers/RouteServiceProvider.php | 14 ------------ .../open/forms/components/FormEditor.vue | 1 - client/composables/useCrisp.js | 2 +- client/composables/useOpnApi.js | 9 +++++++- client/lib/file-uploads.js | 22 ++++++------------- client/nuxt.config.ts | 1 + routes/api.php | 2 -- routes/web.php | 14 ------------ 9 files changed, 18 insertions(+), 48 deletions(-) delete mode 100644 routes/web.php diff --git a/app/Http/Controllers/Content/FileUploadController.php b/app/Http/Controllers/Content/FileUploadController.php index 62e6423..3481d8c 100644 --- a/app/Http/Controllers/Content/FileUploadController.php +++ b/app/Http/Controllers/Content/FileUploadController.php @@ -17,6 +17,7 @@ class FileUploadController extends Controller */ public function upload(Request $request) { + $request->validate(['file' => 'required|file']); $uuid = (string) Str::uuid(); $path = $request->file('file')->storeAs(PublicFormController::TMP_FILE_UPLOAD_PATH, $uuid); diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index b9d4a78..989bcff 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -19,15 +19,6 @@ class RouteServiceProvider extends ServiceProvider */ public const HOME = '/home'; - /** - * The controller namespace for the application. - * - * When present, controller route declarations will automatically be prefixed with this namespace. - * - * @var string|null - */ - // protected $namespace = 'App\\Http\\Controllers'; - /** * Define your route model bindings, pattern filters, etc. * @@ -39,14 +30,9 @@ class RouteServiceProvider extends ServiceProvider $this->registerGlobalRouteParamConstraints(); $this->routes(function () { - Route::middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); - - Route::middleware('web') - ->namespace($this->namespace) - ->group(base_path('routes/web.php')); }); } diff --git a/client/components/open/forms/components/FormEditor.vue b/client/components/open/forms/components/FormEditor.vue index d2d2145..953bfb8 100644 --- a/client/components/open/forms/components/FormEditor.vue +++ b/client/components/open/forms/components/FormEditor.vue @@ -221,7 +221,6 @@ export default { methods: { displayFormModificationAlert (responseData) { const alert = useAlert() - console.log("in okd ----.", responseData) if (responseData.form && responseData.form.cleanings && Object.keys(responseData.form.cleanings).length > 0) { alert.warning(responseData.message) } else if (responseData.message) { diff --git a/client/composables/useCrisp.js b/client/composables/useCrisp.js index 29be4c9..47ac053 100644 --- a/client/composables/useCrisp.js +++ b/client/composables/useCrisp.js @@ -58,7 +58,7 @@ export const useCrisp = () => { }); if (user?.is_subscribed ?? false) { - setSegments([['subscribed', user?.has_enterprise_subscription ? 'enterprise' : 'pro']]) + setSegments(['subscribed', user?.has_enterprise_subscription ? 'enterprise' : 'pro']) } } diff --git a/client/composables/useOpnApi.js b/client/composables/useOpnApi.js index 77bcd7e..fc65e86 100644 --- a/client/composables/useOpnApi.js +++ b/client/composables/useOpnApi.js @@ -9,7 +9,7 @@ function addAuthHeader(request, options) { function addPasswordToFormRequest(request, options) { const url = request.url - if (!url || !url.startsWith('/api/forms/')) return + if (!url || !url.startsWith('/forms/')) return const slug = url.split('/')[3] const passwordCookie = useCookie('password-' + slug, {maxAge: 60 * 60 * 24 * 30}) // 30 days @@ -29,6 +29,13 @@ function addCustomDomainHeader(request, options) { export function getOpnRequestsOptions(request, opts) { const config = useRuntimeConfig() + if (opts.body && opts.body instanceof FormData) { + opts.headers = { + 'charset': 'utf-8', + ...opts.headers, + } + } + opts.headers = {accept: 'application/json', ...opts.headers} // Authenticate requests coming from the server diff --git a/client/lib/file-uploads.js b/client/lib/file-uploads.js index 5fb813e..1b3c2ab 100644 --- a/client/lib/file-uploads.js +++ b/client/lib/file-uploads.js @@ -6,9 +6,6 @@ export const storeFile = async (file, options = {}) => { formData.append('file', file) const response = await useOpnApi('/upload-file', { method: 'POST', - headers: { - 'Content-Type': 'multipart/form-data' - }, body: formData }) response.data.extension = file.name.split('.').pop() @@ -26,7 +23,8 @@ export const storeFile = async (file, options = {}) => { headers: options.headers || {}, ...options.options }) - console.log("response.data",response.data) + + console.log(response) const headers = response.data.headers @@ -38,20 +36,14 @@ export const storeFile = async (file, options = {}) => { options.progress = () => {} } - const cancelToken = options.cancelToken || '' - - // Remove authorization headers - const cleanAxios = axios.create() - cleanAxios.defaults.headers.common = {} - await cleanAxios.put(response.data.url, file, { - cancelToken: cancelToken, + // Upload to S3 + await useFetch(response.data.url,{ + method: 'PUT', + body: file, headers: headers, - onUploadProgress: (progressEvent) => { - options.progress(progressEvent.loaded / progressEvent.total) - } }) response.data.extension = file.name.split('.').pop() return response.data -} \ No newline at end of file +} diff --git a/client/nuxt.config.ts b/client/nuxt.config.ts index 18c84cd..8e091f2 100644 --- a/client/nuxt.config.ts +++ b/client/nuxt.config.ts @@ -32,6 +32,7 @@ export default defineNuxtConfig({ }, sentry: { dsn: process.env.NUXT_PUBLIC_SENTRY_DSN, + lazy: true, }, components: [ { diff --git a/routes/api.php b/routes/api.php index 418156d..18ae52a 100644 --- a/routes/api.php +++ b/routes/api.php @@ -181,8 +181,6 @@ Route::prefix('templates')->group(function () { Route::delete('/{id}', [TemplateController::class, 'destroy'])->name('templates.destroy'); }); - - Route::post( '/stripe/webhook', [\App\Http\Controllers\Webhook\StripeController::class, 'handleWebhook'] diff --git a/routes/web.php b/routes/web.php deleted file mode 100644 index 94febc7..0000000 --- a/routes/web.php +++ /dev/null @@ -1,14 +0,0 @@ -