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

pulibrary / pdc_describe / cace366a-ffad-45f1-9b60-678e607fa527

14 May 2024 02:21PM UTC coverage: 60.862% (-35.0%) from 95.908%
cace366a-ffad-45f1-9b60-678e607fa527

push

circleci

jrgriffiniii
wip

1 of 3 new or added lines in 2 files covered. (33.33%)

1194 existing lines in 57 files now uncovered.

2076 of 3411 relevant lines covered (60.86%)

22.71 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✔
UNCOV
15
    @upload_snapshot = upload_snapshot
×
UNCOV
16
    @original_filenames = @upload_snapshot.files.map { |a| a["filename"] }
×
UNCOV
17
    @files = parse_files_to_rename
×
UNCOV
18
    @renamed_files = rename_files
×
19
  end
20

21
  def parse_files_to_rename
1✔
UNCOV
22
    files = []
×
UNCOV
23
    @original_filenames.each do |original_filename|
×
UNCOV
24
      files << FileRenameService.new(filename: original_filename)
×
25
    end
UNCOV
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✔
UNCOV
33
    @upload_snapshot.with_lock do
×
UNCOV
34
      @upload_snapshot.reload
×
UNCOV
35
      rename_index = 1
×
UNCOV
36
      renamed_files = {}
×
UNCOV
37
      @files.each do |file|
×
UNCOV
38
        next unless file.needs_rename?
×
UNCOV
39
        new_filename = file.new_filename(rename_index)
×
UNCOV
40
        renamed_files[file.original_filename] = new_filename
×
41
        # Also update the filename in the MigrationSnapshot
UNCOV
42
        @upload_snapshot.rename(file.original_filename, new_filename)
×
UNCOV
43
        rename_index += 1
×
44
      end
UNCOV
45
      @upload_snapshot.save
×
UNCOV
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✔
UNCOV
52
    @files.each do |file|
×
UNCOV
53
      return true if file.needs_rename?
×
54
    end
UNCOV
55
    false
×
56
  end
57

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

63
  def renaming_document
1✔
UNCOV
64
    message = "Some files have been renamed to comply with AWS S3 storage requirements\n"
×
UNCOV
65
    message += "Rename date: #{rename_date}\n"
×
UNCOV
66
    message += "Original Filename\t Renamed File\n"
×
UNCOV
67
    @files.each do |file|
×
UNCOV
68
      next unless file.needs_rename?
×
UNCOV
69
      message += "#{file.original_filename}\t#{@renamed_files[file.original_filename]}\n"
×
70
    end
UNCOV
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