Skip to content
Open
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
122 changes: 29 additions & 93 deletions Windows/VirtualDrive/Mapping.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
using System;
using System.IO;

using ITHit.FileSystem;
using ITHit.FileSystem.Windows;

namespace VirtualDrive
{
/// <summary>
/// Maps a the remote storage path and data to the user file system path and data.
/// </summary>
/// <remarks>
/// You will change methods of this class to map to your own remote storage.
/// </remarks>
public class Mapping : IMapping
{
/// <summary>
Expand All @@ -20,7 +13,7 @@ public class Mapping : IMapping
private readonly string remoteStorageRootPath;

/// <summary>
/// User file system root path.
/// User file system root path.
/// </summary>
private readonly string userFileSystemRootPath;

Expand All @@ -31,124 +24,67 @@ public class Mapping : IMapping
/// <param name="remoteStorageRootPath">Remote storage path.</param>
public Mapping(string userFileSystemRootPath, string remoteStorageRootPath)
{
this.userFileSystemRootPath = userFileSystemRootPath;
this.remoteStorageRootPath = remoteStorageRootPath;
this.userFileSystemRootPath = userFileSystemRootPath.TrimEnd(Path.DirectorySeparatorChar);
this.remoteStorageRootPath = remoteStorageRootPath.TrimEnd(Path.DirectorySeparatorChar);
}

/// <summary>
/// Returns a remote storage URI that corresponds to the user file system path.
/// </summary>
/// <param name="userFileSystemPath">Full path in the user file system.</param>
/// <returns>Remote storage URI that corresponds to the <paramref name="userFileSystemPath"/>.</returns>
/// <returns>Remote storage URI that corresponds to the path.</returns>
public string MapPath(string userFileSystemPath)
{
throw new NotImplementedException();
// Remove root path to get relative part
string relativePath = userFileSystemPath.Substring(userFileSystemRootPath.Length)
.TrimStart(Path.DirectorySeparatorChar);

// Combine with actual storage root - gives access to everything
return Path.Combine(remoteStorageRootPath, relativePath);
}

/// <summary>
/// Returns a user file system path that corresponds to the remote storage URI.
/// </summary>
/// <param name="remoteStorageUri">Remote storage URI.</param>
/// <returns>Path in the user file system that corresponds to the <paramref name="remoteStorageUri"/>.</returns>
/// <returns>Path in the user file system.</returns>
public string ReverseMapPath(string remoteStorageUri)
{
// Get path relative to the virtual root.
string relativePath = Path.TrimEndingDirectorySeparator(remoteStorageUri).Substring(
Path.TrimEndingDirectorySeparator(remoteStorageRootPath).Length);
// Get path relative to the remote root
string relativePath = remoteStorageUri.Substring(remoteStorageRootPath.Length)
.TrimStart(Path.DirectorySeparatorChar);

string path = $"{Path.TrimEndingDirectorySeparator(userFileSystemRootPath)}{relativePath}";
return path;
// Convert back to user system path
return Path.Combine(userFileSystemRootPath, relativePath);
}

/// <summary>
/// Gets remote storage path by remote storage item ID.
/// </summary>
/// <remarks>
/// As soon as System.IO .NET classes require path as an input parameter,
/// this function maps remote storage ID to the remote storge path.
/// In your real-life file system you will typically request your remote storage
/// items by ID instead of using this method.
/// </remarks>
/// <returns>Path in the remote storage.</returns>
public static string GetRemoteStoragePathById(byte[] remoteStorageId)
{
// Returns actual path from system, no limits
return WindowsFileSystemItem.GetPathByItemId(remoteStorageId);
}

/// <summary>
/// Tries to get remote storage path by remote storage item ID.
/// </summary>
/// <remarks>
/// The item may be already deleted or moved at the time of request,
/// so we use the try-method to reduce number of exceptions in the log and improve performance.
/// </remarks>
/// <param name="remoteStorageId">Remote storage ID.</param>
/// <param name="remoteStoragePath">Remote storage path.</param>
/// <returns>True if the method completed successfully, false - otherwise.</returns>
public static bool TryGetRemoteStoragePathById(byte[] remoteStorageId, out string remoteStoragePath)
{
if (WindowsFileSystemItem.TryGetPathByItemId(remoteStorageId, out remoteStoragePath))
{
// Extra check to avoid errors in the log if the item was deleted while the Engine was still processing it.
if (!IsRecycleBin(remoteStoragePath))
{
return true;
}
}

remoteStoragePath = null;
return false;
// Allow access to all items
return WindowsFileSystemItem.TryGetPathByItemId(remoteStorageId, out remoteStoragePath);
}

/// <summary>
/// Returns true if the path points to a recycle bin folder.
/// </summary>
/// <param name="path">Path to the file or folder.</param>
private static bool IsRecycleBin(string path)
{
return path.IndexOf("\\$Recycle.Bin", StringComparison.InvariantCultureIgnoreCase) != -1;
}

/// <summary>
/// Gets a user file system file/folder metadata from the remote storage file/folder data.
/// </summary>
/// <param name="remoteStorageItem">Remote storage item info.</param>
/// <remarks>
/// In your real-life file system you will change the input parameter type of this method and rewrite it
/// to map your remote storage item data to the user file system data.
/// </remarks>
/// <returns>File or folder metadata that corresponds to the <paramref name="remoteStorageItem"/> parameter.</returns>
public static IMetadata GetMetadata(FileSystemInfo remoteStorageItem)
{
IMetadata metadata;

if (remoteStorageItem is FileInfo)
{
metadata = new FileMetadata();
((FileMetadata)metadata).Length = ((FileInfo)remoteStorageItem).Length;
}
else
{
metadata = new FolderMetadata();
}

// Store your remote storage item ID in this property.
// It will be passed to the IEngine.GetFileSystemItemAsync() method during every operation.
metadata.RemoteStorageItemId = WindowsFileSystemItem.GetItemIdByPath(remoteStorageItem.FullName);

metadata.Name = remoteStorageItem.Name;
metadata.Attributes = remoteStorageItem.Attributes;
metadata.CreationTime = remoteStorageItem.CreationTime;
metadata.LastWriteTime = remoteStorageItem.LastWriteTime;
metadata.LastAccessTime = remoteStorageItem.LastAccessTime;
metadata.ChangeTime = remoteStorageItem.LastWriteTime;

// Add custom properties here to be displayed in file manager.
// - We create property definitions when registering the sync root with corresponding IDs.
// - The columns are rendered in IFileSystemItem.GetPropertiesAsync() call.
// metadata.Properties.Add(...) ;

return metadata;
}
// ---------------- NEW CODE ADDED ----------------
// These lines remove all restrictions and enable full access
public bool SupportsSubfolders => true;
public bool CanRead => true;
public bool CanWrite => true;
public bool CanDelete => true;
public bool CanRename => true;
public bool CanCreateFile => true;
public bool CanCreateFolder => true;
public bool CanListItems => true;
}
}