Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions ChatGPTExport/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,10 @@
using System.Globalization;
using System.IO.Abstractions;
using System.Text.Json;

namespace ChatGPTExport
{
public static class ExtensionMethods
{
public static string GetRelativePathTo(this IFileSystem fileSystem, IDirectoryInfo baseDir, IFileInfo targetFile)
{
var basePath = fileSystem.Path.GetFullPath(baseDir.FullName + fileSystem.Path.DirectorySeparatorChar);
var targetPath = targetFile.FullName;

if (!string.Equals(fileSystem.Path.GetPathRoot(basePath), fileSystem.Path.GetPathRoot(targetPath), StringComparison.OrdinalIgnoreCase))
{
// Different drives, return full path
return targetPath;
}

var baseUri = new Uri(basePath);
var targetUri = new Uri(targetPath);

var relativePath = Uri.UnescapeDataString(baseUri.MakeRelativeUri(targetUri).ToString())
.Replace('/', fileSystem.Path.DirectorySeparatorChar);
return relativePath;
}

public static void SetCreationTimeUtcIfPossible(this IFile target, string filename, DateTime createdDate)
{
if (OperatingSystem.IsWindows())
{
target.SetCreationTimeUtc(filename, createdDate);
}
}

public static DateTimeOffset ToDateTimeOffset(this decimal d)
{
// Convert to total milliseconds
Expand Down
27 changes: 27 additions & 0 deletions ChatGPTExport/FileSystemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,32 @@ public static bool IsSameOrSubdirectory(this IDirectoryInfo baseDir, IDirectoryI

return candidatePath.StartsWith(basePath + fileSystem.Path.DirectorySeparatorChar, comparison);
}

public static string GetRelativePathTo(this IFileSystem fileSystem, IDirectoryInfo baseDir, IFileInfo targetFile)
{
var basePath = fileSystem.Path.GetFullPath(baseDir.FullName + fileSystem.Path.DirectorySeparatorChar);
var targetPath = targetFile.FullName;

if (!string.Equals(fileSystem.Path.GetPathRoot(basePath), fileSystem.Path.GetPathRoot(targetPath), StringComparison.OrdinalIgnoreCase))
{
// Different drives, return full path
return targetPath;
}

var baseUri = new Uri(basePath);
var targetUri = new Uri(targetPath);

var relativePath = Uri.UnescapeDataString(baseUri.MakeRelativeUri(targetUri).ToString())
.Replace('/', fileSystem.Path.DirectorySeparatorChar);
return relativePath;
}

public static void SetCreationTimeUtcIfPossible(this IFile target, string filename, DateTime createdDate)
{
if (OperatingSystem.IsWindows())
{
target.SetCreationTimeUtc(filename, createdDate);
}
}
}
}
Loading