Skip to content

Commit 9c94e51

Browse files
committed
v1.0.0.2562-Alpha
1 parent 71fbce2 commit 9c94e51

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2401
-0
lines changed

VirtualFileSystem/FsPath.cs

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace VirtualFileSystem
8+
{
9+
/// <summary>
10+
/// Provides file system operations. Helps determining file and folder existence and creating file and folder items.
11+
/// </summary>
12+
public static class FsPath
13+
{
14+
/// <summary>
15+
/// Returns true if the path points to a file. False - otherwise.
16+
/// </summary>
17+
/// <remarks>Throws exception if the file/folder does not exists.</remarks>
18+
/// <param name="path">Path to the file or folder.</param>
19+
/// <returns>True if the path is file. False - otherwise.</returns>
20+
public static bool IsFile(string path)
21+
{
22+
return !IsFolder(path);
23+
}
24+
25+
/// <summary>
26+
/// Returns true if the path points to a folder. False - otherwise.
27+
/// </summary>
28+
/// <remarks>Throws exception if the file/folder does not exists.</remarks>
29+
/// <param name="path">Path to the file or folder.</param>
30+
/// <returns>True if the path is folder. False - otherwise.</returns>
31+
public static bool IsFolder(string path)
32+
{
33+
FileAttributes attributes = File.GetAttributes(path);
34+
return (attributes & FileAttributes.Directory) == FileAttributes.Directory;
35+
}
36+
37+
/// <summary>
38+
/// Returns true if a file or folder exists under the specified path. False - otherwise.
39+
/// </summary>
40+
/// <remarks>Does not throw exceptions.</remarks>
41+
/// <param name="path">Path to the file or folder.</param>
42+
/// <returns>True if the file or folder exists under specified path. False - otherwise.</returns>
43+
public static bool Exists(string path)
44+
{
45+
return File.Exists(path) || Directory.Exists(path);
46+
}
47+
48+
/// <summary>
49+
/// Returns true if the path point to a recycle bin folder.
50+
/// </summary>
51+
/// <param name="path">Path to the file or folder.</param>
52+
public static bool IsRecycleBin(string path)
53+
{
54+
return path.IndexOf("\\$Recycle.Bin", StringComparison.InvariantCultureIgnoreCase) != -1;
55+
}
56+
57+
/// <summary>
58+
/// Returns storage item or null if item does not eists.
59+
/// </summary>
60+
/// <param name="path">Path to the file or folder.</param>
61+
/// <returns>
62+
/// Instance of <see cref="Windows.Storage.StorageFile"/> or <see cref="Windows.Storage.StorageFolder"/>
63+
/// that corresponds to path or null if item does not exists.
64+
/// </returns>
65+
public static async Task<Windows.Storage.IStorageItem> GetStorageItemAsync(string path)
66+
{
67+
if (File.Exists(path))
68+
{
69+
return await Windows.Storage.StorageFile.GetFileFromPathAsync(path);
70+
}
71+
if (Directory.Exists(path))
72+
{
73+
return await Windows.Storage.StorageFolder.GetFolderFromPathAsync(path);
74+
}
75+
return null;
76+
}
77+
78+
/// <summary>
79+
/// Returns folder or file item or null if the item does not exists.
80+
/// </summary>
81+
/// <param name="path">Path to the file or folder.</param>
82+
/// <returns>
83+
/// Instance of <see cref="FileInfo"/> or <see cref="DirectoryInfo"/>
84+
/// that corresponds to path or null if item does not exists.
85+
/// </returns>
86+
public static FileSystemInfo GetFileSystemItem(string path)
87+
{
88+
if (File.Exists(path))
89+
{
90+
return new FileInfo(path);
91+
}
92+
if (Directory.Exists(path))
93+
{
94+
return new DirectoryInfo(path);
95+
}
96+
return null;
97+
}
98+
99+
/// <summary>
100+
/// Gets file or folder attributes in a human-readable form.
101+
/// </summary>
102+
/// <param name="path">Path to the file or folder.</param>
103+
/// <returns>String that represents file or folder attributes or null if the file/folder is not found.</returns>
104+
public static string GetAttString(string path)
105+
{
106+
try
107+
{
108+
return File.GetAttributes(path).ToString();
109+
}
110+
catch
111+
{
112+
return null;
113+
}
114+
}
115+
116+
/// <summary>
117+
/// Returns true if the file hase a format ~XXXXX.tmp, false - otherwise.
118+
/// </summary>
119+
/// <param name="path">Path to a file or folder.</param>
120+
public static bool IsMsOfficeTemp(string path)
121+
{
122+
return Path.GetFileName(path).StartsWith('~') && Path.GetExtension(path).Equals(".tmp", StringComparison.InvariantCultureIgnoreCase);
123+
}
124+
125+
public static bool IsMsOfficeLocked(string path)
126+
{
127+
//string lockFileName = $"~${Path.GetFileName(path)}";
128+
//string lockPath = Path.Combine(Path.GetDirectoryName(path), lockFileName);
129+
130+
int separatorIndex = path.LastIndexOf(Path.DirectorySeparatorChar);
131+
string lockPath = path.Insert(separatorIndex + 1, "~$");
132+
return File.Exists(lockPath);
133+
}
134+
135+
/// <summary>
136+
/// Returns true if the file or folder is marked with Hidden or Temporaty attributes.
137+
/// </summary>
138+
/// <param name="path">Path to a file or folder.</param>
139+
/// <returns>
140+
/// True if the file or folder is marked with Hidden or Temporaty attributes.
141+
/// Returns false if no Hidden or Temporaty attributes found or file/folder does not exists.
142+
/// </returns>
143+
public static bool IsHiddenOrTemp(string path)
144+
{
145+
if(!FsPath.Exists(path))
146+
{
147+
return false;
148+
}
149+
150+
FileAttributes att = File.GetAttributes(path);
151+
return ( (att & System.IO.FileAttributes.Hidden) != 0)
152+
|| ((att & System.IO.FileAttributes.Temporary) != 0);
153+
}
154+
155+
/// <summary>
156+
/// Returns true if the file or folder should not be syched between the user file
157+
/// system and the remote storage. False - otherwise.
158+
/// </summary>
159+
/// <param name="path">Path to a file or folder.</param>
160+
public static bool AvoidSync(string path)
161+
{
162+
return IsMsOfficeLocked(path) || IsMsOfficeTemp(path) || IsHiddenOrTemp(path);
163+
}
164+
165+
/// <summary>
166+
/// Gets formatted file size or null for folders or if the file is not found.
167+
/// </summary>
168+
/// <param name="path">Path to a file or folder.</param>
169+
public static string Size(string path)
170+
{
171+
if (!File.Exists(path))
172+
{
173+
return null;
174+
}
175+
176+
long length;
177+
try
178+
{
179+
length = new FileInfo(path).Length;
180+
}
181+
catch
182+
{
183+
return null;
184+
}
185+
186+
string[] suf = { "b ", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
187+
if (length == 0)
188+
{
189+
return "0" + suf[0];
190+
}
191+
long bytes = Math.Abs(length);
192+
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
193+
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
194+
return (Math.Sign(length) * num).ToString() + suf[place];
195+
}
196+
}
197+
}

VirtualFileSystem/Mapping.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using ITHit.FileSystem;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Text;
6+
7+
namespace VirtualFileSystem
8+
{
9+
/// <summary>
10+
/// Maps user file system path to remote storage path and back.
11+
/// Creates user file system file or folder item based on remote storate item info.
12+
/// </summary>
13+
internal static class Mapping
14+
{
15+
/// <summary>
16+
/// Returns remote storage path that corresponds to user file system path.
17+
/// </summary>
18+
/// <param name="userFileSystemPath">Full path in user file system.</param>
19+
/// <returns>Path in remote storage that corresponds to <paramref name="userFileSystemPath"/>.</returns>
20+
public static string MapPath(string userFileSystemPath)
21+
{
22+
// Get path relative to the virtual root.
23+
string relativePath = Path.TrimEndingDirectorySeparator(userFileSystemPath).Substring(
24+
Path.TrimEndingDirectorySeparator(Program.Settings.UserFileSystemRootPath).Length);
25+
26+
// Get this folder path under the source folder.
27+
string sourcePath = $"{Path.TrimEndingDirectorySeparator(Program.Settings.RemoteStorageRootPath)}{relativePath}";
28+
return sourcePath;
29+
}
30+
31+
/// <summary>
32+
/// Returns user file system path that corresponds to remote storage path.
33+
/// </summary>
34+
/// <param name="remoteStoragePath">Full path in remote storage.</param>
35+
/// <returns>Path in user file system that corresponds to <paramref name="remoteStoragePath"/>.</returns>
36+
public static string ReverseMapPath(string remoteStoragePath)
37+
{
38+
// Get path relative to the virtual root.
39+
string relativePath = Path.TrimEndingDirectorySeparator(remoteStoragePath).Substring(
40+
Path.TrimEndingDirectorySeparator(Program.Settings.RemoteStorageRootPath).Length);
41+
42+
// Get this folder path under the source folder.
43+
string sourcePath = $"{Path.TrimEndingDirectorySeparator(Program.Settings.UserFileSystemRootPath)}{relativePath}";
44+
return sourcePath;
45+
}
46+
47+
/// <summary>
48+
/// Gets user file system item info from the remote storage data.
49+
/// </summary>
50+
/// <param name="remoteStorageItem">Remote storage item info.</param>
51+
/// <returns>User file system item info.</returns>
52+
public static FileSystemItemBasicInfo GetUserFileSysteItemInfo(FileSystemInfo remoteStorageItem)
53+
{
54+
FileSystemItemBasicInfo userFileSystemItem;
55+
56+
if (remoteStorageItem is FileInfo)
57+
{
58+
userFileSystemItem = new FileBasicInfo();
59+
}
60+
else
61+
{
62+
userFileSystemItem = new FolderBasicInfo();
63+
}
64+
65+
userFileSystemItem.Name = remoteStorageItem.Name;
66+
userFileSystemItem.Attributes = remoteStorageItem.Attributes;
67+
userFileSystemItem.CreationTime = remoteStorageItem.CreationTime;
68+
userFileSystemItem.LastWriteTime = remoteStorageItem.LastWriteTime;
69+
userFileSystemItem.LastAccessTime = remoteStorageItem.LastAccessTime;
70+
userFileSystemItem.ChangeTime = remoteStorageItem.LastWriteTime;
71+
72+
// Here you will typically store the file ETag. You will send the ETag to
73+
// the server inside If-Match header togater with updated content from client.
74+
// This will make sure the file on the server is not modified.
75+
//
76+
// In this sample, for the sake of simplicity, we use file last write time.
77+
userFileSystemItem.CustomData = BitConverter.GetBytes(remoteStorageItem.LastWriteTime.ToBinary());
78+
79+
if (remoteStorageItem is FileInfo)
80+
{
81+
((FileBasicInfo)userFileSystemItem).Length = ((FileInfo)remoteStorageItem).Length;
82+
};
83+
84+
return userFileSystemItem;
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)