class ActiveStorage::Service::DiskService

Overview

Wraps a local directory as an ActiveStorage service.

Blobs stored on disk are served by the app itself, through an expiring encoded key: MartenRailsBridge::ActiveStorage::DiskHandler is the endpoint that resolves these keys. https://github.com/rails/rails/blob/v8.1.3/activestorage/lib/active_storage/service/disk_service.rb

Direct Known Subclasses

Defined in:

active_storage/service/disk_service.cr

Constant Summary

BLOB_KEY_PURPOSE = "blob_key"

Purpose carried by the signed keys this service issues.

Constructors

Instance Method Summary

Instance methods inherited from class ActiveStorage::Service

content_disposition_with(filename : Filename, type : String | Nil = "inline") : String content_disposition_with, delete(key : String) : Nil delete, delete_prefixed(prefix : String) : Nil delete_prefixed, download(key : String) : String
download(key : String, & : Bytes -> ) : Nil
download
, download_chunk(key : String, range : Range(Int64, Int64)) : String download_chunk, exist?(key : String) : Bool exist?, private_url(key : String, expires_in : Time::Span, filename : Filename, disposition : String, content_type : String) private_url, public? : Bool public?, public_url(key : String, filename : Filename, content_type : String, disposition : String) public_url, upload(key : String, io : IO, checksum : String | Nil = nil, filename : Filename | Nil = nil, content_type : String | Nil = nil, disposition : String | Nil = nil) : Nil upload, url(key : String, expires_in : Time::Span, filename : Filename, content_type : String, disposition : String) url

Constructor methods inherited from class ActiveStorage::Service

new new

Constructor Detail

def self.new(root : String, public : Bool = false) #

Instance Method Detail

def delete(key : String) : Nil #

Removes the object stored under the passed key.

The sharding directories are left behind, as Rails leaves them. https://github.com/rails/rails/blob/v8.1.3/activestorage/lib/active_storage/service/disk_service.rb


def delete_prefixed(prefix : String) : Nil #

Removes every object whose key starts with the passed prefix.

The prefix goes through #path_for like any key, so it is sharded the same way and cannot resolve outside of the root. Sharding reads the first characters of the whole key, so variants/<blob key>/ lands under va/ri and not under the shard directories of the blob it belongs to.

The glob reads what is under the prefix, so the emptied variants/<key>/ directory is left behind, as #delete leaves the sharding directories. The matches take FileUtils.rm_rf because a prefix can hold directories of its own. https://github.com/rails/rails/blob/v8.1.3/activestorage/lib/active_storage/service/disk_service.rb


def download(key : String, & : Bytes -> ) : Nil #

Yields the content stored for the passed key, CHUNK_SIZE bytes at a time.

Every chunk gets its own buffer, so a caller may hold a yielded slice past the block: Rails' File#read returns a new String per call, and S3Service#download a new one per range request. One buffer reused across reads would let a later read overwrite a chunk still being held. Raises ActiveStorage::FileNotFoundError when nothing is stored for the key. https://github.com/rails/rails/blob/v8.1.3/activestorage/lib/active_storage/service/disk_service.rb


def download(key : String) : String #

Returns the whole content stored for the passed key.

Raises ActiveStorage::FileNotFoundError when nothing is stored for the key.


def download_chunk(key : String, range : Range(Int64, Int64)) : String #

Returns the content stored for the passed key for the passed inclusive byte range.

Raises ActiveStorage::FileNotFoundError when nothing is stored for the key. https://github.com/rails/rails/blob/v8.1.3/activestorage/lib/active_storage/service/disk_service.rb


def exist?(key : String) : Bool #

Returns true when something is stored for the passed key.


def name : String #

Returns the name this service is registered under in ActiveStorage::Blob.services.

Rails names a service when it builds it from config/storage.yml; the services are registered in a hash here, so the name is read back from that hash.


def path_for(key : String) : String #

Returns the path the passed key is stored at.

Every filesystem operation goes through this method: it is where the path traversal protection lives. A blank key, a key carrying a . or .. segment, a key carrying a null byte and a key resolving outside of the root are all rejected with ActiveStorage::InvalidKeyError. https://github.com/rails/rails/blob/v8.1.3/activestorage/lib/active_storage/service/disk_service.rb


def private_url(key : String, expires_in : Time::Span, filename : ActiveStorage::Filename, disposition : String, content_type : String) #

Returns an expiring URL serving the passed key. https://github.com/rails/rails/blob/v8.1.3/activestorage/lib/active_storage/service/disk_service.rb


def public_url(key : String, filename : ActiveStorage::Filename, content_type : String, disposition : String) #

Returns a permanent URL serving the passed key. https://github.com/rails/rails/blob/v8.1.3/activestorage/lib/active_storage/service/disk_service.rb


def root : String #

Returns the directory the blobs are stored under.


def upload(key : String, io : IO, checksum : String | Nil = nil, filename : ActiveStorage::Filename | Nil = nil, content_type : String | Nil = nil, disposition : String | Nil = nil) : Nil #

Stores the content of the passed IO under the passed key.

The content is written to a temporary file next to its final path and renamed once it is complete, so a reader never sees a partially written object. filename, content_type and disposition are accepted for the ActiveStorage::Service contract and ignored: a disk object carries no metadata, the serving headers are rebuilt from the signed key instead. https://github.com/rails/rails/blob/v8.1.3/activestorage/lib/active_storage/service/disk_service.rb