Changing Files or Versions - UVicLibrary/Vault GitHub Wiki
Cheatsheet (Jump to)
- Switching Versions Through the Interface - Public view for a work > [File set name] > Versions tab > Upload a new version or revert, or
Public view for a work > File set "Actions" dropdown > Versions tab > Upload a new version or revert - Switching Versions in Bulk (for Developers) - In the rails console, copy/paste the relevant scripts for each section
Sometimes we need to swap files across an entire collection without changing the metadata. For example, we might need to swap non-linearized PDFs with linearized ("web-ready") PDFs. We can do this manually through the interface, or in batches through the Rails console.
After swapping, the file set will look like it hasn't changed (i.e. it will have the same title). But the viewer should now display the new file and, when you download it, will have the updated file name.
Currently, this feature only works with files that are already on Q:Drive/the server and files that are not images.
Switching Versions Through the Interface
Go to the public view for a work and scroll down to the list of file sets. Click on the dropdown menu under "Actions" and click on "Versions."
You'll be taken to the edit page for the selected file set. Here, you can see the version history under the "Restore Previous Version," along with the date and depositor/user for each version.
To upload a new file, click the "Choose file" button and select a file from your computer, then click "Upload New Version" to save.
If you want to revert to a previous version instead, click on the bubble of the version you want to revert to, and then click "Save Revision".
Switching Versions in Bulk
There are two main steps:
- Create a CSV with file paths in one column and file set ids in another
- Run a script in the rails console
Generating the CSV
In the rails console, grab all works in a collection with GenericWork.where(member_of_collection_ids_ssim: [Collection id])
and save them as an array variable (e.g. clippings_works
for the BC Sessional Clippings).
For this example, assume that the file to be replaced is something like foo.pdf
and the file you want to replace it with is called foo_linearized.pdf
. You could then copy/paste this code into the rails console.
Remember to replace clippings_works
with whatever variable name you're using and replace to_linearize.csv
with the desired name of your csv.
require 'csv'
require 'fileutils'
CSV.open("to_linearize.csv", "a+") do |csv|
csv << ["file_path", "file_set_id"]
clippings_works.each do |work|
work.members.each do |file_set|
file_path = file_set.import_url
file_name = "#{File.basename(file_path, ".pdf")}_linearized.pdf"
new_path = file_path.split("/") - [file_path.split("/").last] + [file_name] # replaces the old file name with the new one; returns an array
new_path = new_path.join("/")
if file_path.split(".").last == "pdf"
csv << [new_path, file_set.id]
end
end
end
end
Running the Script
The following script is saved in the vault
folder as update_clippings.rb
. For each row in the CSV, it uses the file path to read the file that will replace the previous version. Then it creates a file set actor from a fileset ID (whose file you want to swap) and a user object. It then runs a method called update_content
, defined in the file set actor, to update and index the file set and its parent work. The update_content
method triggers a bunch of jobs, including the IngestJob and the CharacterizationJob.
require 'csv'
require 'fileutils'
require 'pathname'
# For each line in the CSV
user = # the user who updates the files
CSV.foreach('to_linearize.csv', headers: true, header_converters: :symbol) do |row|
file_path = row[:file_path]
fileset_id = row[:file_set_id]
puts "#{file_path}"
file = Pathname.new(file_path).open
actor = Hyrax::Actors::FileSetActor.new(FileSet.find(fileset_id), user)
actor.update_content(file)
end
Don't forget to set the variable user
and set your csv name in the CSV.foreach
line.