using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
namespace UnityGLTF
{
public static class URIHelper
{
private const string Base64StringInitializer = "^data:[a-z-]+/[a-z-]+;base64,";
///
/// Get the absolute path to a gltf uri reference.
///
/// The path to the gltf file
/// A path without the filename or extension
public static string AbsoluteUriPath(Uri uri)
{
var partialPath = uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments[uri.Segments.Length - 1].Length);
return partialPath;
}
public static string GetFileFromUri(Uri uri)
{
return uri.Segments[uri.Segments.Length - 1];
}
///
/// Gets a directory name from a file path
///
/// Full path of a file
/// The name of directory file is in
public static string GetDirectoryName(string fullPath)
{
var fileName = Path.GetFileName(fullPath);
return fullPath.Substring(0, fullPath.Length - fileName.Length);
}
///
/// Tries to parse the uri as a base 64 encoded string
///
/// The string that represents the data
/// Returns the deencoded bytes
public static void TryParseBase64(string uri, out byte[] bufferData)
{
Regex regex = new Regex(Base64StringInitializer);
Match match = regex.Match(uri);
bufferData = null;
if (match.Success)
{
var base64Data = uri.Substring(match.Length);
bufferData = Convert.FromBase64String(base64Data);
}
}
///
/// Returns whether the input uri is base64 encoded
///
/// The uri data
/// Whether the uri is base64
public static bool IsBase64Uri(string uri)
{
Regex regex = new Regex(Base64StringInitializer);
Match match = regex.Match(uri);
return match.Success;
}
}
}