summaryrefslogtreecommitdiff
path: root/app/uploaders/file_mover.rb
blob: 7c7953c8a0eaaf60231f4055ed406b6c0bf286db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true

class FileMover
  include Gitlab::Utils::StrongMemoize

  attr_reader :secret, :file_name, :from_model, :to_model, :update_field

  def initialize(file_path, update_field = :description, from_model:, to_model:)
    @secret = File.split(File.dirname(file_path)).last
    @file_name = File.basename(file_path)
    @from_model = from_model
    @to_model = to_model
    @update_field = update_field
  end

  def execute
    temp_file_uploader.retrieve_from_store!(file_name)

    return unless valid?

    uploader.retrieve_from_store!(file_name)

    move

    if update_markdown
      update_upload_model
      uploader.schedule_background_upload
    end
  end

  private

  def valid?
    if temp_file_uploader.file_storage?
      Pathname.new(temp_file_path).realpath.to_path.start_with?(
        (Pathname(temp_file_uploader.root) + temp_file_uploader.base_dir).to_path
      )
    else
      temp_file_uploader.exists?
    end
  end

  def move
    if temp_file_uploader.file_storage?
      FileUtils.mkdir_p(File.dirname(file_path))
      FileUtils.move(temp_file_path, file_path)
    else
      uploader.copy_file(temp_file_uploader.file)
      temp_file_uploader.upload.destroy!
    end
  end

  def update_markdown
    updated_text = to_model.read_attribute(update_field)
                           .gsub(temp_file_uploader.markdown_link, uploader.markdown_link)
    to_model.update_attribute(update_field, updated_text)
  rescue
    revert
    false
  end

  def update_upload_model
    return unless upload = temp_file_uploader.upload
    return if upload.destroyed?

    upload.update!(model: to_model)
  end

  def temp_file_path
    strong_memoize(:temp_file_path) do
      temp_file_uploader.file.path
    end
  end

  def file_path
    strong_memoize(:file_path) do
      uploader.file.path
    end
  end

  def uploader
    @uploader ||=
      begin
        uploader = PersonalFileUploader.new(to_model, secret: secret)

        # Enforcing a REMOTE object storage given FileUploader#retrieve_from_store! won't do it
        # (there's no upload at the target yet).
        if uploader.class.object_store_enabled?
          uploader.object_store = ::ObjectStorage::Store::REMOTE
        end

        uploader
      end
  end

  def temp_file_uploader
    @temp_file_uploader ||= PersonalFileUploader.new(from_model, secret: secret)
  end

  def revert
    Rails.logger.warn("Markdown not updated, file move reverted for #{to_model}") # rubocop:disable Gitlab/RailsLogger

    if temp_file_uploader.file_storage?
      FileUtils.move(file_path, temp_file_path)
    end
  end
end