File: records_uploads.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (84 lines) | stat: -rw-r--r-- 2,295 bytes parent folder | download
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
# frozen_string_literal: true

module RecordsUploads
  module Concern
    extend ActiveSupport::Concern

    # This value is stored in `uploads.version`. Increment this value to have
    # functionality that only applies to certain versions of uploads.
    VERSION = 2

    attr_accessor :upload

    included do
      after  :store,  :record_upload
      before :remove, :destroy_upload
    end

    # After storing an attachment, create a corresponding Upload record
    #
    # NOTE: We're ignoring the argument passed to this callback because we want
    # the `SanitizedFile` object from `CarrierWave::Uploader::Base#file`, not the
    # `Tempfile` object the callback gets.
    #
    # Called `after :store`
    # rubocop: disable CodeReuse/ActiveRecord
    def record_upload(_tempfile = nil)
      return unless model
      return unless file && file.exists?

      Upload.transaction { readd_upload }
    end

    def readd_upload
      Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification.temporary_ignore_tables_in_transaction(
        %w[uploads], url: "https://gitlab.com/gitlab-org/gitlab/-/issues/398199"
      ) do
        uploads.where(model: model, path: upload_path).delete_all
        upload.delete if upload

        self.upload = build_upload.tap(&:save!)
      end
    end
    # rubocop: enable CodeReuse/ActiveRecord

    def upload_path
      File.join(store_dir, filename.to_s)
    end

    def filename
      upload&.path ? File.basename(upload.path) : super
    end

    private

    # rubocop: disable CodeReuse/ActiveRecord
    def uploads
      Upload.order(id: :desc).where(uploader: self.class.to_s)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    def build_upload
      Upload.new(
        uploader: self.class.to_s,
        size: file.size,
        path: upload_path,
        model: model,
        mount_point: mounted_as,
        version: VERSION
      )
    end

    # Before removing an attachment, destroy any Upload records at the same path
    #
    # Called `before :remove`
    # rubocop: disable CodeReuse/ActiveRecord
    def destroy_upload(*args)
      return unless file && file.exists?

      self.upload = nil
      uploads.where(path: upload_path).delete_all
    end
    # rubocop: enable CodeReuse/ActiveRecord
  end
end