Class: FileStore::LocalStore

Inherits:
BaseStore show all
Defined in:
lib/file_store/local_store.rb

Instance Method Summary collapse

Methods inherited from BaseStore

#cache_file, #download, #download!, #download_safe, #get_cache_path_for, #get_from_cache, #get_path_for_optimized_image, #get_path_for_upload, #internal?, #remove_optimized_image, #remove_upload, #s3_upload_host, #store_optimized_image, #store_upload, temporary_upload_path, #upload_path

Instance Method Details

#absolute_base_cdn_urlObject



32
33
34
# File 'lib/file_store/local_store.rb', line 32

def absolute_base_cdn_url
  "#{Discourse.asset_host}#{relative_base_url}"
end

#absolute_base_urlObject



28
29
30
# File 'lib/file_store/local_store.rb', line 28

def absolute_base_url
  "#{Discourse.base_url_no_prefix}#{relative_base_url}"
end

#cdn_url(url) ⇒ Object



53
54
55
# File 'lib/file_store/local_store.rb', line 53

def cdn_url(url)
  UrlHelper.local_cdn_url(url)
end

#copy_file(file, path) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/file_store/local_store.rb', line 80

def copy_file(file, path)
  dir = Pathname.new(path).dirname
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
  # move the file to the right location
  # not using mv, cause permissions are no good on move
  File.open(path, "wb") { |f| f.write(file.read) }
end

#copy_from(source_path) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/file_store/local_store.rb', line 111

def copy_from(source_path)
  FileUtils.mkdir_p(File.join(public_dir, upload_path))

  Discourse::Utils.execute_command(
    "rsync",
    "-a",
    "--safe-links",
    "#{source_path}/",
    "#{upload_path}/",
    failure_message: "Failed to copy uploads.",
    chdir: public_dir,
  )
end

#download_url(upload) ⇒ Object



48
49
50
51
# File 'lib/file_store/local_store.rb', line 48

def download_url(upload)
  return unless upload
  File.join(relative_base_url, upload.sha1)
end

#external?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/file_store/local_store.rb', line 44

def external?
  false
end

#get_path_for(type, upload_id, sha, extension) ⇒ Object



76
77
78
# File 'lib/file_store/local_store.rb', line 76

def get_path_for(type, upload_id, sha, extension)
  prefix_path(super(type, upload_id, sha, extension))
end

#has_been_uploaded?(url) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/file_store/local_store.rb', line 24

def has_been_uploaded?(url)
  is_relative?(url) || is_local?(url)
end

#is_local?(url) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
# File 'lib/file_store/local_store.rb', line 92

def is_local?(url)
  return false if url.blank?
  absolute_url = url.start_with?("//") ? SiteSetting.scheme + ":" + url : url
  absolute_url.start_with?(absolute_base_url) || absolute_url.start_with?(absolute_base_cdn_url)
end

#is_relative?(url) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/file_store/local_store.rb', line 88

def is_relative?(url)
  url.present? && url.start_with?(relative_base_url)
end

#list_missing_uploads(skip_optimized: false) ⇒ Object



106
107
108
109
# File 'lib/file_store/local_store.rb', line 106

def list_missing_uploads(skip_optimized: false)
  list_missing(Upload)
  list_missing(OptimizedImage) unless skip_optimized
end

#path_for(upload) ⇒ Object



57
58
59
60
# File 'lib/file_store/local_store.rb', line 57

def path_for(upload)
  url = upload.try(:url)
  "#{public_dir}#{upload.url}" if url && url[0] == "/" && url[1] != "/"
end

#public_dirObject



98
99
100
# File 'lib/file_store/local_store.rb', line 98

def public_dir
  File.join(Rails.root, "public")
end

#purge_tombstone(grace_period) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/file_store/local_store.rb', line 62

def purge_tombstone(grace_period)
  if Dir.exist?(Discourse.store.tombstone_dir)
    Discourse::Utils.execute_command(
      "find",
      tombstone_dir,
      "-mtime",
      "+#{grace_period}",
      "-type",
      "f",
      "-delete",
    )
  end
end

#relative_base_urlObject



36
37
38
# File 'lib/file_store/local_store.rb', line 36

def relative_base_url
  File.join(Discourse.base_path, upload_path)
end

#remove_file(url, _) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/file_store/local_store.rb', line 12

def remove_file(url, _)
  return unless is_relative?(url)
  source = "#{public_dir}#{url}"
  return unless File.exist?(source)
  destination = "#{public_dir}#{url.sub("/uploads/", "/uploads/tombstone/")}"
  dir = Pathname.new(destination).dirname
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
  FileUtils.remove(destination) if File.exist?(destination)
  FileUtils.move(source, destination, force: true)
  FileUtils.touch(destination)
end

#store_file(file, path) ⇒ Object



7
8
9
10
# File 'lib/file_store/local_store.rb', line 7

def store_file(file, path)
  copy_file(file, "#{public_dir}#{path}")
  "#{Discourse.base_path}#{path}"
end

#temporary_upload_path(filename) ⇒ Object



40
41
42
# File 'lib/file_store/local_store.rb', line 40

def temporary_upload_path(filename)
  FileStore::BaseStore.temporary_upload_path(filename, folder_prefix: relative_base_url)
end

#tombstone_dirObject



102
103
104
# File 'lib/file_store/local_store.rb', line 102

def tombstone_dir
  "#{public_dir}#{relative_base_url.sub("/uploads/", "/uploads/tombstone/")}"
end