forked from ifunam/salva
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migration to extract photos from db to file_system. Closes ifunam#3
- Loading branch information
juarlex
committed
Oct 26, 2011
1 parent
79d0f99
commit 6e466bf
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
db/migrate/20111026154416_extract_photos_from_people_to_file_system.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class ExtractPhotosFromPeopleToFileSystem < ActiveRecord::Migration | ||
def self.up | ||
base_path = Rails.root.to_s + '/tmp' | ||
Person.where("photo IS NOT NULL AND photo_content_type IS NOT NULL").all.each do |record| | ||
user_path = File.join(base_path, 'image/file', record.id.to_s) | ||
unless File.exist? user_path | ||
puts "Creating directory: #{user_path}" | ||
system "mkdir -p #{user_path}" | ||
end | ||
original_photo = File.join(user_path, "#{record.user.login}.#{record.photo_content_type}") | ||
puts "Creating file: #{original_photo}" | ||
|
||
File.open(original_photo, 'w') do |f| | ||
f.write record.photo.force_encoding('utf-8') | ||
end | ||
|
||
puts "Uploading file: #{original_photo}" | ||
record.image = Image.new(:file => File.open(original_photo)) | ||
record.save | ||
|
||
puts "Deleting file: #{original_photo}" | ||
File.delete original_photo | ||
end | ||
end | ||
|
||
def self.down | ||
end | ||
end |