Ruby/CustomExporter.nuixscript/CustomExporter.rb
# Method used later on to move an export product file to appropriate structure
# based on template user provided
Method used later on to move an export product file to appropriate structure
based on template user provided
Method used later on to move an export product file to appropriate structure based on template user provided
def restructure_product(record,product_path_field,product_name,resolver,current_item,template,temp_export_directory,export_directory,placeholder_tags,pd)
	xref = {}
	original_path = record[product_path_field]
	original_fullpath = File.join(temp_export_directory,original_path).gsub(/\//,"\\")
	pathing_data = []
	if !java.io.File.new(original_fullpath).exists
		pd.logMessage("Could not find #{product_name} file: #{original_fullpath}")
	else
		current_item_tags = current_item.getTags.select{|tag| placeholder_tags[tag] == true}
		current_item_tags << "No Tags" if current_item_tags.size < 1
		current_item_tags.each do |tag|
			tag_path = tag.gsub(/\|/,"\\")
			resolver.set("tags",tag_path)
			updated_fullpath = resolver.resolveTemplatePath(template)
			updated_path = updated_fullpath.gsub(/^#{Regexp.escape(export_directory)}\\/i,".\\")
			pathing_data << {
				:updated_fullpath => updated_fullpath,
				:updated_path => updated_path
			}
		end
		pathing_data.uniq!
		pathing_data.each do |path_entry|
			updated_fullpath = path_entry[:updated_fullpath] 
			updated_path = path_entry[:updated_path] 
			was_truncated = false
			original_updated_fullpath = updated_fullpath
			if updated_fullpath.size >= 260
				pd.logMessage("Resolved path equals/exceeds 260 characters and will be truncated for item with GUID #{current_item.getGuid}")
				resolved_file_name = java.io.File.new(updated_fullpath).getName
				updated_fullpath = File.join(export_directory,"PATH_TOO_LONG",resolved_file_name)
				was_truncated = true
			end
			copy = 1
			temp_fullpath = updated_fullpath
			while java.io.File.new(temp_fullpath).exists
				extension = FilenameUtils.getExtension(updated_fullpath)
				file_base_name = FilenameUtils.getBaseName(updated_fullpath)
				directory = java.io.File.new(updated_fullpath).getParentFile.getAbsolutePath
				temp_fullpath = File.join(directory,"#{file_base_name}_#{copy}.#{extension}")
				copy += 1
			end
			updated_fullpath = temp_fullpath
			updated_fullpath = updated_fullpath.gsub("/","\\")
			updated_path = updated_fullpath.gsub(/^#{Regexp.escape(export_directory)}\\/i,".\\")
			if was_truncated
				File.open(File.join(export_directory,"PATH_TOO_LONG.TXT"),"a:utf-8") do |path_log|
					path_log.puts("="*20)
					path_log.puts("GUID: #{current_item.getGuid}")
					path_log.puts("Product: #{product_name}")
					path_log.puts("Resolved path of #{original_updated_fullpath.size} characters equals/exceeds 260 character limit")
					path_log.puts(" Resolved Path: #{original_updated_fullpath}")
					path_log.puts("Truncated Path: #{updated_fullpath}")
				end
			end
			path_entry[:updated_fullpath] = updated_fullpath
			path_entry[:updated_path] = updated_path
		end
		had_error = false
		pathing_data.each_with_index do |path_entry,path_entry_index|
			updated_fullpath = path_entry[:updated_fullpath]
			updated_path = path_entry[:updated_path]
			xref[original_path] = updated_path
			java.io.File.new(updated_fullpath).getParentFile.mkdirs
			begin
				puts "Renaming #{original_fullpath} to #{updated_fullpath}"
				if path_entry_index == pathing_data.size - 1
					java.io.File.new(original_fullpath).renameTo(java.io.File.new(updated_fullpath))
				else
					org.apache.commons.io.FileUtils.copyFile(java.io.File.new(original_fullpath),java.io.File.new(updated_fullpath))
				end
			rescue Exception => exc
				pd.logMessage("Error while renaming #{product_name} for #{current_item.getGuid}:")
				pd.logMessage(exc.message)
				record[product_path_field] = exc.message
				had_error = true
			end
		end
		if !had_error
			record[product_path_field] = pathing_data.map{|path_entry| path_entry[:updated_path]}.join("; ")
		end
	end
	return xref
end
def restructure_product ( record , product_path_field , product_name , resolver , current_item , template , temp_export_directory , export_directory , placeholder_tags , pd ) xref = { } original_path = record [ product_path_field ] original_fullpath = File . join ( temp_export_directory , original_path ) . gsub ( / \/ / , "\\" ) pathing_data = [ ] if ! java . io . File . new ( original_fullpath ) . exists pd . logMessage ( "Could not find #{product_name} file: #{original_fullpath}" ) else current_item_tags = current_item . getTags . select { | tag | placeholder_tags [ tag ] == true } current_item_tags << "No Tags" if current_item_tags . size < 1 current_item_tags . each do | tag | tag_path = tag . gsub ( / \| / , "\\" ) resolver . set ( "tags" , tag_path ) updated_fullpath = resolver . resolveTemplatePath ( template ) updated_path = updated_fullpath . gsub ( / ^ #{ Regexp . escape ( export_directory ) } \\ /i , ".\\" ) pathing_data << { :updated_fullpath => updated_fullpath , :updated_path => updated_path } end pathing_data . uniq! pathing_data . each do | path_entry | updated_fullpath = path_entry [ :updated_fullpath ] updated_path = path_entry [ :updated_path ] was_truncated = false original_updated_fullpath = updated_fullpath if updated_fullpath . size >= 260 pd . logMessage ( "Resolved path equals/exceeds 260 characters and will be truncated for item with GUID #{current_item.getGuid}" ) resolved_file_name = java . io . File . new ( updated_fullpath ) . getName updated_fullpath = File . join ( export_directory , "PATH_TOO_LONG" , resolved_file_name ) was_truncated = true end copy = 1 temp_fullpath = updated_fullpath while java . io . File . new ( temp_fullpath ) . exists extension = FilenameUtils . getExtension ( updated_fullpath ) file_base_name = FilenameUtils . getBaseName ( updated_fullpath ) directory = java . io . File . new ( updated_fullpath ) . getParentFile . getAbsolutePath temp_fullpath = File . join ( directory , "#{file_base_name}_#{copy}.#{extension}" ) copy += 1 end updated_fullpath = temp_fullpath updated_fullpath = updated_fullpath . gsub ( "/" , "\\" ) updated_path = updated_fullpath . gsub ( / ^ #{ Regexp . escape ( export_directory ) } \\ /i , ".\\" ) if was_truncated File . open ( File . join ( export_directory , "PATH_TOO_LONG.TXT" ) , "a:utf-8" ) do | path_log | path_log . puts ( "=" * 20 ) path_log . puts ( "GUID: #{current_item.getGuid}" ) path_log . puts ( "Product: #{product_name}" ) path_log . puts ( "Resolved path of #{original_updated_fullpath.size} characters equals/exceeds 260 character limit" ) path_log . puts ( " Resolved Path: #{original_updated_fullpath}" ) path_log . puts ( "Truncated Path: #{updated_fullpath}" ) end end path_entry [ :updated_fullpath ] = updated_fullpath path_entry [ :updated_path ] = updated_path end had_error = false pathing_data . each_with_index do | path_entry , path_entry_index | updated_fullpath = path_entry [ :updated_fullpath ] updated_path = path_entry [ :updated_path ] xref [ original_path ] = updated_path java . io . File . new ( updated_fullpath ) . getParentFile . mkdirs begin puts "Renaming #{original_fullpath} to #{updated_fullpath}" if path_entry_index == pathing_data . size - 1 java . io . File . new ( original_fullpath ) . renameTo ( java . io . File . new ( updated_fullpath ) ) else org . apache . commons . io . FileUtils . copyFile ( java . io . File . new ( original_fullpath ) , java . io . File . new ( updated_fullpath ) ) end rescue Exception => exc pd . logMessage ( "Error while renaming #{product_name} for #{current_item.getGuid}:" ) pd . logMessage ( exc . message ) record [ product_path_field ] = exc . message had_error = true end end if ! had_error record [ product_path_field ] = pathing_data . map { | path_entry | path_entry [ :updated_path ] } . join ( "; " ) end end return xref end
Method used later on to move an export product file to appropriate structure
based on template user provided
Method used later on to move an export product file to appropriate structure based on template user provided
# Handle things like: # - Too long file path # - Suffix when name collides with file already on disk # Absolute path # Relative path for loadfile # We need to handle path too long here since FileUtils.copyFile doesn't seem to be # throwing exceptions for this
record
product_path_field
product_name
resolver
current_item
template
temp_export_directory
export_directory
placeholder_tags
pd