No commit activity in last 3 years
No release in over 3 years
Enscapsulates directory naming scheme for carrierwave's file storage, enabling creation of directory structure that will be efficient and survive subdirs limit.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Introduction

This is a simple Ruby gem that just enscapsulates directory naming scheme for carrierwave's file storage, enabling creation of directory structure that will be efficient and survive subdirectories limit.

Problem

If you have plenty of attachments you can encounter slowdowns or OS will reject creating next subdirectory at some point.

It is due to the fact that some filesystems have limit of subdirectories (for example: 64000 in ext4, until you enable the file system feature flag dir_nlink). Moreover, having a flat subdirectories structure can be suboptimal.

Solution

Create directory structure of the following syntax:

(ROOT_PATH)/(model class)/(mounted as)/(record's ID in hex split into groups of max 3 characters)/(file name)

For example:

/home/myrailsapp/public/system/User/Avatar/af7/233/1a3/12/whatever.mp3

Installation

Add to your Gemfile:

gem "carrierwave-hex-prefix-for-file-storage"

and then type:

bundle install

Then include it in your Uploader:

class SourceBodyUploader < CarrierWave::Uploader::Base
  include CarrierWave::Uploader::HexPrefixForFileStorage
  
  storage :file

  def store_dir
    store_dir_with_hex_prefix 
  end
end

Optionally, you can pass a parameter to store_dir_with_hex_prefix in order to change root path (by default it is RAILS_ROOT/public/system):

class SourceBodyUploader < CarrierWave::Uploader::Base
  include CarrierWave::Uploader::HexPrefixForFileStorage
  
  storage :file

  def store_dir
    store_dir_with_hex_prefix Rails.root.join("my_directory")
  end
end

Changelog

0.0.1 - Initial release

License

MIT