I've got this working, but it's very slow (to the point of timing out) for even a dozen or so files.
It grabs a dir listing from dropbox, and compares that to the contents of a table. I'd like to optimize this so that it would run as fast and efficiently as possible. I know querying every time is not optimal, but i think the major delay is during the Photo.create
method since that is where it copies the file from the dropbox folder to Amazon S3 (via carrierwave gem
). I am looking into putting a time on the operation to see where the delay is coming from. for a folder with 10 files, it takes well over a minute to load the page. The strange thing is that it takes this long even if it skips those files because they already exist, which makes no sense to me.
Here's my controller code:
def sync
photo_size = 1024
@event = Event.find(params[:id])
@client = Dropbox::API::Client.new(:token => 'derp', :secret => 'herp')
@dropbox_files = @client.ls "images/#{@event.keyword}/#{photo_size}/"
@existing_photos = @event.photos.all
@data = []
# TODO: need to make it not add files multiple times
@dropbox_files.each do |f|
photo_exists = Photo.where(:dropbox_path => f.direct_url.url).count
if photo_exists == 0
@photo = Photo.create(:remote_filename_url => f.direct_url.url,
:dropbox_path => f.direct_url.url,
:event_id => @event.id)
@data << "Added: #{f.direct_url.url.split('/').last}"
else
@data << "Skipped: #{f.direct_url.url.split('/').last}"
end
end
end
Ideally, i'd like to separate each Photo.create
call into an async request, but that might be a whole 'notha thing. For now, i'd be happy if it was something that could handle adding 5 photos out of a list of 100 without timing out.
what is the best way to do this? I'm a PHP programmer that's new to RoR3. Please help. thanks!
one note: for now, this outputs to a screen, but eventually it will be a background action.