Skip to content

Instantly share code, notes, and snippets.

@icy
Created October 24, 2024 07:31
Show Gist options
  • Save icy/3fbcfcbea27e4b8eeb031d742628c121 to your computer and use it in GitHub Desktop.
Save icy/3fbcfcbea27e4b8eeb031d742628c121 to your computer and use it in GitHub Desktop.
Add missing folders to the projects.json used by Windows 3d drawing application
#!/usr/bin/env ruby
# Purpose : Re-update missing drawing objects' metadata in projects.json
# Author : Ky-Anh Huynh
# Date : 2024-03-21
# License : MIT
require 'rubygems'
require 'json'
require 'securerandom'
# https://stackoverflow.com/questions/6161776/convert-windows-filetime-to-second-in-unix-linux/6161842#6161842
def windows_to_unix(timestamp)
timestamp/ 10000000 - 11644473600
end
def unix_to_window(timestamp)
10000000 * (timestamp + 11644473600)
end
old = JSON.load_file("Projects.json.beauty")
new = JSON.load_file("Projects.json.latest")
merged = old|new
STDERR.puts "Old size: #{old.size}, New size: #{new.size}, Merged size: #{merged.size}"
STDERR.puts "Old + new == Merged: #{old.size + new.size == merged.size}"
recorded_dirs = merged.map{|item| item["Path"].gsub('Projects\\', "")}
dirs = Dir.glob("Checkpoint*")
missing = dirs - recorded_dirs
missing_objects = missing.map do |draw|
mtime = File.mtime(draw)
uuid = SecureRandom.uuid
STDERR.puts "#{draw}, modified #{mtime}, uuid (generated): #{uuid}"
draw_as_ob = {
"Id" => "{#{uuid}}",
"SourceId" => "",
"Name" => "Papa-sorry-#{mtime.strftime("%Y-%m-%d")}",
"URI" => "ms-appdata:///local/Projects/#{draw}/Thumbnail.png",
"DateTime" => unix_to_window(mtime.to_f),
"Path" => "Projects\\#{draw}",
"SourceFilePath" => "",
"Version" => 0.21,
"IsRecovered" => false,
"IsPreviouslySaved" => true
}
end
finally =merged | missing_objects
puts JSON.dump(finally)
STDERR.puts "## Fixed for #{missing_objects.size} items"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment