You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: attachments.rb
    path of file: ./repos/rubywarden/lib/routes
    
    the code of the file until where you have to start completion: #
# Copyright (c) 2018 joshua stein <jcs@jcs.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SP
    
      def self.registered(app)
        app.namespace BASE_URL do
          post "/ciphers/:uuid/attachment" do
            if !device_from_bearer
              return validation_error("invalid bearer")
            end

            cipher = retrieve_cipher uuid: params[:uuid]

            need_params(:data) do |p|
              return validation_error("#{p} cannot be blank")
            end

            # we have to extract filename from data -> head, since data -> filename is truncated
            filename = nil
            if md = params[:data][:head].match(/filename=\"(\S+)\"/)
              filename = md[1]
            else
              return validation_error("filename cannot be blank")
            end

            file = params[:data][:tempfile]
            attachment_params = { filename: filename,
                                  size: file.size,
                                  file: file.read }
            attachment = cipher.attachments.build_from_params(attachment_params)

            Attachment.transaction do
              if !attachment.save
                return validation_error("error saving")
              end

              cipher.to_hash.to_json
            end