Class: Filepond::Rails::IngressController

Inherits:
ApplicationController show all
Defined in:
app/controllers/filepond/rails/ingress_controller.rb

Instance Method Summary collapse

Instance Method Details

#fetchObject

FilePond calls this endpoint when a URL is dropped onto the upload widget. The server acts as a “proxy” for the client in order to load a file. We then redirect to the actual file which is now served from our servers, and proceed through the usual route.

Note that the implementation below may not be the most efficient. The alternative way would be to directly proxy the request to the URL where the file is originally hosted.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/filepond/rails/ingress_controller.rb', line 15

def fetch
  begin
    # We explicitly declare this for clarity of what is in the
    # raw_post value as sent by FilePond
    uri = URI.parse(raw_post)
    url = uri.to_s

    blob = ActiveStorage::Blob.create_and_upload!(
      io: URI.open(uri),
      filename: URI.parse(url).path.parameterize
    )

    redirect_to ::Rails.application.routes.url_helpers.rails_service_blob_path(
      blob.signed_id,
      blob.filename
    )
  # Why we casting such a wide net with StandardError (TL;DR: too many errors to catch):
  # See https://stackoverflow.com/a/46979718/20551849
  rescue StandardError
    head :unprocessable_entity
  end
end

#removeObject

FilePond calls this endpoint when a user removes (ie. undos) a file upload. This ensures that the blob is removed.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/filepond/rails/ingress_controller.rb', line 40

def remove
  # We explicitly declare this for clarity of what is in the
  # raw_post value, as sent by FilePond
  signed_id = raw_post

  blob = ActiveStorage::Blob.find_signed(signed_id)
  if blob && blob.purge
    head :ok
  else
    # If we cannot find the blob, then we'll just return 404
    head :not_found
  end
end