Skip to content

Commit

Permalink
Check for path existence in case test suite runs too fast.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpendragon committed Sep 15, 2023
1 parent 65b184f commit 54b5dad
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/valkyrie/storage/versioned_disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ def initialize(base_path:, path_generator: ::Valkyrie::Storage::Disk::BucketedSt
# @param resource [Valkyrie::Resource]
# @param _extra_arguments [Hash] additional arguments which may be passed to other adapters
# @return [Valkyrie::StorageAdapter::File]
def upload(file:, original_filename:, resource: nil, **_extra_arguments)
def upload(file:, original_filename:, resource: nil, **extra_arguments)
version_timestamp = current_timestamp
new_path = path_generator.generate(resource: resource, file: file, original_filename: "v-#{version_timestamp}-#{original_filename}")
# If we've gone faster than milliseconds here, re-call. Probably only an
# issue for test suites.
return upload(file: file, original_filename: original_filename, resource: resource, **extra_arguments) if File.exist?(new_path)
FileUtils.mkdir_p(new_path.parent)
file_mover.call(file.try(:path) || file.try(:disk_path), new_path)
find_by(id: Valkyrie::ID.new("versiondisk://#{new_path}"))
end

def current_timestamp
Time.now.strftime("%s%N")
Time.now.strftime("%s%L")
end

def upload_version(id:, file:)
Expand All @@ -37,6 +40,8 @@ def upload_version(id:, file:)
current_version_id = current_version_id.version_files[1] if current_version_id.deletion_marker?
existing_path = current_version_id.file_path
new_path = Pathname.new(existing_path.gsub(current_version_id.version, version_timestamp.to_s))
# If we've gone faster than milliseconds here, re-call.
return upload_version(id: id, file: file) if File.exist?(new_path)
FileUtils.mkdir_p(new_path.parent)
file_mover.call(file.try(:path) || file.try(:disk_path), new_path)
find_by(id: Valkyrie::ID.new("versiondisk://#{new_path}"))
Expand Down

0 comments on commit 54b5dad

Please sign in to comment.