class Shrine::Storage::FileSystem

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

Attributes

Public Class methods

new (directory, prefix: nil, host: 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, host: nil, clean: true, permissions: 0644, directory_permissions: 0755)
32   Shrine.deprecation("The :host option to Shrine::Storage::FileSystem#initialize is deprecated and will be removed in Shrine 3. Pass :host to FileSystem#url instead, you can also use default_url_options plugin.") if host
33 
34   if prefix
35     @prefix = Pathname(relative(prefix))
36     @directory = Pathname(directory).join(@prefix).expand_path
37   else
38     @directory = Pathname(directory).expand_path
39   end
40 
41   @host = host
42   @permissions = permissions
43   @directory_permissions = directory_permissions
44   @clean = clean
45 
46   unless @directory.exist?
47     @directory.mkpath
48     @directory.chmod(directory_permissions) if directory_permissions
49   end
50 end

Public Instance methods

clear! (older_than: nil)

Deletes all files from the directory. If :older_than is passed in (a Time object), deletes all files which were last modified before that time.

[show source]
    # File lib/shrine/storage/file_system.rb
112 def clear!(older_than: nil)
113   if older_than
114     # add trailing slash to make it work with symlinks
115     Pathname("#{directory}/").find do |path|
116       if path.file? && path.mtime < older_than
117         path.delete
118         clean(path) if clean?
119       end
120     end
121   else
122     directory.children.each(&:rmtree)
123   end
124 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
92 def delete(id)
93   path = path(id)
94   path.delete
95   clean(path) if clean?
96 rescue Errno::ENOENT
97 end
exists? (id)

Returns true if the file exists on the filesystem.

[show source]
   # File lib/shrine/storage/file_system.rb
86 def exists?(id)
87   path(id).exist?
88 end
method_missing (name, *args, &block)

Catches the deprecated #download method.

[show source]
    # File lib/shrine/storage/file_system.rb
132 def method_missing(name, *args, &block)
133   case name
134   when :download then deprecated_download(*args, &block)
135   else
136     super
137   end
138 end
movable? (io, id)

Returns true if the file is a File or a UploadedFile uploaded by the FileSystem storage.

[show source]
   # File lib/shrine/storage/file_system.rb
74 def movable?(io, id)
75   io.respond_to?(:path) ||
76     (io.is_a?(UploadedFile) && io.storage.is_a?(Storage::FileSystem))
77 end
move (io, id, shrine_metadata: {}, **upload_options)

Moves the file to the given location. This gets called by the moving plugin.

[show source]
   # File lib/shrine/storage/file_system.rb
62 def move(io, id, shrine_metadata: {}, **upload_options)
63   if io.respond_to?(:path)
64     FileUtils.mv io.path, path!(id)
65   else
66     FileUtils.mv io.storage.path(io.id), path!(id)
67     io.storage.clean(io.storage.path(io.id)) if io.storage.clean?
68   end
69   path(id).chmod(permissions) if permissions
70 end
open (id, **options, &block)

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

[show source]
   # File lib/shrine/storage/file_system.rb
81 def open(id, **options, &block)
82   path(id).open(binmode: true, **options, &block)
83 end
path (id)

Returns the full path to the file.

[show source]
    # File lib/shrine/storage/file_system.rb
127 def path(id)
128   directory.join(id.gsub("/", File::SEPARATOR))
129 end
upload (io, id, shrine_metadata: {}, **upload_options)

Copies the file into the given location.

[show source]
   # File lib/shrine/storage/file_system.rb
53 def upload(io, id, shrine_metadata: {}, **upload_options)
54   bytes_copied = IO.copy_stream(io, path!(id))
55   path(id).chmod(permissions) if permissions
56 
57   shrine_metadata["size"] ||= bytes_copied
58 end
url (id, host: self.host, **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
104 def url(id, host: self.host, **options)
105   path = (prefix ? relative_path(id) : path(id)).to_s
106   host ? host + path : path
107 end