Change store_dir, move old files to new path

2,175 views
Skip to first unread message

RB

unread,
Jan 29, 2012, 12:51:47 AM1/29/12
to carrierwave
I need to change the store_dir for my Carrierwave uploader.
Unfortunately this will break the paths of all existing files stored
in the old store_dir. What is the safest way to copy/move these old
images to the new store_dir path and update their filenames (the old
filenames are not unique)?



RB

unread,
Feb 5, 2012, 11:46:37 AM2/5/12
to carrierwave
Update - I wrote a rake task that treated the files in the old
location as remote files & uploaded them to the new location
(recreating all versions). Like this:

model.remote_image_url = old_image_url

Marcel Fahle

unread,
Mar 6, 2012, 11:23:53 AM3/6/12
to carri...@googlegroups.com
Hey, 

do you have a few more details on your solution? I think I have a similar problem, but can't figure it out.

Thanks a lot!!

Marcel

Hans

unread,
Sep 14, 2012, 1:01:04 AM9/14/12
to carri...@googlegroups.com
I have done that by a rake task that moved all files from one dir to the dir  defined in store dir, changed their names and stored the filename in the uploader column.
However the rake task should not define any uploader to be able to work as described above.

Den torsdagen den 13:e september 2012 kl. 04:11:48 UTC+2 skrev Bayarja:
 RB did you solve that? i wanna change stored director and rename stored files. how did you do it? carrierwave or rake task which one is better?

Bayarja

unread,
Sep 14, 2012, 1:08:57 AM9/14/12
to carri...@googlegroups.com
if is it possible please you can send me solved code. sorry my english skill

Hans Marmolin

unread,
Sep 14, 2012, 5:28:23 AM9/14/12
to carri...@googlegroups.com
Here it comes. 
This is the part with file handling that I call from a rake task that creates appropriate objects

=========================


def handle_my_document(document,kind,user,my_file,count=0) 
  file_name=my_file.URL
  forum=user.forum
  puts'>>>>>'+'Handling shared document file '+file_name.to_s+' for user '+user.nil_or.id.to_s
  filepath="#{Rails.root}/public/forum_shared_files/"
  if !forum.nil? then forum='Forum_'+forum.id.to_s else forum='Forum_???' end  
  new_filepath="/Users/hansmarmolin/Projekt/Forum/st_forum/public/uploads/document/file/#{forum}/#{document.id}/"
  source=filepath+file_name
  dest=new_filepath+file_name
  new_name=sanitize_my_document_name(my_file,new_filepath)
  if File.exists?(source)
    file_size=File.size(source)     
    FileUtils.mkdir_p(File.dirname(dest)) 
    FileUtils.cp source, dest 
    File.rename(new_filepath+file_name,new_filepath+new_name)
    count+=1
  else
    puts'**** '+'File '+source.to_s+' '+'does not exists'
  end
  return count, new_name, file_size
end


def sanitize_my_document_name(my_document,new_filepath)
  name=truncate(my_document.title,40).clean
  if name.blank? then name=truncate(my_document.URL, 40).clean end
  name=name.gsub(' ','_')
  name=name.gsub('"','')
  name=name.gsub(' - ','-').gsub(' -','-').gsub('- ','-') 
  name=name.gsub(/[^[:word:]\.\-\+]/,"_")
  if !my_document.URL.include?('.') then name+'_'+my_document.id.to_s+'.???' 
  else name=name+'.'+my_document.URL.split('.').last end
  name=name.gsub('__','_')
  name=name.gsub('_-_','-')       
  return name
end

def truncate(text, chars = 30)
    return if text.nil?
    return text if text.length<=chars 
    words=text.split(' ')
    for n in (1..words.size) 
      if words[0..n].join(' ').length>chars then break end
    end
     if words[0..n].join(' ').length-chars>chars-words[0..n-1].join(' ').length then n=n-1 end
     result=words[0..n].join(' ')
  end

===================
Here are the part of the rake task that calls the methods above

       puts'>>>>>'+'Creating my_document '+my_file.URL
        if my_file.Uploaded_by==0 then uploader=my_file.created_by else uploader=my_file.Uploaded_by end
        user=find_user(uploader)
        if user.nil? then next end
       document=Document.create
       filecount,new_name, file_size=handle_my_document(document,'user_document',user,my_file,filecount)     
       document.update_attributes(:title=>my_file.title,
                       :kind=>'user_document',
                       :file=>new_name,
                       :file_size=>file_size,
                       :forum_id=>user.forum.nil_or.id,:description=>my_file.Info,:show_for=>user.user_preference.show_documents,:old_document_id=>my_file.id,
                       :created_by_id=>user.nil_or.id,
                       :updated_by_id=>user.nil_or.id,
                       :created_at=>my_file.created,
                       :updated_at=>my_file.edited)                                 
     end
____________________________________________
Hans Marmolin
St: Larsgatan 50, 58224 Linköping, Sweden



14 sep 2012 kl. 07.08 skrev Bayarja:

if is it possible please you can send me solved code. sorry my english skill

--
You received this message because you are subscribed to the Google Groups "carrierwave" group.
To view this discussion on the web visit https://groups.google.com/d/msg/carrierwave/-/-AktPLxEytAJ.
To post to this group, send email to carri...@googlegroups.com.
To unsubscribe from this group, send email to carrierwave...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/carrierwave?hl=en.

Reply all
Reply to author
Forward
0 new messages