Methods
Public Class
Public Instance
Attributes
| directory | [R] | |
| directory_permissions | [R] | |
| permissions | [R] | |
| prefix | [R] |
Public Class methods
Initializes a storage for uploading to the filesystem.
| :prefix |
The directory relative to |
| :permissions |
The UNIX permissions applied to created files. Can be set to |
| :directory_permissions |
The UNIX permissions applied to created directories. Can be set to |
| :clean |
By default empty folders inside the directory are automatically deleted, but if it happens that it causes too much load on the filesystem, you can set this option to |
# File lib/shrine/storage/file_system.rb 31 def initialize(directory, prefix: nil, clean: true, permissions: 0644, directory_permissions: 0755) 32 if prefix 33 @prefix = Pathname(relative(prefix)) 34 @directory = Pathname(directory).join(@prefix).expand_path 35 else 36 @directory = Pathname(directory).expand_path 37 end 38 39 @permissions = permissions 40 @directory_permissions = directory_permissions 41 @clean = clean 42 43 unless @directory.exist? 44 @directory.mkpath 45 @directory.chmod(directory_permissions) if directory_permissions 46 end 47 end
Public Instance methods
Deletes all files from the directory. If a block is passed in, deletes only the files for which the block evaluates to true.
file_system.clear! # deletes all files and subdirectories in the storage directory file_system.clear! { |path| path.mtime < Time.now - 7*24*60*60 } # deletes only files older than 1 week
# File lib/shrine/storage/file_system.rb 104 def clear!(&condition) 105 if condition 106 list_files(directory) do |path| 107 next unless condition.call(path) 108 path.delete 109 clean(path) if clean? 110 end 111 else 112 directory.children.each(&:rmtree) 113 end 114 end
Delets the file, and by default deletes the containing directory if it’s empty.
# File lib/shrine/storage/file_system.rb 85 def delete(id) 86 path = path(id) 87 path.delete 88 clean(path) if clean? 89 rescue Errno::ENOENT 90 end
Deletes the specified directory on the filesystem.
file_system.delete_prefixed(“somekey/derivatives/”)
# File lib/shrine/storage/file_system.rb 95 def delete_prefixed(delete_prefix) 96 FileUtils.rm_rf directory.join(delete_prefix) 97 end
Returns true if the file exists on the filesystem.
# File lib/shrine/storage/file_system.rb 69 def exists?(id) 70 path(id).exist? 71 end
Opens the file on the given location in read mode. Accepts additional File.open arguments.
# File lib/shrine/storage/file_system.rb 62 def open(id, **options) 63 path(id).open(binmode: true, **options) 64 rescue Errno::ENOENT 65 raise Shrine::FileNotFound, "file #{id.inspect} not found on storage" 66 end
Returns the full path to the file.
# File lib/shrine/storage/file_system.rb 117 def path(id) 118 directory.join(id.gsub("/", File::SEPARATOR)) 119 end
Copies the file into the given location.
# File lib/shrine/storage/file_system.rb 50 def upload(io, id, move: false, **) 51 if move && movable?(io) 52 move(io, path!(id)) 53 else 54 IO.copy_stream(io, path!(id)) 55 end 56 57 path(id).chmod(permissions) if permissions 58 end
If prefix is not present, returns a path composed of directory and the given id. If prefix is present, it excludes the directory part from the returned path (e.g. directory can be set to “public” folder). Both cases accept a :host value which will be prefixed to the generated path.
# File lib/shrine/storage/file_system.rb 78 def url(id, host: nil, **options) 79 path = (prefix ? relative_path(id) : path(id)).to_s 80 host ? host + path : path 81 end