disk_service.rb 4.7 KB
Newer Older
1 2
# frozen_string_literal: true

D
David Heinemeier Hansson 已提交
3 4
require "fileutils"
require "pathname"
5
require "digest/md5"
D
David Heinemeier Hansson 已提交
6
require "active_support/core_ext/numeric/bytes"
D
David Heinemeier Hansson 已提交
7

8
module ActiveStorage
9
  # Wraps a local disk path as an Active Storage service. See ActiveStorage::Service for the generic API
10 11
  # documentation that applies to all services.
  class Service::DiskService < Service
12
    attr_reader :root
13

14
    def initialize(root:, public: false, **options)
15
      @root = root
16
      @public = public
17
    end
18

19
    def upload(key, io, checksum: nil, **)
G
George Claghorn 已提交
20
      instrument :upload, key: key, checksum: checksum do
21 22 23
        IO.copy_stream(io, make_path_for(key))
        ensure_integrity_of(key, checksum) if checksum
      end
24
    end
25

26
    def download(key, &block)
27
      if block_given?
G
George Claghorn 已提交
28
        instrument :streaming_download, key: key do
29
          stream key, &block
30
        end
31
      else
G
George Claghorn 已提交
32
        instrument :download, key: key do
33 34 35
          File.binread path_for(key)
        rescue Errno::ENOENT
          raise ActiveStorage::FileNotFoundError
36
        end
37
      end
38 39
    end

40 41
    def download_chunk(key, range)
      instrument :download_chunk, key: key, range: range do
42 43 44
        File.open(path_for(key), "rb") do |file|
          file.seek range.begin
          file.read range.size
45
        end
46 47
      rescue Errno::ENOENT
        raise ActiveStorage::FileNotFoundError
48 49 50
      end
    end

51
    def delete(key)
G
George Claghorn 已提交
52
      instrument :delete, key: key do
53 54 55
        File.delete path_for(key)
      rescue Errno::ENOENT
        # Ignore files already deleted
56 57
      end
    end
D
Style  
David Heinemeier Hansson 已提交
58

G
George Claghorn 已提交
59 60 61 62 63 64 65 66
    def delete_prefixed(prefix)
      instrument :delete_prefixed, prefix: prefix do
        Dir.glob(path_for("#{prefix}*")).each do |path|
          FileUtils.rm_rf(path)
        end
      end
    end

67
    def exist?(key)
G
George Claghorn 已提交
68
      instrument :exist, key: key do |payload|
69 70 71 72
        answer = File.exist? path_for(key)
        payload[:exist] = answer
        answer
      end
73
    end
74

75
    def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
G
George Claghorn 已提交
76
      instrument :url, key: key do |payload|
77 78 79 80 81
        verified_token_with_expiration = ActiveStorage.verifier.generate(
          {
            key: key,
            content_type: content_type,
            content_length: content_length,
82 83
            checksum: checksum,
            service_name: name
84
          },
A
Akira Matsuda 已提交
85 86
          expires_in: expires_in,
          purpose: :blob_token
87 88
        )

89
        generated_url = url_helpers.update_rails_disk_service_url(verified_token_with_expiration, host: current_host)
90

91
        payload[:url] = generated_url
92

93 94
        generated_url
      end
95 96
    end

97 98
    def headers_for_direct_upload(key, content_type:, **)
      { "Content-Type" => content_type }
99 100
    end

101 102 103
    def path_for(key) #:nodoc:
      File.join root, folder_for(key), key
    end
104

105
    private
106
      def private_url(key, expires_in:, filename:, content_type:, disposition:, **)
107 108 109 110 111 112 113 114
        generate_url(key, expires_in: expires_in, filename: filename, content_type: content_type, disposition: disposition)
      end

      def public_url(key, filename:, content_type: nil, disposition: :attachment, **)
        generate_url(key, expires_in: nil, filename: filename, content_type: content_type, disposition: disposition)
      end

      def generate_url(key, expires_in:, filename:, content_type:, disposition:)
115 116 117 118 119
        content_disposition = content_disposition_with(type: disposition, filename: filename)
        verified_key_with_expiration = ActiveStorage.verifier.generate(
          {
            key: key,
            disposition: content_disposition,
120 121
            content_type: content_type,
            service_name: name
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
          },
          expires_in: expires_in,
          purpose: :blob_key
        )

        current_uri = URI.parse(current_host)

        url_helpers.rails_disk_service_url(verified_key_with_expiration,
          protocol: current_uri.scheme,
          host: current_uri.host,
          port: current_uri.port,
          filename: filename
        )
      end


138 139 140 141 142 143 144 145 146 147
      def stream(key)
        File.open(path_for(key), "rb") do |file|
          while data = file.read(5.megabytes)
            yield data
          end
        end
      rescue Errno::ENOENT
        raise ActiveStorage::FileNotFoundError
      end

148 149 150
      def folder_for(key)
        [ key[0..1], key[2..3] ].join("/")
      end
151

152 153
      def make_path_for(key)
        path_for(key).tap { |path| FileUtils.mkdir_p File.dirname(path) }
154
      end
155 156 157 158 159 160 161

      def ensure_integrity_of(key, checksum)
        unless Digest::MD5.file(path_for(key)).base64digest == checksum
          delete key
          raise ActiveStorage::IntegrityError
        end
      end
162 163 164 165

      def url_helpers
        @url_helpers ||= Rails.application.routes.url_helpers
      end
166 167 168 169

      def current_host
        ActiveStorage::Current.host
      end
170
  end
171
end