117 lines
3.8 KiB
Ruby
117 lines
3.8 KiB
Ruby
|
|
#!/usr/bin/env ruby
|
||
|
|
require 'yaml' # For reading input
|
||
|
|
require 'json' # <--- REQUIRED FOR OUTPUT
|
||
|
|
require 'fileutils'
|
||
|
|
require 'pathname'
|
||
|
|
require 'net/http'
|
||
|
|
require 'uri'
|
||
|
|
require 'erb'
|
||
|
|
|
||
|
|
if ! ENV['IMPORT_INSTANCES']
|
||
|
|
exit 0 # nothing to do
|
||
|
|
end
|
||
|
|
|
||
|
|
INPUT_ROOT_DIR = '/root/instances'
|
||
|
|
OUTPUT_ROOT_DIR = '/mnt/instances'
|
||
|
|
|
||
|
|
FileUtils.mkdir_p(OUTPUT_ROOT_DIR)
|
||
|
|
|
||
|
|
puts "scanning yaml-files in /root/instances"
|
||
|
|
|
||
|
|
# 2. Iterate over all 'room.yaml' files recursively
|
||
|
|
Dir.glob(File.join(INPUT_ROOT_DIR, '**', 'room.yaml')).each do |input_file_path|
|
||
|
|
|
||
|
|
begin
|
||
|
|
input_data = YAML.load_file(input_file_path)
|
||
|
|
rescue StandardError => e
|
||
|
|
puts " ❌ Error reading or parsing YAML: #{e.message}"
|
||
|
|
next
|
||
|
|
end
|
||
|
|
|
||
|
|
relative_path = Pathname.new(input_file_path).relative_path_from(Pathname.new(INPUT_ROOT_DIR))
|
||
|
|
path_components = relative_path.to_s.split(File::SEPARATOR)
|
||
|
|
|
||
|
|
unless path_components.length >= 4
|
||
|
|
puts " ⚠️ Skipping: Path structure too shallow. Expected SERVER/AUTHOR/ROOM_NAME/room.yaml"
|
||
|
|
next
|
||
|
|
end
|
||
|
|
|
||
|
|
server_name = path_components[0]
|
||
|
|
author_name = path_components[1]
|
||
|
|
room_name = path_components[2]
|
||
|
|
|
||
|
|
begin
|
||
|
|
u = input_data['url']
|
||
|
|
r = Net::HTTP.new(URI.parse(u).host, URI.parse(u).port)
|
||
|
|
r.use_ssl = (URI.parse(u).scheme == 'https')
|
||
|
|
|
||
|
|
a = r.request(Net::HTTP::Head.new(URI.parse(u).request_uri))
|
||
|
|
|
||
|
|
if a.code.to_i != 200
|
||
|
|
puts " ⚠️ Skipping:HTTP code #{a.code} received :/"
|
||
|
|
next
|
||
|
|
end
|
||
|
|
|
||
|
|
# 5. Construct the final data structure (Hash)
|
||
|
|
output_data = {}
|
||
|
|
output_data['homepage'] = input_data['url']
|
||
|
|
output_data['name'] = input_data['title']
|
||
|
|
output_data['description'] = input_data['description']
|
||
|
|
output_data['image'] = input_data['thumbnail']
|
||
|
|
output_data['tags'] = [server_name]
|
||
|
|
|
||
|
|
output_dir = File.join(OUTPUT_ROOT_DIR, author_name, room_name)
|
||
|
|
output_file_path = File.join(output_dir, 'datapackage.json')
|
||
|
|
|
||
|
|
begin
|
||
|
|
|
||
|
|
url = URI(input_data['thumbnail'])
|
||
|
|
output_path = output_dir+"/.xrforge/thumbnail.jpg"
|
||
|
|
|
||
|
|
Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == "https") do |http|
|
||
|
|
request = Net::HTTP::Get.new(url)
|
||
|
|
http.request(request) do |response|
|
||
|
|
raise "Download failed: #{response.code}" unless response.is_a?(Net::HTTPSuccess)
|
||
|
|
|
||
|
|
File.open(output_path, "wb") do |file|
|
||
|
|
response.read_body do |chunk|
|
||
|
|
file.write(chunk)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
if a.code.to_i != 200
|
||
|
|
puts " ⚠️ Skipping:HTTP code #{a.code} receivedfor thumbnail :/"
|
||
|
|
next
|
||
|
|
end
|
||
|
|
|
||
|
|
# write datapackage.json
|
||
|
|
FileUtils.mkdir_p(output_dir) # Create directory (including parents)
|
||
|
|
File.write(output_file_path, JSON.pretty_generate(output_data))
|
||
|
|
puts " ✅ HTTP 200: Wrote #{output_file_path}"
|
||
|
|
|
||
|
|
# write janusxr.html + scene.jml
|
||
|
|
templateDir = File.dirname(__FILE__)+"/../../templates/"
|
||
|
|
FileUtils.mkdir_p(output_dir+"/.xrforge") # Create directory (including parents)
|
||
|
|
data = output_data
|
||
|
|
data['title'] = output_data['name']
|
||
|
|
$links = []
|
||
|
|
janusweb_src = ENV['DEV'] ? "/mnt/janusweb/janusweb.js" : "/mnt/janusweb/janusweb.min.js"
|
||
|
|
jml = ERB.new( File.read( templateDir+"JML/portalAR.erb" ) ).result(binding)
|
||
|
|
html = ERB.new( File.read( templateDir+"JML/client.html") ).result(binding)
|
||
|
|
File.write(output_dir+"/.xrforge/scene.jml", jml )
|
||
|
|
puts " ✅ Wrote #{output_dir}/.xrforge/scene.jml"
|
||
|
|
File.write(output_dir+"/.xrforge/janusxr.html", html )
|
||
|
|
puts " ✅ Wrote #{output_dir}/.xrforge/janusxr.html"
|
||
|
|
|
||
|
|
rescue StandardError => e
|
||
|
|
puts " ❌ Error writing output file: #{e.message}"
|
||
|
|
end
|
||
|
|
rescue StandardError => e
|
||
|
|
# Optionally handle error quietly or output e.message
|
||
|
|
end
|
||
|
|
|
||
|
|
end
|
||
|
|
|