从iOS上传到Rails的PDF已损坏

时间:2012-08-02 21:47:34

标签: ruby-on-rails-3 cocoa-touch paperclip content-type afnetworking

我正在将一个pdf文件从iOS应用程序上传到Rails应用程序。 文件已上传,但其内容类型已损坏。

以下是上传文件的iOS代码的相关部分(使用AFNetworking库):

NSURL *url = [NSURL URLWithString:kWebserviceHost];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
//    httpClient.parameterEncoding = AFFormURLParameterEncoding; // Experimented with different values, no effect

NSData *documentData = [NSData dataWithContentsOfURL:self.documentURL];
if (! documentData) {
    NSLog(@"Trying to upload nil document: sorry! - %@", self.documentData);
    return;
}

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"PUT" // @"POST" 
                                                                     path:@"new_upload"
                                                               parameters:@{ @"fooKey" : fooString, @"barKey" : barString }
                                                constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:documentData name:@"document" fileName:@"upload.pfd" mimeType:@"application/pdf"];
}];


AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
    progressBlock(totalBytesWritten / totalBytesExpectedToWrite);
}];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    uploadSuccessBlock();
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Sorry, could not upload document - %@", error);
    failureBlock(error);
}];
[operation start];

让我们走到另一边,一个Rails 3.2.3服务器 我正在使用宝石PaperClip(版本3.1.4)。以下是我的模型类中回形针附件的定义:

paperclip_storage = (Rails.env.production?) ? {
                       :storage        => :s3,
                       :s3_credentials => "#{Rails.root}/config/s3.yml",
                       :path           => ':attachment/:id/:style.:extension',
                       :bucket         => 'mybucketname' #,
                       # :s3_headers =>  { "Content-Type" => "video/mp4" }
                        } 
                     : { :headers =>  { "Content-Type" => "application/pdf" }
                     }

has_attached_file :document, paperclip_storage.merge( :use_timestamp => false,
  :url  => "/assets/:id/:basename.:extension",
  :path => ":rails_root/public/assets/:id/:basename.:extension",
  :processors => nil # Maybe the processing of the file mess things up?
)

我也尝试跳过后期处理(即使文档说没有应用,因为没有定义样式)。

before_post_process :skip_post_process

def skip_post_process
  return false
end

另一种尝试:在post_process过滤器中设置content_type:

after_post_process :force_content_type_to_pdf
 def force_content_type_to_pdf
   puts "--------------- Changing content_type"
   self.document.instance_write(:content_type, "application/pdf")
 end

以下是接收和保存文件的控制器方法:

def new_upload

  @document = Document.create()
  document = params[:document]

  if (document.nil?) 
    return
  end

  @document.document = document

  puts "Document type: #{@document.document.content_type}" # logs: 'application/pdf'
  puts "Parameters: #{params}" # logs     Parameters: {"fooKey"=>"foo", "barKey"=>"bar", "document"=>#<ActionDispatch::Http::UploadedFile:0x007ff5a429be58 @original_filename="upload.pfd", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"document\"; filename=\"upload.pfd\"\r\nContent-Type: application/pdf\r\n", @tempfile=#<File:/var/folders/vy/zm_x1bts5hs6pkvzk7clnmvm0000gn/T/RackMultipart20120802-12714-1j0hq6q>>}


  @document.foo = params['fooKey']
  @document.bar = params['barKey']

  if    @document.save
    render :json => {status: "saved" }
    return
  else 
      render json: @document.errors, status: :unprocessable_entity 
      return
  end
end

文件已上传,但无法读取。 Finder不知道使用哪个应用程序打开它 以下是QuickLook预览的屏幕截图: QuickLooking the file

内容类型以某种方式混淆。
file --mime /file/path/before/or/after/upload.pdf在原始文件(iOS应用程序将上传的文件)或成功上传后由Rails服务器创建的文件上返回以下消息:
/upload.pfd: application/pdf; charset=binary。到目前为止听起来不错。

mdls -name kMDItemContentType /file/path/before/upload.pdf会在要上传的文件上返回kMDItemContentType = "com.adobe.pdf"。还好。 但是Rails服务器创建的文件上的相同命令返回:kMDItemContentType = "dyn.ah62d4rv4ge81a3xe" 这至少解释了为什么Finder感到困惑。

在浏览器中下载文件时问题类似。这是我的控制器的相关方法:

def download_document
  @document = Document.find(params[:id])

  if (@document && @document.document) 
    # Line below propagate the file content-type problem:
    send_file Rails.root.join(document.path), :type => "application/pdf", :x_sendfile => false, :stream => false

    # This hack works while server is locally hosted
    send_data File.read(Rails.root.join(@document.document.path)),  :type => "application/pdf"
  end
end

不幸的是,在S3上托管时黑客攻击不行。并且它阻止我在调试我的iOS应用程序时方便地浏览我的文件系统以查看上传的文档 通过FTP客户端(例如Transmit)直接从S3下载文件会得到同样的损坏文件。

有什么问题?你认为我应该在哪里进一步排除故障,客户端或服务器?怎么样?有任何想法,提示,预感要断言或看待?

如果您没有任何解决方案,我也很高兴获得临时修复,以便在下载后再次正确设置损坏文件中的content_type(或任何问题)。

1 个答案:

答案 0 :(得分:3)

如果您将上传的文件命名为“upload.pdf”而不是“upload.pdf”会怎样?