Carrierwave Programatic Upload

2020-06-25 05:36发布

问题:

Right now in my rails app I'm using Carrierwave to upload files to Amazon S3. I'm using a file selector and a form to select and submit the file, this works well.

However, I'm now trying to make posts from an iPhone app and am receiving the contents of the file. I'd like to create a file using this data and then upload it using Carrierwave so that I can get the correct path back.

May file model consists of:

path
file_name
id
user_id

where path is the Amazon S3 url. I'd like to do something like this to build the files:

 data = params[:data]
~file creation magic using data~
~carrierwave upload magic using file~
@user_id = params[:id]
@file_name = params[:name]
@path = path_provided_by_carrierwave_magic
File.build(@user_id, @file_name, @path)

Would really love someone to point me in the right direction. Thanks!

回答1:

Here is what I wrote to perform an upload to s3 from an ios application through carrierwave :

First the Photo model

class Photo
  include Mongoid::Document
  include Mongoid::Timestamps

  mount_uploader :image, PhotoImageUploader

  field :title, :type => String
  field :description, :type => String
end

Second in the Api::V1::PhotosController

def create
    @photo = current_user.photos.build(params)
    if @photo.save
        render :json => @photo.to_json, :status=>201
    else
        render :json => {:errors => @photo.errors}.to_json, :status=>403
    end
end

Then the call from my iPhone application using AFNetworking

-(void) sendNewPhoto
{
    NSURL *url = [NSURL URLWithString:@"http://myserverurl.com"];

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:_photoTitle.text, @"title", _photoDescription.text, @"description",nil];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

    NSString *endUrl = [NSString stringWithFormat:@"/api/v1/photos?auth_token=%@", [[User sharedInstance] token]];

    NSData *imageData = UIImageJPEGRepresentation(_photo.image, 1.0);
    NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:endUrl parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"image" fileName:@"image.jpg" mimeType:@"image/jpg"];
    }];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSLog(@"%@", JSON);
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Error creating photo!");
        NSLog(@"%@", error);
    }];

    [operation start];
}

In the JSON response I can get the new instance of Photo with the image.url attribute set to the url in the s3.



回答2:

Alright, I have a working solution. I'm going to best explain what I did so that others can learn from my experience. Here goes:

Assuming you have an iPhone app that takes a picture:

//handle the image that has just been selected
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //get the image
    UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];

    //scale and rotate so you're not sending a sideways image -> method provided by http://blog.logichigh.com/2008/06/05/uiimage-fix/
    image = [self scaleAndRotateImage:image];

    //obtain the jpeg data (.1 is quicker to send, i found it better for testing)
    NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, .1)];

    //get the data into a string
    NSString* imageString = [NSString stringWithFormat:@"%@", imageData];
    //remove whitespace from the string
    imageString = [imageString stringByReplacingOccurrencesOfString:@" " withString:@""];
    //remove < and > from string
    imageString = [imageString substringWithRange:NSMakeRange(1, [imageString length]-2)];

    self.view.hidden = YES;
    //dismissed the camera
    [picker dismissModalViewControllerAnimated:YES];

    //posts the image
    [self performSelectorInBackground:@selector(postImage:) withObject:imageString];
}

- (void)postImage:(NSString*)imageData
{
    //image string formatted in json
   NSString* imageString = [NSString stringWithFormat:@"{\"image\": \"%@\", \"authenticity_token\": \"\", \"utf8\": \"✓\"}", imageData];

    //encoded json string
    NSData* data = [imageString dataUsingEncoding:NSUTF8StringEncoding];

    //post the image
    [API postImage:data];
}[/code]

Then for the post:

[code]+(NSArray*)postImage:(NSData*) data
{
    //url that you're going to send the image to
    NSString* url = @"www.yoururl.com/images";

    //pretty self explanatory request building
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];

    [request setTimeoutInterval:10000];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    [request setHTTPMethod: @"POST"];

    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [request setHTTPBody:data];

    NSError *requestError;
    NSURLResponse *urlResponse = nil;

    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];

    return [API generateArrayWithData:result];
}

On the rails side I set up a method specifically for handling mobile images, this should help you post the image to your Amazon S3 account through Carrierwave:

def post
      respond_to do |format|
  format.json {
 #create a new image so that you can call it's class method (a bit hacky, i know)
  @image = Image.new
#get the json image data
  pixels = params[:image]
#convert it from hex to binary
pixels = @image.hex_to_string(pixels)
#create it as a file
   data = StringIO.new(pixels)
#set file types
    data.class.class_eval { attr_accessor :original_filename, :content_type }
    data.original_filename = "test1.jpeg"
    data.content_type = "image/jpeg"
#set the image id, had some weird behavior when i didn't
    @image.id = Image.count + 1
#upload the data to Amazon S3
  @image.upload(data)
#save the image
  if @image.save!
  render :nothing => true
  end
  }
  end
  end

This works for me for posting and I feel should be pretty extendable. For the class methods:

#stores the file
  def upload(file)
  self.path.store!(file)
  end

#converts the data from hex to a string -> found code here http://4thmouse.com/index.php/2008/02/18/converting-hex-to-binary-in-4-languages/
  def hex_to_string(hex)
    temp = hex.gsub("\s", "");
    ret = []
    (0...temp.size()/2).each{|index| ret[index] = [temp[index*2, 2]].pack("H2")}
    file = String.new
    ret.each { |x| file << x}
    file  
  end

Not saying this code is perfect, not even by a longshot. However, it does work for me. I'm open to suggestions if anyone thinks it could be improved. Hope this helps!