Last active
September 26, 2015 19:07
-
-
Save ipoval/1144510 to your computer and use it in GitHub Desktop.
File_column ruby legacy file-upload plugin
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
## | |
# 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