class Shrine::Storage::FileSystem
Attributes
Public Class Methods
# 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
Initializes a storage for uploading to the filesystem.
- :prefix
-
The directory relative to
directoryto which files will be stored, and it is included in the URL. - :permissions
-
The UNIX permissions applied to created files. Can be set to
nil, in which case the default permissions will be applied. Defaults to0644. - :directory_permissions
-
The UNIX permissions applied to created directories. Can be set to
nil, in which case the default permissions will be applied. Defaults to0755. - :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
false.
Public Instance Methods
Source
# 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
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
Source
# 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
Delets the file, and by default deletes the containing directory if it’s empty.
# File lib/shrine/storage/file_system.rb 95 def delete_prefixed(delete_prefix) 96 FileUtils.rm_rf directory.join(delete_prefix) 97 end
Deletes the specified directory on the filesystem.
file_system.delete_prefixed(“somekey/derivatives/”)
Source
# File lib/shrine/storage/file_system.rb 69 def exists?(id) 70 path(id).exist? 71 end
Returns true if the file exists on the filesystem.
Source
# File lib/shrine/storage/file_system.rb 62 def open(id, **) 63 path(id).open(binmode: true, **) 64 rescue Errno::ENOENT 65 raise Shrine::FileNotFound, "file #{id.inspect} not found on storage" 66 end
Opens the file on the given location in read mode. Accepts additional File.open arguments.
Source
# File lib/shrine/storage/file_system.rb 119 def path(id) 120 path = directory.join(id.gsub("/", File::SEPARATOR)) 121 expanded = path.expand_path 122 123 unless expanded == directory || expanded.to_s.start_with?("#{directory}#{File::SEPARATOR}") 124 raise Shrine::Error, "path #{id.inspect} resolves outside of the storage directory" 125 end 126 127 path 128 end
Returns the full path to the file. Raises Shrine::Error if the id would resolve to a location outside of the storage directory (e.g. an id containing ../ path traversal sequences in attacker-controlled data).
# 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
Copies the file into the given location.
Source
# File lib/shrine/storage/file_system.rb 78 def url(id, host: nil, **) 79 path = (prefix ? relative_path(id) : path(id)).to_s 80 host ? host + path : path 81 end