class Shrine::Storage::FileSystem

  1. lib/shrine/storage/file_system.rb
Superclass: Object

Attributes

Public Class methods

new(directory, prefix: nil, clean: true, permissions: 0644, directory_permissions: 0755)

Initializes a storage for uploading to the filesystem.

:prefix

The directory relative to directory to 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 to 0644.

:directory_permissions

The UNIX permissions applied to created directories. Can be set to nil, in which case the default permissions will be applied. Defaults to 0755.

: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.

[show source]
   # 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

clear!(&condition)

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
[show 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
delete(id)

Delets the file, and by default deletes the containing directory if it’s empty.

[show 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
delete_prefixed(delete_prefix)

Deletes the specified directory on the filesystem.

file_system.delete_prefixed(“somekey/derivatives/”)

[show source]
   # File lib/shrine/storage/file_system.rb
95 def delete_prefixed(delete_prefix)
96   FileUtils.rm_rf directory.join(delete_prefix)
97 end
exists?(id)

Returns true if the file exists on the filesystem.

[show source]
   # File lib/shrine/storage/file_system.rb
69 def exists?(id)
70   path(id).exist?
71 end
open(id, **options)

Opens the file on the given location in read mode. Accepts additional File.open arguments.

[show source]
   # 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
path(id)

Returns the full path to the file.

[show source]
    # File lib/shrine/storage/file_system.rb
117 def path(id)
118   directory.join(id.gsub("/", File::SEPARATOR))
119 end
upload(io, id, move: false, **)

Copies the file into the given location.

[show source]
   # 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
url(id, host: nil, **options)

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.

[show source]
   # 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