From 21e2e4050f06058b8220cb859c91fb8aebc3fdaa Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 13 Jan 2025 08:49:15 +0000 Subject: [PATCH 1/2] add uppy URL upload config --- .../stylesheets/entrypoints/application.scss | 4 +++- app/javascript/src/uploads.ts | 2 ++ package.json | 1 + yarn.lock | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/assets/stylesheets/entrypoints/application.scss b/app/assets/stylesheets/entrypoints/application.scss index 1a4b3d6fa..fcee2c8d5 100644 --- a/app/assets/stylesheets/entrypoints/application.scss +++ b/app/assets/stylesheets/entrypoints/application.scss @@ -19,11 +19,13 @@ $ra-font-path: "https://cdn.jsdelivr.net/npm/rpg-awesome@0.2.0/fonts"; @import '@uppy/core/dist/style.min'; @import '@uppy/dashboard/dist/style.min'; +@import '@uppy/url/dist/style.min'; .selectize-control { padding: 1px; } -p.alert:empty, p.notice:empty { +p.alert:empty, +p.notice:empty { display: none; } diff --git a/app/javascript/src/uploads.ts b/app/javascript/src/uploads.ts index c8e6e1d6b..5cc068614 100644 --- a/app/javascript/src/uploads.ts +++ b/app/javascript/src/uploads.ts @@ -1,6 +1,7 @@ import Uppy from '@uppy/core' import Dashboard from '@uppy/dashboard' import Tus from '@uppy/tus' +import Url from '@uppy/url' import en from '@uppy/locales/lib/en_US' import fr from '@uppy/locales/lib/fr_FR' @@ -29,6 +30,7 @@ document.addEventListener('ManyfoldReady', () => { showRemoveButtonAfterComplete: true, hideProgressAfterFinish: true }) + .use(Url, { companionUrl: '/' }) .use(Tus, { endpoint: settings.uploadEndpoint ?? '/upload', chunkSize: 5 * 1024 * 1024 diff --git a/package.json b/package.json index d0ac9b155..a83401c95 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@uppy/form": "^4.1.1", "@uppy/locales": "^4.5.0", "@uppy/tus": "^4.2.1", + "@uppy/url": "^4.2.1", "autoprefixer": "^10.4.20", "bootstrap": "^5.3.3", "bootstrap-icons": "^1.11.3", diff --git a/yarn.lock b/yarn.lock index e878e7035..70e4ba913 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1508,6 +1508,20 @@ __metadata: languageName: node linkType: hard +"@uppy/url@npm:^4.2.1": + version: 4.2.1 + resolution: "@uppy/url@npm:4.2.1" + dependencies: + "@uppy/companion-client": ^4.4.1 + "@uppy/utils": ^6.1.1 + nanoid: ^5.0.9 + preact: ^10.5.13 + peerDependencies: + "@uppy/core": ^4.4.1 + checksum: 2415fe731e3f89d87fe36a8b858308edf3e10bbfc41386371257b0c0fe13a3610cb859a2bb83eecf554b2a455d3010934c18921f2eec297765ac162c6e560d2c + languageName: node + linkType: hard + "@uppy/utils@npm:^6.1.0": version: 6.1.0 resolution: "@uppy/utils@npm:6.1.0" @@ -4287,6 +4301,7 @@ __metadata: "@uppy/form": ^4.1.1 "@uppy/locales": ^4.5.0 "@uppy/tus": ^4.2.1 + "@uppy/url": ^4.2.1 autoprefixer: ^10.4.20 bootstrap: ^5.3.3 bootstrap-icons: ^1.11.3 From 73cbbc6dc3187f7f58cc5db37fb1d720f95c215c Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 13 Jan 2025 08:50:03 +0000 Subject: [PATCH 2/2] routes and working url_meta for URL uploading --- app/controllers/uppy_companion_controller.rb | 21 ++++++++++++++++++++ config/routes.rb | 2 ++ spec/requests/uppy_companion_spec.rb | 18 +++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 app/controllers/uppy_companion_controller.rb create mode 100644 spec/requests/uppy_companion_spec.rb diff --git a/app/controllers/uppy_companion_controller.rb b/app/controllers/uppy_companion_controller.rb new file mode 100644 index 000000000..efe75f60e --- /dev/null +++ b/app/controllers/uppy_companion_controller.rb @@ -0,0 +1,21 @@ +class UppyCompanionController < ApplicationController + skip_forgery_protection + + def url_meta + authorize :model, :new? + # Do HEAD request for url + response = Faraday.head params[:url] + render json: { + name: response.headers["filename"], + type: response.headers["content-type"], + size: response.headers["content-length"], + status_code: response.status + } + end + + def url_get + authorize :model, :new? + Faraday.get params[:url] + render status: :internal_server_error, json: {} + end +end diff --git a/config/routes.rb b/config/routes.rb index db04316dd..d44b10f36 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -124,5 +124,7 @@ authenticate :user, lambda { |u| u.is_contributor? } do mount Tus::Server => "/upload", :as => :upload + post "/url/meta" => "uppy_companion#url_meta" + post "/url/get" => "uppy_companion#url_get" end end diff --git a/spec/requests/uppy_companion_spec.rb b/spec/requests/uppy_companion_spec.rb new file mode 100644 index 000000000..54aa36a10 --- /dev/null +++ b/spec/requests/uppy_companion_spec.rb @@ -0,0 +1,18 @@ +require 'rails_helper' + +RSpec.describe "UppyCompanions", type: :request do + describe "GET /url_meta" do + it "returns http success" do + get "/uppy_companion/url_meta" + expect(response).to have_http_status(:success) + end + end + + describe "GET /url_get" do + it "returns http success" do + get "/uppy_companion/url_get" + expect(response).to have_http_status(:success) + end + end + +end