<commit_before>require "rack"

require "json"

class Shrine
  module Plugins
    # The `presign_endpoint` plugin provides a Rack endpoint which generates
    # the URL, fields, and headers that can be used to upload files directly to
    # a storage service. It can be used with client-side file upload libraries
    # like [FineUploader], [Dropzone] or [jQuery-File-Upload] for asynchronous
    # uploads. Storage services that support direct uploads include [Amazon
    # S3], [Google Cloud Storage], [Microsoft Azure Storage] and more.
    #
    #     plugin :presign_endpoint
    #
    # The plugin adds a `Shrine.presign_endpoint` method which accepts a
    # storage identifier and returns a Rack application that accepts GET
    # requests and generates a presign for the specified storage.
    #
    #     Shrine.presign_endpoint(:cache) # rack app
    #
    # Asynchronous upload is typically meant to replace the caching phase in
    # the default synchronous workflow, so we want to generate parameters for
    # uploads to the temporary (`:cache`) storage.
    #
    # We can mount the returned Rack application inside our application:
    #
    #     Rails.application.routes.draw do
    #       mount Shrine.presign_endpoint(:cache) => "/presign"
    #     end
    #
    # The above will create a `GET /presign` endpoint, which generates presign
    # URL, fields, and headers using the specified storage, and returns it in
    # JSON format.
    #
    #     # GET /presign
    #     {
    #       "url": "https://my-bucket.s3-eu-west-1.amazonaws.com",
    #       "fields": {
    #         "key": "<KEY>",
    #         "policy": "<KEY>",
    #         "x-amz-credential": "AKIAIJF55TMZYT6Q/20151024/eu-west-1/s3/aws4_request",
    #         "x-amz-algorithm": "AWS4-HMAC-SHA256",
    #         "x-amz-date": "20151024T001129Z",
    #         "x-amz-signature": "c1eb634f83f96b69bd675f535b3ff15ae184b102fcba51e4db5f4959b4ae26f4"
    #       },
    #       "headers": {}
    #     }
    #
    # This gives the client all the information it needs to make the upload
    # request to the selected file to the storage service. The `url` field is
    # the request URL, `fields` are the required POST parameters, and `headers`
    # are the required request headers.
    #
    # ## Location
    #
    # By default the generated location won't have any file extension, but you
    # can specify one by sending the `filename` query parameter:
    #
    #     GET /presign?filename=nature.jpg
    #
    # It's also possible to customize how the presign location is generated:
    #
    #     plugin :presign_endpoint, presign_location: -> (request) do
    #       "#{SecureRandom.hex}/#{request.params["filename"]}"
    #     end
    #
    # ## Options
    #
    # Some storages accept additional presign options, which you can pass in via
    # `:presign_options`:
    #
    #     plugin :presign_endpoint, presign_options: -> (request) do
    #       filename     = request.params["filename"]
    #       extension    = File.extname(filename)
    #       content_type = Rack::Mime.mime_type(extension)
    #
    #       {
    #         content_length_range: 0..(10*1024*1024),                     # limit filesize to 10MB
    #         content_disposition: "attachment; filename=\"#{filename}\"", # download with original filename
    #         content_type:        content_type,                           # set correct content type
    #       }
    #     end
    #
    # ## Presign
    #
    # You can also customize how the presign itself is generated via the
    # `:presign` option:
    #
    #     plugin :presign_endpoint, presign: -> (id, options, request) do
    #       # return an object that responds to #url, #fields, and #headers
    #     end
    #
    # ## Response
    #
    # The response returned by the endpoint can be customized via the
    # `:rack_response` option:
    #
    #     plugin :presign_endpoint, rack_response: -> (hash, request) do
    #       body = { endpoint: hash[:url], params: hash[:fields], headers: hash[:headers] }.to_json
    #       [201, { "Content-Type" => "application/json" }, [body]]
    #     end
    #
    # ## Ad-hoc options
    #
    # You can override any of the options above when creating the endpoint:
    #
    #     Shrine.presign_endpoint(:cache, presign_location: "${filename}")
    #
    # [Amazon S3]: https://aws.amazon.com/s3/
    # [Google Cloud Storage]: https://cloud.google.com/storage/
    # [Microsoft Azure Storage]: https://azure.microsoft.com/en-us/services/storage/
    module PresignEndpoint
      def self.configure(uploader, opts = {})
        uploader.opts[:presign_endpoint_presign_location] = opts.fetch(:presign_location, uploader.opts[:presign_endpoint_presign_location])
        uploader.opts[:presign_endpoint_presign_options] = opts.fetch(:presign_options, uploader.opts[:presign_endpoint_presign_options])
        uploader.opts[:presign_endpoint_presign] = opts.fetch(:presign, uploader.opts[:presign_endpoint_presign])
        uploader.opts[:presign_endpoint_rack_response] = opts.fetch(:rack_response, uploader.opts[:presign_endpoint_rack_response])
      end

      module ClassMethods
        # Returns a Rack application (object that responds to `#call`) which
        # accepts GET requests to the root URL, calls the specified storage to<commit_msg>Fix missing links in presign_endpoint documentation<commit_after>require "rack"

require "json"

class Shrine
  module Plugins
    # The `presign_endpoint` plugin provides a Rack endpoint which generates
    # the URL, fields, and headers that can be used to upload files directly to
    # a storage service. It can be used with client-side file upload libraries
    # like [FineUploader], [Dropzone] or [jQuery-File-Upload] for asynchronous
    # uploads. Storage services that support direct uploads include [Amazon
    # S3], [Google Cloud Storage], [Microsoft Azure Storage] and more.
    #
    #     plugin :presign_endpoint
    #
    # The plugin adds a `Shrine.presign_endpoint` method which accepts a
    # storage identifier and returns a Rack application that accepts GET
    # requests and generates a presign for the specified storage.
    #
    #     Shrine.presign_endpoint(:cache) # rack app
    #
    # Asynchronous upload is typically meant to replace the caching phase in
    # the default synchronous workflow, so we want to generate parameters for
    # uploads to the temporary (`:cache`) storage.
    #
    # We can mount the returned Rack application inside our application:
    #
    #     Rails.application.routes.draw do
    #       mount Shrine.presign_endpoint(:cache) => "/presign"
    #     end
    #
    # The above will create a `GET /presign` endpoint, which generates presign
    # URL, fields, and headers using the specified storage, and returns it in
    # JSON format.
    #
    #     # GET /presign
    #     {
    #       "url": "https://my-bucket.s3-eu-west-1.amazonaws.com",
    #       "fields": {
    #         "key": "<KEY>",
    #         "policy": "<KEY>",
    #         "x-amz-credential": "AKIAIJF55TMZYT6Q/20151024/eu-west-1/s3/aws4_request",
    #         "x-amz-algorithm": "AWS4-HMAC-SHA256",
    #         "x-amz-date": "20151024T001129Z",
    #         "x-amz-signature": "<KEY>"
    #       },
    #       "headers": {}
    #     }
    #
    # This gives the client all the information it needs to make the upload
    # request to the selected file to the storage service. The `url` field is
    # the request URL, `fields` are the required POST parameters, and `headers`
    # are the required request headers.
    #
    # ## Location
    #
    # By default the generated location won't have any file extension, but you
    # can specify one by sending the `filename` query parameter:
    #
    #     GET /presign?filename=nature.jpg
    #
    # It's also possible to customize how the presign location is generated:
    #
    #     plugin :presign_endpoint, presign_location: -> (request) do
    #       "#{SecureRandom.hex}/#{request.params["filename"]}"
    #     end
    #
    # ## Options
    #
    # Some storages accept additional presign options, which you can pass in via
    # `:presign_options`:
    #
    #     plugin :presign_endpoint, presign_options: -> (request) do
    #       filename     = request.params["filename"]
    #       extension    = File.extname(filename)
    #       content_type = Rack::Mime.mime_type(extension)
    #
    #       {
    #         content_length_range: 0..(10*1024*1024),                     # limit filesize to 10MB
    #         content_disposition: "attachment; filename=\"#{filename}\"", # download with original filename
    #         content_type:        content_type,                           # set correct content type
    #       }
    #     end
    #
    # ## Presign
    #
    # You can also customize how the presign itself is generated via the
    # `:presign` option:
    #
    #     plugin :presign_endpoint, presign: -> (id, options, request) do
    #       # return an object that responds to #url, #fields, and #headers
    #     end
    #
    # ## Response
    #
    # The response returned by the endpoint can be customized via the
    # `:rack_response` option:
    #
    #     plugin :presign_endpoint, rack_response: -> (hash, request) do
    #       body = { endpoint: hash[:url], params: hash[:fields], headers: hash[:headers] }.to_json
    #       [201, { "Content-Type" => "application/json" }, [body]]
    #     end
    #
    # ## Ad-hoc options
    #
    # You can override any of the options above when creating the endpoint:
    #
    #     Shrine.presign_endpoint(:cache, presign_location: "${filename}")
    #
    # [FineUploader]: https://github.com/FineUploader/fine-uploader
    # [Dropzone]: https://github.com/enyo/dropzone
    # [jQuery-File-Upload]: https://github.com/blueimp/jQuery-File-Upload
    # [Amazon S3]: https://aws.amazon.com/s3/
    # [Google Cloud Storage]: https://cloud.google.com/storage/
    # [Microsoft Azure Storage]: https://azure.microsoft.com/en-us/services/storage/
    module PresignEndpoint
      def self.configure(uploader, opts = {})
        uploader.opts[:presign_endpoint_presign_location] = opts.fetch(:presign_location, uploader.opts[:presign_endpoint_presign_location])
        uploader.opts[:presign_endpoint_presign_options] = opts.fetch(:presign_options, uploader.opts[:presign_endpoint_presign_options])
        uploader.opts[:presign_endpoint_presign] = opts.fetch(:presign, uploader.opts[:presign_endpoint_presign])
        uploader.opts[:presign_endpoint_rack_response] = opts.fetch(:rack_response, uploader.opts[:presign_endpoint_rack_response])
      end

      module ClassMethods
        # Returns a Rack application (object that responds to `#call`) which
        # accepts GET requests to the root URL, calls the specified storage to
{'fraction_non_alphanumeric': 0.10503583900018379, 'fraction_numerical': 0.01314096673405624, 'mean_word_length': 2.619221815763219, 'pattern_counts': {'":': 18, '<': 8, '<?xml version=': 0, '>': 20, 'https://': 11, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '807485', 'n_tokens_mistral': 3287, 'n_tokens_neox': 3147, 'n_words': 916}