Skip to content

Instantly share code, notes, and snippets.

@ipoval
Last active September 26, 2015 19:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipoval/1144510 to your computer and use it in GitHub Desktop.
Save ipoval/1144510 to your computer and use it in GitHub Desktop.
File_column ruby legacy file-upload plugin
##
# If you encountered a problem like this:
# Errno::EMLINK: Too many links ...releases/20110811111351/public/document/file/51159
# Most likely it is caused by the limitations of your Linux filesystem -
# a directory can only have a certain number of sub-directories.
#
# http://blog.zachwaugh.com/post/309921185/ext3-filesystem-sub-directory-limit
#
##
# Solution:
# /public/document/file/149/file_name.ext =>
# /public/document/file/1/4/9/file_name.ext
#
require 'fileutils'
Dir['*'].each do |dir_name|
if dir_name.length > 1
rel_path = dir_name.scan(/\d/).join('/')
FileUtils.mkdir_p(rel_path)
FileUtils.mv Dir.glob(dir_name + '/*.*'), rel_path
FileUtils.rm_rf dir_name
p rel_path
end
end
##
# Patch for legacy ruby file-upload plugin "file_column"
#
def after_save
super
# we have a newly uploaded image, move it to the correct location
file = clone_as PermanentUploadedFile
# PATCH HERE:
FileUtils.mkdir_p file.instance_eval { @dir }.to_s
file.move_from(File.join(tmp_base_dir, @tmp_dir), @just_uploaded)
# delete temporary files
delete_files
# replace with the new PermanentUploadedFile object
file
end
def relative_path_prefix
raise RuntimeError.new("Trying to access file_column, but primary key got lost.") if @instance.id.to_s.empty?
# PATCH HERE:
@instance.id.to_s.scan(/\d/).join('/')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment