0
votes

I have an Android client that hopes to send a file to my Rails server. I'm using Paperclip Gem for rails and this is the error that I'm getting at in my WebRick console:

Started POST "/logs" for 192.168.63.142 at 2015-07-23 16:51:20 +0800 ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations" Processing by LogsController#create as HTML Parameters: {"log"=>{"description"=>"Description", "user_id"=>"1", "file"=>#, @original_filename="bugreport-1hour-head.txt", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"log[file]\"; filename=\"bugreport-1hour-head.txt\"\r\nContent-Type: application/octet-stream\r\n">}} Command :: file -b --mime '/tmp/9859aa87e4cbf0f33fd178012d8819b720150723-8057-lwvwns.txt' [paperclip] Content Type Spoof: Filename bugreport-1hour-head.txt (application/octet-stream from Headers, [#] from Extension), content type discovered from file command: text/plain. See documentation to allow this combination. (0.1ms) begin transaction Command :: file -b --mime '/tmp/9859aa87e4cbf0f33fd178012d8819b720150723-8057-1ndzzr1.txt' [paperclip] Content Type Spoof: Filename bugreport-1hour-head.txt (application/octet-stream from Headers, [#] from Extension), content type discovered from file command: text/plain. See documentation to allow this combination. (0.2ms) rollback transaction Rendered logs/_form.html.erb (16.6ms) Rendered logs/new.html.erb within layouts/application (21.6ms) Completed 200 OK in 656ms (Views: 306.1ms | ActiveRecord: 0.7ms)

It says

content type discovered from file command: text/plain. See documentation to allow this combination.

But I already allowed content-type text/plain in my model below

Android code:

 mSendLogs.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String url = "http://192.168.63.145:3000/logs";
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
                    "bugreport-1hour-head.txt");
            try {
                HttpClient httpclient = new DefaultHttpClient();

                HttpPost httppost = new HttpPost(url);
                MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                multipartEntity.addPart("log[description]", new StringBody("Description"));
                multipartEntity.addPart("log[user_id]", new StringBody("1"));
                multipartEntity.addPart("log[file]", new FileBody(file) );
                httppost.setEntity(multipartEntity);

                HttpResponse response = httpclient.execute(httppost);
                String statusCode = response.getEntity().getContent().toString();
                Log.d("Benggal", "http.fr.server: " + statusCode + "Upload Logs");

            } catch (Exception e) {
                Log.e("Benggal",e.toString());
            }
        }
    });

Rails Model With Paperclip Values:

class Log < ActiveRecord::Base
has_attached_file :file
validates_attachment_content_type :file, :content_type => "text/plain"

end

Rails Controller's Action For Saving:

 # POST /logs
  # POST /logs.json
  def create
    @log = Log.new(log_params)

    respond_to do |format|
      if @log.save
        format.html { redirect_to @log, notice: 'Log was successfully created.' }
        format.text {@log.file.url}
        format.json { render :show, status: :created, location: @log }
      else
        format.html { render :new }
        format.json { render json: @log.errors, status: :unprocessable_entity }
      end
    end
  end
2
Looks like android is sending it as application/octet-stream, not text/plain and that mismatch causes an exception.katafrakt
Man, you're right! I edited the file entity to multipartEntity.addPart("log[file]", new FileBody(file,"text/plain") ); It worked! Please make it an answer, i'll accept it.Jezer Crespo

2 Answers

4
votes

i think this message is raised by a validation check for content spoofing. For Paperclip v.4 this generates a bug [https://github.com/thoughtbot/paperclip/issues/1429][1]

While for Paperclip v.3, it seems it just throws a deprecation warning, [https://github.com/thoughtbot/paperclip/issues/1423][2]

so either ..use this in Gemfile

 gem "paperclip", "~> 3.5.3"

OR

add this to an initializer to disable spoofing protection:

config/initializers/paperclip_media_type_spoof_detector_override.rb

require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

OR

you can specify mime type created a paperclip.rb file in config/initializers.

Paperclip.options[:content_type_mappings] = {
  :txt=> 'application/octet-stream'
}
2
votes

Your Android client sends a file as application/octet-steam instead of text/plain and that causes Paperclip's confusion, leading to this exception. You probably have to fix the client to make it work.