|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace ITHit.FileSystem.Samples.Common.Windows |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Provides method for reading and writing ETags. |
| 11 | + /// </summary> |
| 12 | + public class ETagManager |
| 13 | + { |
| 14 | + private readonly string userFileSystemPath; |
| 15 | + private readonly string userFileSystemRootPath; |
| 16 | + private readonly string serverDataFolderPath; |
| 17 | + private readonly ILogger logger; |
| 18 | + internal readonly string ETagFilePath; |
| 19 | + |
| 20 | + private const string eTagExt = ".etag"; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Creates instance of this class. |
| 24 | + /// </summary> |
| 25 | + /// <param name="userFileSystemRootPath">User file system root path.</param> |
| 26 | + /// <param name="serverDataFolderPath">Folder where ETags are stored.</param> |
| 27 | + /// <param name="logger">Logger.</param> |
| 28 | + internal ETagManager(string userFileSystemPath, string serverDataFolderPath, string userFileSystemRootPath, ILogger logger) |
| 29 | + { |
| 30 | + this.userFileSystemPath = userFileSystemPath; |
| 31 | + this.userFileSystemRootPath = userFileSystemRootPath; |
| 32 | + this.serverDataFolderPath = serverDataFolderPath; |
| 33 | + this.logger = logger; |
| 34 | + this.ETagFilePath = $"{GetEtagFilePath(userFileSystemPath)}{eTagExt}"; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Creates or updates ETag associated with the file. |
| 39 | + /// </summary> |
| 40 | + /// <param name="eTag">ETag.</param> |
| 41 | + /// <returns></returns> |
| 42 | + public async Task SetETagAsync(string eTag) |
| 43 | + { |
| 44 | + // Delete ETag file if null or empty string value is passed. |
| 45 | + if (string.IsNullOrEmpty(eTag) && File.Exists(ETagFilePath)) |
| 46 | + { |
| 47 | + DeleteETag(); |
| 48 | + } |
| 49 | + |
| 50 | + Directory.CreateDirectory(Path.GetDirectoryName(ETagFilePath)); |
| 51 | + await File.WriteAllTextAsync(ETagFilePath, eTag); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Gets ETag associated with a file. |
| 56 | + /// </summary> |
| 57 | + /// <returns>ETag.</returns> |
| 58 | + public async Task<string> GetETagAsync() |
| 59 | + { |
| 60 | + if (!File.Exists(ETagFilePath)) |
| 61 | + { |
| 62 | + return null; |
| 63 | + } |
| 64 | + return await File.ReadAllTextAsync(ETagFilePath); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Moves ETag to a new location. |
| 69 | + /// </summary> |
| 70 | + /// <param name="userFileSystemNewPath">Path of the file in the user file system to move this Etag to.</param> |
| 71 | + internal async Task MoveToAsync(string userFileSystemNewPath) |
| 72 | + { |
| 73 | + // Move ETag file. |
| 74 | + string eTagTargetPath = GetEtagFilePath(userFileSystemNewPath); |
| 75 | + string eTagFileTargetPath = $"{eTagTargetPath}{eTagExt}"; |
| 76 | + |
| 77 | + // Ensure the target directory exisit, in case we are moving into empty folder or which is offline. |
| 78 | + new FileInfo(eTagFileTargetPath).Directory.Create(); |
| 79 | + File.Move(ETagFilePath, eTagFileTargetPath); |
| 80 | + |
| 81 | + // If this is a folder, move all eTags in this folder. |
| 82 | + string eTagSourceFolderPath = GetEtagFilePath(userFileSystemPath); |
| 83 | + if (Directory.Exists(eTagSourceFolderPath)) |
| 84 | + { |
| 85 | + Directory.Move(eTagSourceFolderPath, eTagTargetPath); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Deletes ETag associated with a file. |
| 91 | + /// </summary> |
| 92 | + internal void DeleteETag() |
| 93 | + { |
| 94 | + File.Delete(ETagFilePath); |
| 95 | + |
| 96 | + // If this is a folder, delete all eTags in this folder. |
| 97 | + string eTagFolderPath = GetEtagFilePath(userFileSystemPath); |
| 98 | + if (Directory.Exists(eTagFolderPath)) |
| 99 | + { |
| 100 | + Directory.Delete(eTagFolderPath, true); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Returns true if the remote storage ETag and user file system ETags are equal. False - otherwise. |
| 106 | + /// </summary> |
| 107 | + /// <param name="remoteStorageItem">Remote storage item info.</param> |
| 108 | + /// <remarks> |
| 109 | + /// ETag is updated on the server during every document update and is sent to client with a file. |
| 110 | + /// During user file system to remote storage update it is sent back to the remote storage together with a modified content. |
| 111 | + /// This ensures the changes in the remote storage are not overwritten if the document on the server is modified. |
| 112 | + /// </remarks> |
| 113 | + public async Task<bool> ETagEqualsAsync(FileSystemItemMetadata remoteStorageItem) |
| 114 | + { |
| 115 | + string remoteStorageETag = remoteStorageItem.ETag; |
| 116 | + string userFileSystemETag = await GetETagAsync(); |
| 117 | + |
| 118 | + if (string.IsNullOrEmpty(remoteStorageETag) && string.IsNullOrEmpty(userFileSystemETag)) |
| 119 | + { |
| 120 | + // We assume the remote storage is not using ETags or no ETag is ssociated with this file/folder. |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + return remoteStorageETag == userFileSystemETag; |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Gets ETag file path (without extension). |
| 129 | + /// </summary> |
| 130 | + /// <param name="userFileSystemPath">Path of the file in user file system to get ETag path for.</param> |
| 131 | + private string GetEtagFilePath(string userFileSystemPath) |
| 132 | + { |
| 133 | + // Get path relative to the virtual root. |
| 134 | + string relativePath = userFileSystemPath.TrimEnd(Path.DirectorySeparatorChar).Substring( |
| 135 | + userFileSystemRootPath.TrimEnd(Path.DirectorySeparatorChar).Length); |
| 136 | + |
| 137 | + return $"{serverDataFolderPath.TrimEnd(Path.DirectorySeparatorChar)}{relativePath}"; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments