• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / 9091a1ae-29be-458c-984a-339d213919c4

12 Dec 2024 07:41PM UTC coverage: 26.434% (-69.7%) from 96.113%
9091a1ae-29be-458c-984a-339d213919c4

Pull #2000

circleci

jrgriffiniii
Removing integration with ActiveStorage
Pull Request #2000: Bump actionpack from 7.2.1.1 to 7.2.2.1

945 of 3575 relevant lines covered (26.43%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.51
/app/services/file_rename_mapping_service.rb
1
# frozen_string_literal: true
2

3
# We sometimes have data with filenames that contain characters that AWS S3 cannot handle. In those cases we want to:
4
# 1.  Rename the files to something that is AWS legal. Replace all illegal characters with a _ (underscore)
5
# 2.  Ensure there are no duplicate file names after the renaming by appending a (1), (2) at the end of the filename
6
#     if the file has been renamed
7
# 3.  Keep a record of all of the file names as they originally existed and what they were renamed to
8
# 4.  The record goes into a file called files_renamed.txt, which contains a list of all files that have been renamed
9
#     and what they were renamed to, along with a timestamp
10
# 5.  This files_renamed.txt file gets added to the dataset as a payload file, akin to a README.txt or license.txt
11
class FileRenameMappingService
1✔
12
  attr_reader :upload_snapshot, :files, :renamed_files, :original_filenames
1✔
13

14
  def initialize(upload_snapshot:)
1✔
15
    @upload_snapshot = upload_snapshot
×
16
    @original_filenames = @upload_snapshot.files.map { |a| a["filename"] }
×
17
    @files = parse_files_to_rename
×
18
    @renamed_files = rename_files
×
19
  end
20

21
  def parse_files_to_rename
1✔
22
    files = []
×
23
    @original_filenames.each do |original_filename|
×
24
      files << FileRenameService.new(filename: original_filename)
×
25
    end
26
    files
×
27
  end
28

29
  # Make a hash containing all files that need renaming.
30
  # The key of the hash is the original filename.
31
  # The value of the hash is the re-named file with an index number appended.
32
  def rename_files
1✔
33
    @upload_snapshot.with_lock do
×
34
      @upload_snapshot.reload
×
35
      rename_index = 1
×
36
      renamed_files = {}
×
37
      @files.each do |file|
×
38
        next unless file.needs_rename?
×
39
        new_filename = file.new_filename(rename_index)
×
40
        renamed_files[file.original_filename] = new_filename
×
41
        # Also update the filename in the MigrationSnapshot
42
        @upload_snapshot.rename(file.original_filename, new_filename)
×
43
        rename_index += 1
×
44
      end
45
      @upload_snapshot.save
×
46
      renamed_files
×
47
    end
48
  end
49

50
  # A rename is needed if any of the original filenames need renaming
51
  def rename_needed?
1✔
52
    @files.each do |file|
×
53
      return true if file.needs_rename?
×
54
    end
55
    false
×
56
  end
57

58
  # Format: "Sep 19 2023"
59
  def rename_date
1✔
60
    Time.zone.now.strftime("%d %b %Y")
×
61
  end
62

63
  def renaming_document
1✔
64
    message = "Some files have been renamed to comply with AWS S3 storage requirements\n"
×
65
    message += "Rename date: #{rename_date}\n"
×
66
    message += "Original Filename\t Renamed File\n"
×
67
    @files.each do |file|
×
68
      next unless file.needs_rename?
×
69
      message += "#{file.original_filename}\t#{@renamed_files[file.original_filename]}\n"
×
70
    end
71
    message
×
72
  end
73
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc